2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

zshの補完で候補をインクリメンタルに絞り込む

Last updated at Posted at 2023-10-24

ここまではみんなやっていると思う。メニュー補完を有効にする設定だ。

autoload -Uz compinit && compinit -u
zstyle ':completion:*' menu select

次のように書くと、候補をインクリメンタルサーチで絞り込める(前方一致)。

zstyle ':completion:*' menu select interactive

あるいは次のように書くと、候補をインクリメンタルサーチで絞り込める(部分一致)。ただし候補を選択確定するのにEnterを2回押さなければならなくなる。

zstyle ':completion:*' menu select search

Tabを押した直後は通常のメニュー補完で、特定のキーを押したときだけ絞り込みモードに移行することもできる:

zstyle ':completion:*' menu select
bindkey -M menuselect '^K' vi-insert                           # 前方一致で絞り込み
bindkey -M menuselect '^L' history-incremental-search-forward  # 部分一致でインクリメンタルサーチ(not 絞り込み)

他にもメニュー補完中のキーバインドはbindkey -M menuselectでカスタマイズ可能だ。

accept-line - Validate the selection and leave the menu.
send-break - Leaves the menu selection and restore the previous command.
clear-screen - Clear the screen without leaving the menu selection.
accept-and-hold - Insert the match in your command and let the completion menu open to insert another match.
accept-and-infer-next-history - Insert the match and, in case of directories, open completion menu to complete its children.
undo - Undo.
vi-forward-blank-word - Move cursor to next group of match.
vi-backward-blank-word - Move cursor to previous group of match.
beginning-of-buffer-or-history - Move the cursor to the leftmost column.
end-of-buffer-or-history - Move the cursor to the rightmost column.
vi-insert - Toggle between normal and interactive mode. We’ve seen the interactive mode above.
history-incremental-search-forward and history-incremental-search-backward - Begin incremental search.

特に下記2個は設定しておくとたまに便利かもしれない。

bindkey -M menuselect '^O' accept-and-hold                # 選択してメニューを維持(複数選択する場合に便利)
bindkey -M menuselect '^J' accept-and-infer-next-history  # 候補を選択し、再度補完を起動する(パスの続きを入力する際に便利)

参考:

これらのstyleの公式なドキュメントはStandard Stylesmenuのところにある。

こういうチマチマした設定をせずにzshを便利にしたい人にはzsh4humansがいいかもしれない。powerlevel10k作者のromkatv氏による「設定無しで欲しい機能が全部揃っている」zshフレームワークで、fzfによる補完候補絞り込み機能がある。romkatv氏のことだから、速度にも最大限に注意を払っていると思われる。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?