开发者

Vim: Search in Files Short-cut Key

开发者 https://www.devze.com 2023-01-24 19:24 出处:网络
I use vimg开发者_Go百科rep (or grep) to search in files inside vim, but I see it\'s quite inconvenient to have to write that every time I want to make search in vim. Any idea on how to make a suitabl

I use vimg开发者_Go百科rep (or grep) to search in files inside vim, but I see it's quite inconvenient to have to write that every time I want to make search in vim. Any idea on how to make a suitable short-cut key to search in files?

Regards,

Rafid


The following will take you to the command-line, pre-populated with vimgrep if you press F3 in normal mode:

:nmap <F3> :vimgrep<space>

If you always wish to search the current directory, try:

:nmap <F3> :vimgrep // *<left><left><left>

If you want to save even more keypresses, try this to search the current directory for the word under the cursor:

:nmap <F3> :vimgrep /<C-R><C-W>/ *<CR>

Of course, these can be put into your .vimrc file.

Vim's inbuilt help system provides lots of useful information on this subject. See the following sections to explain what I have done:

  • :help :nmap
  • :help c_CTRL-R_CTRL-W


An alternative to Johnsyweb's solution:

set grepprg=ack
nnoremap <F2> :grep<space>
nnoremap <F3> :noautocmd vimgrep // **/*<c-f>$Bhhi
nnoremap <S-F3> :noautocmd vimgrep /<C-R>// **/*<return>
xnoremap <F3> :<c-u>let tmp=@y<cr>gv"yy:noautocmd vimgrep /\V<c-r>=substitute(substitute(@y,'\','\\','g'),'/','\/','g')<cr>/ **/*<return>:let @y=tmp<cr>:unlet tmp<cr>

This will map F2 to ack use Perl regexps), F3 to vimgrep with no autocmds triggered (a lot faster), shift-f3 to a vimgrep of the current search pattern, and F3 in visual mode to a vimgrep of the current highlighted text.


Not exactly elegant but I use these command-line maps (because I've already consumed all of the function keys with other commands):

cmap vvv vimgrep // **/*.*<Left><Left><Left><Left><Left><Left><Left><Left>
cmap vvs vimgrep // **/*.sas<Left><Left><Left><Left><Left><Left><Left><Left><Left><Left>
0

精彩评论

暂无评论...
验证码 换一张
取 消