Vim language: send current word to CtrlP
Asked Answered
L

4

6

I know how to use CtrlP. I type ctrl+p, then I start to write file name, ... and so on. But, ... I am very lazy developer. I want to directly send to CtrlP current word. I know how to get current word:

let l:currentWord = expand('<cword>')

In Vim Language, ... I How can I send l:currentWord to CtrlP?

map <F6>  :call ComposerKnowWhereCurrentFileIs()<CR>
function! ComposerKnowWhereCurrentFileIs()
    let l:currentWord = expand('<cword>')
    let l:command = "grep " . l:currentWord . " ../path/to/composer -R | awk '{print $6}' | awk -F\\' '{print $2}'"
    let l:commandFileFound = l:command . ' | wc -l'
    let l:numberOfResults = system(l:commandFileFound)
    if l:numberOfResults == 1
        let l:fileName = system(l:command)
        let l:openFileCommand = 'tabe /path/to/project' . l:fileName
        exec l:openFileCommand
    else
        echo "Too many files :-( - use CtrlP ;-) "
    endif
endfunction
Leveridge answered 22/5, 2015 at 7:0 Comment(4)
map <F6> <C-P><C-\>wScutari
Can I map inside a function?Leveridge
Why do you want to do this inside the function?Scutari
Because the function do a lot of things (removed in this question). In some cases I DONT need to call CtrlP: in that cases I ask to composer the path of file grepping current word. But is not the case of this question =)Leveridge
S
14
<C-P><C-\>w

See :h ctrlp-mappings. You may map this combination:

map <F6> <C-P><C-\>w

In a function:

exe "normal \<C-P>" . expand('<cword>')
Scutari answered 22/5, 2015 at 7:23 Comment(1)
I dont need remapping.Leveridge
F
4

The whole point of CtrlP and similar plugins is to provide an alternative command-line where you can refine your search as you type.

If you don't need fuzzy search and you already have the filename under the cursor… why not simply use the built-in gf?

-- edit --

In the gif below:

  • I jump to /path/not/knowable/BlaBlaClassName.php with gf,
  • I jump back to the previous buffer with <C-^> (unrelated to your question),
  • I jump to the declaration of BlaBlaClassName in /path/not/knowable/BlaBlaClassName.php again with <C-]> thanks to a tagsfile generated with ctags.

gf

Freelance answered 22/5, 2015 at 7:47 Comment(6)
Well, ... if I am on BlaBlaClassName, how can gf open a file placed in /path/not/knowable/SomeFileName.php?Leveridge
Aniway: the question is "send current word to CtrlP in VimL".Leveridge
If you feed BlaBlaClassName to CtrlP, the chances of it suggesting a totally unrelated /path/not/knowable/SomeFileName.php are pretty slim. But, with the correct settings, gf on BlaBlaClassName will open /path/not/knowable/BlaBlaClassName.php instantly. See my edit. That said, if you have a foo class in a bar file, you have more important problems than fiddling with CtrlP.Freelance
Then use ctags. CtrlP is completely useless for your use case.Freelance
Too big project. Composer knows where files are.Leveridge
Using ctags/cscope/GNU global/phpctags is the correct way to handle this situation. You may want to look into Effortless Ctags with Git or a plugin like Gutentags.Doubleedged
F
3
function! LazyP()
  let g:ctrlp_default_input = expand('<cword>')
  CtrlP
  let g:ctrlp_default_input = ''
endfunction
command! LazyP call LazyP()
nnoremap <C-P> :LazyP<CR>

(this could probably be simplified but I suck at vim syntax)

Fixture answered 22/5, 2015 at 7:40 Comment(2)
I tried that snippet in my .vimrc before posting it here, and it does the job.Hoatzin
Ok, I have not remapped <c-p> in CtrlPLeveridge
F
1

For that, you wouldn't use the <C-P> mapping, but the :CtrlP command, as that one takes parameters.

To build a mapping that passes the current word to the command, there are two approaches. Either directly insert the current word into the command-line (via :help c_CTRL-R_CTRL-W):

:nnoremap <Leader>p :CtrlP <C-r><C-p><CR>

Or, in order to use expand(), build the Ex command via :execute:

:nnoremap <Leader>p :execute 'CtrlP' expand('<cword>')<CR>
Faction answered 22/5, 2015 at 7:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.