How to yank a CtrlP match result in vim
Asked Answered
H

2

6

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...

Hewe answered 3/9, 2016 at 10:38 Comment(2)
Please give with an example.Garotte
Interesting. Maybe it would be possible to somehow exploit g:ctrlp_open_func and grab the match to a register instead of opening it, but it's just a guess...Gangrene
I
0

(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.

Interoceptor answered 17/8, 2018 at 1:0 Comment(0)
P
0

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' }
                                                                                                       
Pronucleus answered 4/8, 2021 at 15:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.