chriad
chriad

Reputation: 1412

How to yank a CtrlP match result in vim

Is it possible to copy a search result form the ctrlP match window? With <s-tab> one can focus the match window but it doesn't seem posible to yank from there.

I often have to search for files that I need in my scripts, but I only remember their names vaguely. So I use ctrlP to find the file. No I'd like to paste the matching file name to my script. I cannot find anything like this in the documentation of ctrlP!

enter image description here

For example in this case I want to copy the path the cursor is on (in the match window at the bottom) to a register and then paste it into the above window...

Upvotes: 6

Views: 482

Answers (2)

Jin
Jin

Reputation: 1

I has the same question. And follow ~guessimtoolate suggestion. I come up with this custom function for CtrlP. Press Ctrl-t over the file will append it's filename at the cursor.

function! YankFilenameFunc(action, line)
  if a:action =~ '^[t]$' 
    " Get the filename
    let filename = fnameescape(fnamemodify(a:line, ':t'))
    " Close CtrlP
    call ctrlp#exit()
    exec "normal a". filename ."\<Esc>"
  else
    " Use CtrlP's default file opening function
    call call('ctrlp#acceptfile', [a:action, a:line])
  endif
endfunction
let g:ctrlp_open_func = { 'files': 'YankFilenameFunc' }
                                                                                                       

Upvotes: 0

WW00WW
WW00WW

Reputation: 437

(Notice: This is not an advertisement!)

I don't use CtrlP so I have no idea to do it with CtrlP.

Now I use LeaderF for fuzzy search and I think it may solve your problem.

With LeaderF, once the search window is open, you can press Tab to switch to normal mode. And then you may use, for example, yy to yank a filename and paste it to the above window.

Hope this helps.

Upvotes: 0

Related Questions