之前用的是 ctrlp ,现在改用 leaderf ,配置更简单(主要是没搞定 ctrlp 过滤指定文件和文件夹),功能也更多一点。
安装和配置
Plug 'Yggdroot/LeaderF', { 'do': './install.sh' }
let g:Lf_ShortcutF = '<c-p>'
nmap <unique> <leader>fr <Plug>LeaderfRgPrompt
nmap <unique> <leader>fra <Plug>LeaderfRgCwordLiteralNoBoundary
nmap <unique> <leader>frb <Plug>LeaderfRgCwordLiteralBoundary
nmap <unique> <leader>frc <Plug>LeaderfRgCwordRegexNoBoundary
nmap <unique> <leader>frd <Plug>LeaderfRgCwordRegexBoundary
vmap <unique> <leader>fra <Plug>LeaderfRgVisualLiteralNoBoundary
vmap <unique> <leader>frb <Plug>LeaderfRgVisualLiteralBoundary
vmap <unique> <leader>frc <Plug>LeaderfRgVisualRegexNoBoundary
vmap <unique> <leader>frd <Plug>LeaderfRgVisualRegexBoundary
let g:Lf_WorkingDirectoryMode = 'c'
let g:Lf_UseVersionControlTool = 0
let g:Lf_DefaultExternalTool = "rg"
let g:Lf_ExternalCommand = 'rg --files "%s"'
let g:Lf_WildIgnore={ 'file':['*.lib', '*.a', '*.o', '*.d', '*.so', ],'dir':['tmp', '.git', 'api', 'attachments', 'images', 'img', 'download', ]}
在用之前还需要先安装rg
:
sudo apt install -y ripgrep
配置有几个要点,首先是定义一些快捷键,最常用的是ctrl+p
快速调用搜索文件框,然后是,fr
搜索文件内容。
然后是Lf_WorkingDirectoryMode
一定要设置为c
,即搜索当前工作路径下的文件。后面两个选项设置使得工作路径下如果有git
子模块,也会被搜索(默认将被忽略,很诡异的默认设置)。
上面设置 leaderf 使用 rg 来搜索文件和文件内容,默认将根据.gitignore
忽略指定文件夹内容。如果不想使用.gitignore
,可设置-no-ignore-vsc
:
let g:Lf_ExternalCommand = 'rg --files "%s" --no-ignore-vcs'
然后在目录下创建.ignore
文件,把要忽略的内容写到该文件下。
附 Lf_WorkingDirectoryMode 的选项说明:
g:Lf_WorkingDirectoryMode *g:Lf_WorkingDirectoryMode*
This option customizes LeaderF's working directory.
e.g., >
let g:Lf_WorkingDirectoryMode = 'Ac'
<
c - the directory of the current working directory.(default)
a - the nearest ancestor of current working directory that contains one of
directories or files defined in |g:Lf_RootMarkers|. Fall back to 'c' if
no such ancestor directory found.
A - the nearest ancestor of current file that contains one of directories
or files defined in |g:Lf_RootMarkers|. Fall back to 'c' if no such
ancestor directory found.
f - the directory of the current file.
F - if the current working directory is not the direct ancestor of current
file, use the directory of the current file as LeaderF's working
directory, otherwise, same as 'c'.
Note: if "f", "F" is included with "a" or "A", use the behavior of "f" or
"F"(as a fallback) when a root can't be found.
Q. E. D.