VIM 搜索功能增强(插件)

发现当工程比较大,文件比较多的时候。VIM内置的vimgrep比较卡比较慢。搜索和试用了一通。发现 The Silver Searcher 性能和星星的数量都不错。

The Silver Searcher

安装 The Silver Searcher

1
sudo apt-get install silversearcher-ag

.vimrc 或者 .init 加入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
" The Silver Searcher 替换grep成Ag
if executable('ag')
" Use ag over grep
set grepprg=ag\ --nogroup\ --nocolor

" Use ag in CtrlP for listing files. Lightning fast and respects .gitignore
let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""'

" ag is fast enough that CtrlP doesn't need to cache
let g:ctrlp_use_caching = 0
endif

" bind K to grep word under cursor 绑定大写K去搜索光标所指的关键词
nnoremap K :grep! "\b<C-R><C-W>\b"<CR>:cw<CR>


" bind \ (backward slash) to grep shortcut
command -nargs=+ -complete=file -bar Ag silent! grep! <args>|cwindow|redraw!

搜索

  • 输入关键字搜索
1
2
3
# VIM 输入下面的去搜索
:Ag getOSS ./
# :Ag 关键字 路径(./是当前目录下)

VIM with the Silver Searcher

  • 光标指定搜索(光标在词上时候点大写K)

K指定搜索词

参考

https://robots.thoughtbot.com/faster-grepping-in-vim