2

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.

Andreas
  • 357
  • 2
  • 10

3 Answers3

3

Add FZF setup:

    func! s:insert_file_name(lines)
        let @@ = fnamemodify(a:lines[0], ":p")
        normal! p
    endfunc
    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.

Maxim Kim
  • 13,376
  • 2
  • 18
  • 46
1

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
endfunction

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

Good luck.

eyal karni
  • 1,106
  • 9
  • 33
0

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.

D. Ben Knoble
  • 26,070
  • 3
  • 29
  • 65