LoginSignup
1

More than 5 years have passed since last update.

zsh+fzfで実行可能なコマンドをあいまい検索する

Last updated at Posted at 2017-09-17

半年くらい前から,こちらの記事を参考にzsh+fzfライフになりました.

おい、peco もいいけど fzf 使えよ - Qiita

前々から$PATHやシェル関数をインタラクティブフィルタできると嬉しいなあと思っていたのですが,公式のwikiでは見つけられなかった(もしかしたらあるのかも)ので,自分で作ってみました.

シェル関数とキーバインドの追加

.zshrc
# search commands and shell functions
fzf-cmd() {
  local selected
  selected=`(for p in ${(@s/:/)PATH}; do
    find $p -depth 1 -perm +111 -type f -or -type l 2>/dev/null
    done;
    print -l ${(ok)functions} | grep '^[^_]') |\
      sed 's|.*/||' | fzf --query="$LBUFFER"`
  if [ -n "$selected" ]; then
    LBUFFER="$selected "
  fi
  zle reset-prompt
}

zle -N fzf-cmd
bindkey '^[' fzf-cmd
  • fzfのインストールなどはされている前提です
  • $PATH にある実行可能ファイルと,シェル関数のリストからコマンド名だけにしてfzfに渡しています
  • シェル関数は _ で始まるものは除いています

実行イメージ

Sep-18-2017 00-31-28.gif

  • コマンドの一部など曖昧な場合に,キーワードだけ入れて ^[ すればfzfできます

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
1