Build a File Finder for Zsh

I have used zsh for 2+ years and I am really happy with it. Sometime I have to insert path of files, usually I have to type in characters, tab or /, characters, tab or /, etc… I want to have something like Ctrl-P for vim for me to find the path quickly and insert to my current editing command. I also want to do it again and again, like committing multiple files into git with one single command.

A plugin of zaw

Building from scratch is painful so I decided to build as a plugin on top of zaw.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function zaw-src-fuzzy() {
  # Get the file list by find .
  OLDIFS=$IFS
  IFS=$'\n'
  candidates=($(find .))
  candidates=(${(iou)candidates[@]})
  IFS=$OLDIFS

  # Define what kind of action can be performed on the selected item
  # first: accept-line
  # second: accept-search
  actions=("zaw-callback-execute" "zaw-callback-append-to-buffer")
  act_descriptions=("execute" "append to edit buffer")
}
# Register our plugin
zaw-register-src -n fuzzy zaw-src-fuzzy

# Setup Ctrl-F shortcut to trigger
function fuzzy-start { kill-line; zaw-fuzzy }
zle -N fuzzy-start
bindkey '^F' fuzzy-start

Source in your .zshrc file, press ctrl-f in zsh and enjoy.

Comments