2013-04-25 42 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