6

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?

2 Answers 2

7

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.

0
2

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

  1. put your cursor where you want the filename to be put
  2. use Ctrl + p, as normal to find your file, then open the file in a new tab
    (open a file in a new tab from inside Ctrl-p is Ctrl + t)
  3. run my leader mapping <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.

0

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.