Vim Tips Wiki
m (Use functionality similar to the * search on multiple files moved to Search for current word in multiple files: Page moved by JohnBot to improve title)
(mark as duplicate)
 
(13 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{review}}
+
{{Duplicate|1543}}
  +
{{TipImported
{{Tip
 
 
|id=123
 
|id=123
  +
|previous=122
|title=use functionality similar to the * search on multiple files
 
  +
|next=124
|created=September 28, 2001 17:22
 
  +
|created=2001
 
|complexity=basic
 
|complexity=basic
|author=* type grep
+
|author=
 
|version=6.0
 
|version=6.0
 
|rating=17/8
 
|rating=17/8
  +
|category1=Searching
|text=
 
  +
|category2=
The use of star as in [[VimTip1]] and [[VimTip5]] is great, here is how to use this type of search accross a
 
 
}}
  +
It is easy to search for the word under the cursor using the [[Searching|super star]] (<code>*</code>). Here is how to search all files in a directory.
   
  +
Enter the command <code>:set grepprg?</code> to determine what program is used on your system to execute the <code>:grep</code> command. If it is the <code>grep</code> utility, the following mappings allow searching files in a directory for the word under the cursor.
whole directory:
 
  +
<pre>
  +
: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>
  +
</pre>
   
 
The first mapping searches for the text in the word under the cursor (like <code>g*</code>) in any of the files in the current directory.
Just add the mappings (or choose different letter combinations):
 
   
 
The second mapping searches for the text in the word under the cursor (like <code>g*</code>) in any of the files in the same directory as the current file.
map gr :grep &lt;cword&gt; *&lt;cr&gt;
 
   
 
The third mapping searches for the whole word under the cursor (like <code>*</code>) in any of the files in the current directory.
map gr :grep &lt;cword&gt; %:p:h/*&lt;cr&gt;
 
   
 
The fourth mapping searches for the whole word under the cursor (like <code>*</code>) in any of the files in the same directory as the current file.
map gR :grep \b&lt;cword&gt;\b *&lt;cr&gt;
 
   
  +
On a Windows system, <code>'grepprg'</code> may be set to use the <code>findstr</code> utility. In that case, the last two mappings should be:
map GR :grep \b&lt;cword&gt;\b %:p:h/*&lt;cr&gt;
 
  +
<pre>
  +
:nnoremap gR :grep "\<<cword>\>" *<CR>
  +
:nnoremap GR :grep "\<<cword>\>" %:p:h/*<CR>
  +
</pre>
   
  +
==See also==
  +
*[[Find in files within Vim]]
   
 
==Comments==
   
  +
It would be nice if this tip also metion how to browse through the returned locations.
 
  +
:See the [[Find in files within Vim]] tip. There's not really much here that isn't covered there. --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 17:30, November 27, 2012 (UTC)
 
mapping one will search for the word under the cursor (like g*) in any of the files in the current directory
 
 
mapping two will search for the word under the cursor (like g*) in any of the files in the same directory as the current file
 
 
mapping three will search for the word under the cursor by itself (i.e. surrounded by word boundary like *) in any of the files in the current directory
 
 
mapping four will search for the word under the cursor by itself (i.e. surrounded by word boundary like *) in any of the files in the same directory as the current file
 
 
 
 
Benoit
 
 
 
}}
 
 
== Comments ==
 
<!-- parsed by vimtips.py in 0.479748 seconds-->
 

Latest revision as of 17:30, 27 November 2012

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

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[]

It would be nice if this tip also metion how to browse through the returned locations.

See the Find in files within Vim tip. There's not really much here that isn't covered there. --Fritzophrenic (talk) 17:30, November 27, 2012 (UTC)