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.

0

3 Answers 3

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.

1
  • If you change ":p" to ":f" it will give you the relative path
    – joshpetit
    Commented Mar 24, 2021 at 15:07
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.

3
  • See :h fzf ................
    – eyal karni
    Commented Aug 18, 2019 at 16:31
  • 1
    Instead of clobbering the z register, do put =a:item
    – D. Ben Knoble
    Commented Aug 18, 2019 at 16:44
  • It is a bit different since put works linewise.
    – eyal karni
    Commented Aug 20, 2019 at 11:42
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.

2
  • 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
    Commented Mar 20, 2019 at 10:32
  • @Andreas see edita
    – D. Ben Knoble
    Commented Mar 20, 2019 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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