我想将 ag(银色搜索器)与 ctrlp 和 vim 一起使用。 我的 .vimrc 中有这个:
if executable("ag")
set grepprg=ag\ --nogroup\ --nocolor
let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""'
endif
let g:ctrlp_show_hidden = 1
set wildignore+=*/tmp/*,*.so,*.swp,*.zip,*/.tmp/*,*/.sass-cache/*,*/node_modules/*,*.keep,*.DS_Store,*/.git/*
我希望 ctrlp 包含隐藏文件,但这些文件是隐藏的。如果我将 -u
添加到 ag 命令中,它会显示所有隐藏文件,但不尊重 wildignore 或 .gitignore。有可能让它尊重这些吗?
最佳答案
如果您通过 ctrlp_user_command
几个选项使用自定义查找器,包括 ctrlp_show_hidden
ctrlp_custom_ignore
和 vim 的 wildignore
模式,不被 CtrlP ( see documentation ) 使用。
g:ctrlp_show_hidden
...
Note: does not apply when a command defined with |g:ctrlp_user_command| is being used.
g:ctrlp_custom_ignore
...
Note #1: by default, |wildignore| and |g:ctrlp_custom_ignore| only apply when |globpath()| is used to scan for files, thus these options do not apply when a command defined with |g:ctrlp_user_command| is being used.
因此,您只能受搜索工具(在本例中为 ag)的支配。幸运的是,您可以做一些事情来获得您想要的行为。
要显示隐藏的点文件,但仍尊重忽略
文件,请使用ag的--hidden
选项:
let g:ctrlp_user_command = 'ag %s -l --nocolor --hidden -g ""'
现在要定义要忽略的模式,您可以使用ag自己的忽略文件 .agignore 。这可以是每个目录或全局目录,ag 将在每次运行时检查。您将其放置在您的主目录 ~/.agignore
中。
我知道让 vims wildignore
处理模式会很好,但是使用 .agignore
当你从 cli 使用 ag 时,你会得到这些限制的好处。如果您想搜索所有文件,只需使用您提到的ag -u
命令来绕过任何ignore
文件。
作为最后的花絮,您可以使用一种字典格式来定义 g:ctrlp_user_command
,其中包含一个 ignore
键,该键将使 CtrlP 使用 wildignore
模式。但是,我从未尝试过此操作,并且文档指出了潜在的性能影响。如果您不喜欢我提出的其他解决方案 ( see documentation ),您可以尝试此方法。
Note #3: unless the |Dictionary| format is used and 'ignore' is defined and set to 1, the |wildignore| and |g:ctrlp_custom_ignore| options do not apply when these custom commands are being used. When not present, 'ignore' is set to 0 by default to retain the performance advantage of using external commands.
关于vim - 在ctrlp + vim中使用ag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18285751/