概要
Powershell7ではデフォルトで予測機能が有効になっており、ヒットする履歴をリスト表示できるようになっています。
-> https://learn.microsoft.com/ja-jp/powershell/scripting/learn/shell/using-predictors?view=powershell-7.4
この機能をzshのプラグインを組み合わせて実現します。
この画像のように「vim」と入力するだけで、以前に使用した「vim」にヒットするコマンドの履歴がリスト表示されます。
導入手順
fzfのインストール
$ brew install fzf
zsh-autocompleteのインストール
入れ方は色々あるようですが、自分はoh-my-zshを利用する形でインストールしました。
$ git clone https://github.com/marlonrichert/zsh-autocomplete.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autocomplete
.zshrcに設定を追加
# 履歴設定
HISTFILE=~/.zsh_history
HISTSIZE=100000
SAVEHIST=100000
setopt share_history
setopt hist_ignore_dups
setopt hist_ignore_all_dups
setopt hist_ignore_space
setopt hist_reduce_blanks
setopt print_eight_bit
# 履歴表示するコマンド設定
function fzf-select-history() {
BUFFER=$(history -n -r 1 | fzf --query "$LBUFFER" --reverse)
CURSOR=$#BUFFER
zle accept-line
}
zle -N fzf-select-history
bindkey '^r' fzf-select-history # ↓のautocompleteがない場合はctrl + r で履歴表示
# 文字入力に応じた自動補完設定
zstyle ':autocomplete:*' default-context history-incremental-search-backward
zstyle ':autocomplete:history-incremental-search-backward:*' min-input 1
# プラグインにzsh-autocompleteを追加
pluguins=(
zsh-autocomplete
)
