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.
3 Answers
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
.
-
If you change ":p" to ":f" it will give you the relative path Commented Mar 24, 2021 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
endfunction
So you just need to create a custom command and use PInsert as the sink argument.
Good luck.
-
-
1Instead of clobbering the z register, do
put =a:item
– D. Ben Knoble ♦Commented Aug 18, 2019 at 16:44 -
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.– AndreasCommented Mar 20, 2019 at 10:32
-