6

我知道如何使用 CtrlP。我输入 ctrl+p,然后我开始写文件名,……等等。但是,...我是一个非常懒惰的开发者。我想直接发送到 CtrlP 当前单词。我知道如何获取当前单词:

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

在 Vim 语言中,... 我如何将 l:currentWord 发送到 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
4

4 回答 4

14
<C-P><C-\>w

:h ctrlp-mappings。您可以映射此组合:

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

在一个函数中:

exe "normal \<C-P>" . expand('<cword>')
于 2015-05-22T07:23:31.470 回答
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>

(这可能会被简化,但我很讨厌 vim 语法)

于 2015-05-22T07:40:16.283 回答
3

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

于 2015-05-22T07:47:21.677 回答
1

为此,您不会使用<C-P>映射,而是使用:CtrlP命令,因为该命令需要参数。

要构建将当前单词传递给命令的映射,有两种方法。直接将当前单词插入命令行(通过:help c_CTRL-R_CTRL-W):

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

或者,为了使用expand(),通过以下方式构建 Ex 命令:execute

:nnoremap <Leader>p :execute 'CtrlP' expand('<cword>')<CR>
于 2015-05-22T07:24:29.303 回答