如何让 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 用户指南:完成、旧和新