Search code examples
vimfindinfiles

Vim: Search in Files Short-cut Key



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


Solution

  • 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