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
sensorario
  • 20,262
  • 30
  • 97
  • 159

4 Answers4

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>')
lollo
  • 2,289
  • 16
  • 20
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

romainl
  • 186,200
  • 21
  • 280
  • 313
  • Well, ... if I am on BlaBlaClassName, how can gf open a file placed in /path/not/knowable/SomeFileName.php? – sensorario May 22 '15 at 07:58
  • Aniway: the question is "send current word to CtrlP in VimL". – sensorario May 22 '15 at 07:58
  • 2
    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. – romainl May 22 '15 at 08:19
  • Then use ctags. CtrlP is completely useless for your use case. – romainl May 22 '15 at 08:39
  • Too big project. Composer knows where files are. – sensorario May 22 '15 at 08:39
  • 1
    Using ctags/cscope/GNU global/phpctags is the correct way to handle this situation. You may want to look into [Effortless Ctags with Git](http://tbaggery.com/2011/08/08/effortless-ctags-with-git.html) or a plugin like [Gutentags](https://github.com/ludovicchabant/vim-gutentags). – Peter Rincker May 22 '15 at 17:08
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)

Régis B.
  • 10,092
  • 6
  • 54
  • 90
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>
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324