2

I have fuzzy file finder (FZF) set up and working:

enter image description here

And when I do :History it also shows a preview of the file:

enter image description here

I've been able to do a find with passing an "empty" preview to it:

:FZF --preview 'cat'

enter image description here

However, as soon as I pass any argument (actually anything more than a single argument, regardless of what it is), I get:

:FZF --preview 'cat {}'
Error running '/Users/david/.vim/plugged/fzf/bin/fzf'  '--multi' '--prompt' '~/' '-
-preview' ''\''cat' '{}'\''' --expect=ctrl-v,ctrl-x,ctrl-t --height=16 > /var/folde
rs/b1/mmc6r_jj00ldzr2prvqzy56m0000gp/T/vsmz6fL/47

What seems to be the issue here, and how can I fix it? Tried it both on mac and linux and get the same result.

Note: this exact same command works from the command-line:

$ /Users/david/.vim/plugged/fzf/bin/fzf --multi --prompt ~/ --preview 'cat {}' --expect=ctrl-v,ctrl-x,ctrl-t --height=16 > /var/folders/b1/mmc6r_jj00ldzr2prvqzy56m0000gp/T/vsmz6fL/47
  • Hey David, I edited your error message to use a code-block, but I cant tell if the newlines were in the original or not. Can you please give the exact error message? – D. Ben Knoble Apr 28 at 12:04
3

The arguments you pass to :FZF are split at spaces and tabs, because :FZF uses the escape sequence <f-args>. This means that Vim splits the arguments on these 2 spaces:

:FZF --preview 'cat {}'
              ^    ^
              ✔    ✘

You don't want Vim to split the arguments at the second space. Instead you want the space to be embedded into the arguments. To do so, you need to escape it:

:FZF --preview 'cat\ {}'
                   ^

But there is still one issue. <f-args> quotes each argument individually. So, s:cmd() receives these 2 arguments:

'--preview'
'''cat\ {}'''
 ^^       ^^
 embedded single quotes

On the command-line, you provided cat\ {} as a string, which Vim has embedded inside another string. This will prevent the shell from splitting the command name (here cat(1)) from its file argument. To avoid this, remove the quotes around cat\ {}:

:FZF --preview cat\ {}
| improve this answer | |
3

I'm not sure why this error occurs (I have exactly the same here), however there's a workaround. So this is not a direct answer, but I hope it helps nonetheless.

You can set defaults options for FZF in your shell config file.

In my case, my .bashrc holds those lines:

export FZF_DEFAULT_OPTS="-m"
FZF_DEFAULT_OPTS+=" --color='light'"
FZF_DEFAULT_OPTS+=" --height 40%"
FZF_DEFAULT_OPTS+=" --bind 'ctrl-u:preview-up,ctrl-d:preview-down,ctrl-o:toggle+up,ctrl-i:toggle+down,ctrl-space:toggle-preview'"
FZF_DEFAULT_OPTS+=" --preview 'head -500 {}'"
FZF_DEFAULT_OPTS+=" --preview-window=:hidden"

This is the relevant one:

FZF_DEFAULT_OPTS+=" --preview 'head -500 {}'"

These settings will be used in vim.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.