如何讓zsh tab補全來cat **/foo<TAB>
匹配並完成foo*
目前目錄下任意子目錄中的第一個檔案匹配?
例如,在新的測試目錄中執行此操作:(同樣,這是 zsh)
% mkdir aaa bbb ccc
% touch aaa/foo bbb/foo ccc/foo
% cat **/f<TAB>
當我點擊最後一行時,我想要的<TAB>
是我的螢幕最終看起來像這樣:
% cat aaa/foo_ # filled in the first match; "_" is the cursor
aaa/foo bbb/foo ccc/foo # and here is the list of all matches
我嘗試過setopt GLOB_COMPLETE
,但這並沒有達到我想要的效果。
答案1
將以下內容新增至您的~/.zshrc
文件(或將其貼到命令列中進行嘗試):
# Load Zsh's new completion system.
autoload -Uz compinit && compinit
# Bind Tab to complete-word instead of
# expand-or-complete. This is required for
# the new completion system to work
# correctly.
bindkey '^I' complete-word
# Add the _match completer.
# We add it after _expand & _complete, so it
# will get called only once those two have
# failed.
# _match_ completes patterns only, which
# _expand can do, too, (which is why we call
# _match_ only when _expand & _complete
# fail), but _match adds an extra * at the
# cursor position. Without that, the pattern
# **/f would not match {aaa,bbb,ccc}/foo
zstyle ':completion:*' completer \
_expand _complete _match _ignored
# Let all possible completions for a partial
# path be listed, rather than just the first
# one.
zstyle ':completion:*' list-suffixes true
然後,當您鍵入cat **/f
並按時Tab,您將得到以下輸出:
% cat aaa/foo
aaa/foo bbb/foo ccc/foo
**/f
文件:
也可以看看:Z-Shell 使用者指南:完成,新舊