2013-04-25 43 views
7

我使用zsh和emacs键绑定。有时候我需要用不同的输入来执行相同的命令。输入ususally有共同的子串。有没有简单的方法来替换以前的命令的一部分与其他字符串?例如,以前的命令是:替换zsh命令行中的字符串

cat chr2.unknow.feature.filtered ../chr2.unknow.feature.filtered ../train/chr2.unknow.withpvalue.feature.filtered > chr2.combined.txt 

我该如何轻松地将'chr2'替换为'chr3'?

的扩展问题,如何在命令替换多个不同子到其他不同的字符串:

cat chr1.unknow.feature.filtered ../chr2.unknow.feature.filtered ../train/chr3.unknow.withpvalue.feature.filtered 

如何更换,让我们说,“CHR 1”与“chrX”,“CHR 2” wtih 'chrY','chr3'和'chrZ'?

谢谢。

回答

17

你也可以做^old^new快速历史替换(快了很多比!!:s^old^new^

~ % cd /tmp 
/tmp % ^tmp^etc # <-- changes tmp for etc 
/tmp % cd /etc 
/etc % 

这仅改变第一次出现。如果您需要更多,请使用任何历史修饰符。例如:全球变化:

/etc % echo tmp tmp tmp 
tmp tmp tmp 
/etc % ^tmp^etc 
/etc % echo etc tmp tmp # <--- only the first tmp got changed 
etc tmp tmp 
/etc % ^tmp^etc^:G  # <-- 'global' 

PS:搜索zshexpn手册^foo^bar找到描述此部分。向下滚动一下(在手册中),你会看到'历史扩展修饰符'列表。

PS:对于多个取代,这样做:^old1^new1^:s^old2^new2^

6

要acomplish你问什么直接:

$ !!:s/chr1/chrX/:s/chr2/chrY/:s/chr3/chrZ/ 

这将修改最后输入的命令。要执行全局替换,请使用:gs/a/b/

实施例:

$ man zshall 
$ !!:gs/man/less/:gs/zshall/.history/<TAB> 
$ less .history 

了解更多在man zshexpn - ZSH扩展和替换。

+0

谢谢您的回答。由于弗朗西斯科提供了两种方式,所以我接受他并且赞扬你的观点。 – Rainfield 2013-04-26 19:06:23

+0

那么你一次要求多次替换,AFAIU'^old^new ^'只允许一次替换。 – 2013-04-26 19:24:35

+1

注意'^ old^new ^'只是'!!:s^foo^bar ^'的同义词。所以如果你想做多个替换,你可以像你一样继续。我会更新我的答案... – Francisco 2013-04-27 13:16:20