LoginSignup
3
1

More than 3 years have passed since last update.

zshで、Gitリポジトリ内のファイルをfuzzy searchして、Vimで開く

Last updated at Posted at 2019-06-30

peco を使うと、標準入力から受け取ったテキストデータをあいまい検索して、選択した行を標準出力に返す事ができる。

$ echo "apple\nbanana\nchocolate" | peco

apple-banana-chocolate.gif

これを利用して、あるリポジトリ内にあるファイル一覧を出力し、pecoに渡すと、ファイル名のfuzzy search(あいまい検索)をすることが出来る。

$ git ls-files | peco

ls-file.gif

さらに、fuzzy searchの出力結果をvimに渡してやるというような処理を peco-vim というfunctionとして定義し、 Ctrl-[ にマッピングしてやる。

このソースは参考記事にある peco-src を改変したものだ。

.zshrc
bindkey '^[' peco-vim

function peco-vim() {
    local src=$(git ls-files | peco --query "$LBUFFER" --prompt "vim>")
    if [ -n "$src" ]; then
        BUFFER="vim $src"
        zle accept-line
    fi
    zle -R -c
}
zle -N peco-vim

peco-vim.gif

結構便利だと思う。

参考

3
1
1

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
3
1