5


I use vimgrep (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

3 Answers 3

8

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
4
  • Are you sure the first one is correct? vimgrep expects a file name and this was basically my problem. I know how to make mapping, but the thing is how to put the cursor in the middle. For example, if I want to search for hello in all files, I would write: ":vimgrep /hello/ *". So how is it possible in vim to make a mapping that writes the above command, yet put me between the two slashes?
    – Rafid
    Nov 14, 2010 at 7:26
  • Yes I am sure it is correct, it leaves an incomplete commandline. I couldn't predict which directories you wanted to search, though. Shall add to my answer.
    – johnsyweb
    Nov 14, 2010 at 7:39
  • 1
    Thanks, it works now. I should have thought of the <left> before asking :-)
    – Rafid
    Nov 14, 2010 at 8:15
  • Add a double star to make it recursive :nmap <F3> :vimgrep /<C-R><C-W>/ **<CR> Jul 21, 2020 at 8:25
2

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.

5
  • 1
    Good alternative. ack is very neat, too.
    – johnsyweb
    Nov 14, 2010 at 8:12
  • 1
    Thanks for that. What does $Bhhi above mean?
    – Rafid
    Nov 14, 2010 at 8:18
  • 2
    @Rafid: Try it (preceded by CTRL+F) on the command line. It's a neat trick for editing the command-line.
    – johnsyweb
    Nov 14, 2010 at 8:26
  • Thanks for this. It is really nice. I am wondering how long, from the theoretical point of view, it takes one to know all the commands and key combinations of vim! Probably there are tricks that even Vim's creators don't know!
    – Rafid
    Nov 14, 2010 at 8:43
  • 1
    Actually, that <ctrl-f> thing is controlled by the 'cedit' setting (:help 'cedit')
    – Benoit
    Nov 14, 2010 at 9:51
2

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>
2
  • 1
    Hello @rshdev, maybe you might like to see :help 'cedit' to avoid all those <left>s (see second mapping from my answer and do not consider syntax highlighting).
    – Benoit
    Nov 14, 2010 at 21:04
  • Thanks @Benoit nice alternative
    – rshdev
    Nov 14, 2010 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.