In the fzf plugin you can define mappings for the fzf window to open the selected file in a vertical split, etc. How can I make fzf mapping to insert the path of the selected file as a string into the current buffer? I want to use this mapping inside the fzf-window opend by :FZF and similar commands.


Add FZF setup:

    func! s:insert_file_name(lines)
        let @@ = fnamemodify(a:lines[0], ":p")
        normal! p
    let g:fzf_action = { 'ctrl-r': function('s:insert_file_name')}

Then open :Files select a file and press <c-r>.

See :h fzf-examples.

  • If you change ":p" to ":f" it will give you the relative path – joshpetit Mar 24 at 15:07

You will need to run the underlying FZF functions to do so.

Say you intend to adapt Files to do so.

:Files probably calls fzf#run at some point. Find that call.

The sink argument of fzf#run (and grep and others) would be a function that says what to do with the selected string.

I have the following code:

:call fzf#run({'source': uniq(reverse(b:inserts)),'sink':function('PInsert'),'options': '-m'})

The source is the list of lines to select from. And PInsert would insert the selection to the buffer.

function! PInsert(item)
    let @z=a:item
    norm "zp

So you just need to create a custom command and use PInsert as the sink argument.

Good luck.

  • See :h fzf ................ – eyal karni Aug 18 '19 at 16:31
  • 1
    Instead of clobbering the z register, do put =a:item – D. Ben Knoble Aug 18 '19 at 16:44
  • It is a bit different since put works linewise. – eyal karni Aug 20 '19 at 11:42

You could always yank it and put it. I dont know how to create fzf mappings but the right hand side would basically be something like

normal! yaW<C-w>pp

Or in “pure” Ex commands, which work linewise:

yank | wincmd p | put

Depending on how much you want to yank.

  • You cannot (as far as I know) use normal mode command when selecting files in the fzf promt. I have tried to clarify my question. – Andreas Mar 20 '19 at 10:32
  • @Andreas see edita – D. Ben Knoble Mar 20 '19 at 12:35

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.