FZF and Text Editors
19 Apr 2018 • Ian Emnace

Because of the Unix-like nature of FZF (list through stdin, selection through stdout), it’s very easy to come up with different uses for it.

One of my personal favorites is to select files to open in my text editor. Most basic would be:

$ vim "$(fzf)"

Since FZF uses find to list files by default.

Multiple files: the problem

What about multiple files, though? FZF has --multi, so…

$ # Doing it wrongly
  $ vim "$(fzf --multi)"

Unfortunately, if you try to select a.txt and b.txt with that, you’ll find that vim will open a single file with one long a.txt^@b.txt as its filename.

You can’t exactly go unquoted either:

$ # Still doing it wrongly
  $ vim $(fzf --multi)

Because now if you select My First Text File.txt, vim will open 4 files, My, First, Text, and File.txt.

Multiple files: the solution

I have a more robust version of the above (slightly more robust than warranted in most cases), stored in a shell function:

# Requires arrays, so bash >= 4, zsh, etc
  ez() {
      while IFS= read -r -d '' file; do
      done < <(fzf --multi --print0)

      (( ${#files} )) || return
      "${VISUAL:-${EDITOR:-vi}}" "$@" "${files[@]}"

Because of the reliance on VISUAL and EDITOR, you can easily use this for any text editor of your choice (vim, emacs, nano, any modern editor, etc).

FZF from within the running editor

Once you already have a text editor instance running, it’s pretty easy to make use of a plugin to integrate with FZF.

I personally use Vim, so naturally I have the fzf.vim plugin.