2

How can I launch fzf inside vim and open the selected file on a split window? I don't want to install any plugin.

I tried

:!a=$(ls | fzf) && vsplit $a
AdminBee
  • 21,637
  • 21
  • 47
  • 71
testoflow
  • 117
  • 8
  • Are you using vim or neovim? Because if you are using neovim it would be possible. With normal vim as far as I know without a plugin it is impossible. – mad_a_i Aug 12 '20 at 16:31
  • I'm using vim. I didn't try neovim. You say it could be possible do it in neovim. how? thanks for answering btw. – testoflow Aug 12 '20 at 18:16
  • 2
    Interesting question. The problem is that fzf is sending output to stdout the whole time but you want to get just the result. Only thing I got to work so far requires use of a temporary file: `exe '!ls | fzf >/tmp/foo ' | exe 'vsplit ' . system('cat /tmp/foo')` .... might be cleaner solution than that but I'm not sure. – B Layer Aug 20 '20 at 07:51

1 Answers1

1

Since fzf needs access to the console, you will need to run it as a foreground command and there isn't really a good way for you to do that and capture the output from fzf (the filename) in Vim through a pipe. So using a temporary file to store the filename is the easiest approach here.

A simple solution is:

function! SelectFile()
  let tmp = tempname()
  execute '!ls | fzf >'.tmp
  let fname = readfile(tmp)[0]
  silent execute '!rm '.tmp
  execute 'vsplit '.fname
endfunction

Then use it with:

:call SelectFile()

(Or create a key mapping or a user command for it.)

Using the Vim :terminal to run fzf inside it (instead of shelling out with :!) is also a good option.

In any case, the real best approach here is to just use one of the excellent Vim plug-ins, either the simple bare-bones one which is included with fzf itself and probably also the more complete fzf.vim which builds on the low-level plug-in to deliver powerful features you'll benefit from having around.

filbranden
  • 21,113
  • 3
  • 58
  • 84