如何在zsh中重複目前輸入的參數?

如何在zsh中重複目前輸入的參數?

有時我們只需要在使用時輸入稍微不同的名稱mv/cp/convert。例如,

convert IMG-long-number.jpg  IMG-long-number.png

如何在輸入 IMG-long-number.png 之前重複 IMG-long-number.jpg,這樣我只需要進行小的調整?

這類似於如何在 bash 控制台上重複目前輸入的參數?但對於 zsh/zle 來說。

答案1

!#$<Tab>對我有用。鑑於:

$ echo a

鍵入!#$然後按下Tab將擴展!#$a。如果您嘗試使用下列命令進行操作,製表符補全也會列出其他選項:

$ echo a !#$:
&  -- repeat substitution
A  -- absolute path resolving symbolic links
Q  -- strip quotes
a  -- absolute path
c  -- PATH search for command
e  -- leave only extension
g  -- globally apply s or &
h  -- head - strip trailing path element
l  -- lower case all words
q  -- quote to escape further substitutions
r  -- root - strip suffix
s  -- substitute string
t  -- tail - strip directories
u  -- upper case all words

答案2

另一種需要記住的方法是:你不會這樣做。

使用完成後得到

轉換 IMG-long-number.jpg
只需將其編輯為

轉換 IMG-long-number.{jpg,png}

答案3

我會用Ctrl+Alt+_它來複製前一個單字(copy-prev-word小部件)。複製後,您可以使用Backspace編輯副本的副檔名。

或使用Ctrl+W將其整體刪除。預設情況下,Ctrl+W刪除整個單字(定義為 alnums+$WORDCHARS),但您可以根據需要使用小部件更改行為select-word-style

~/.zshrc

autoload select-word-style
zle -N select-word-style
bindkey '\eW' select-word-style

然後,您可以選擇不同的字型Alt+Shift+W。您可以在這裡使用bash單字樣式(僅單字alnums)(參考如何bash單字小部件以外Ctrl+W喜歡Alt+BAlt+D...考慮)。

答案4

類似於 @muru 建議的 - !#$<TAB>。但我也在我的 .zshrc 中做了這樣的綁定:

bindkey -s "^[," "!#\$^I"

這樣,按Escthen 逗號即可為您完成此操作,類似於Escthen 點將為您提供上一個命令的最後一個參數。

相關內容