I'm using ctrlpvim/ctrlp.vim to open files by fuzzy search.
Is there a way to get the full path of the searched file and insert it under cursor, instead of opening it?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityI'm using ctrlpvim/ctrlp.vim to open files by fuzzy search.
Is there a way to get the full path of the searched file and insert it under cursor, instead of opening it?
You can customize the function handling the opening of files, as explained at :help 'g:ctrlp_open_func'
.
You could try something similar to this:
function! CtrlPOpenFunc(action, line)
if a:action =~ '^h$'
" Get the filename
let filename = fnameescape(fnamemodify(a:line, ':p'))
" Close CtrlP
call ctrlp#exit()
call ctrlp#mrufiles#add(filename)
" insert the contents of filename into the buffer
put =filename
else
" Use CtrlP's default file opening function
call call('ctrlp#acceptfile', [a:action, a:line])
endif
endfunction
let g:ctrlp_open_func = {
\ 'files': 'CtrlPOpenFunc',
\ 'mru files': 'CtrlPOpenFunc'
\ }
, in order to insert the file path by pressing Ctrl-x in the Ctrlp window.
This is really hacky, but it worked for me, if you source this mapping
nnoremap <Leader>pf :let @p=expand("%")<CR>:tabclose<CR>"pp
then
<Leader>pf
the mapping will assign the file that Ctrl-p opened to the p
register, close the tab and paste the p
register where your cursor was.