zsh タブ補完を使用して、現在のディレクトリの下の任意のサブディレクトリで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
ドキュメンテーション: