Wikia

Vim Tips Wiki

Search for current word in multiple files

Talk0
1,612pages on
this wiki
Revision as of 06:22, October 29, 2012 by JohnBeckett (Talk | contribs)

Tip 123 Printable Monobook Previous Next

created 2001 · complexity basic · version 6.0


It is easy to search for the word under the cursor using the super star (*). Here is how to search all files in a directory.

Enter the command :set grepprg? to determine what program is used on your system to execute the :grep command. If it is the grep utility, the following mappings allow searching files in a directory for the word under the cursor.

:nnoremap gr :grep <cword> *<CR>
:nnoremap Gr :grep <cword> %:p:h/*<CR>
:nnoremap gR :grep '\b<cword>\b' *<CR>
:nnoremap GR :grep '\b<cword>\b' %:p:h/*<CR>

The first mapping searches for the text in the word under the cursor (like g*) in any of the files in the current directory.

The second mapping searches for the text in the word under the cursor (like g*) in any of the files in the same directory as the current file.

The third mapping searches for the whole word under the cursor (like *) in any of the files in the current directory.

The fourth mapping searches for the whole word under the cursor (like *) in any of the files in the same directory as the current file.

On a Windows system, 'grepprg' may be set to use the findstr utility. In that case, the last two mappings should be:

:nnoremap gR :grep "\<<cword>\>" *<CR>
:nnoremap GR :grep "\<<cword>\>" %:p:h/*<CR>

See also

Comments

Around Wikia's network

Random Wiki