Vim Tips Wiki
(refactor as an introduction; merge in idea for smartcase * from 200908; move "Highlight matches" to VimTip14)
Line 1: Line 1:
__NOTOC__
 
 
{{TipImported
 
{{TipImported
 
|id=1
 
|id=1
 
|previous=
 
|previous=
 
|next=2
 
|next=2
|created=February 24, 2001
+
|created=2001
 
|complexity=basic
 
|complexity=basic
|author=Scott+Yegappan
+
|author=
|version=5.7
+
|version=6.0
 
|rating=5333/1765
 
|rating=5333/1765
 
|category1=Searching
 
|category1=Searching
 
|category2=
 
|category2=
 
}}
 
}}
  +
This tip shows how to search using Vim. The [[#See also|see also]] section links to other useful searching tips.
In normal mode, move the cursor to any word. How do you search for the next occurrence of that word?
 
   
  +
==Basic searching==
Press the <tt>*</tt> key to search forwards for the current word, or press <tt>#</tt> to search backwards.
 
  +
In normal mode you can search forwards by pressing <tt>/</tt> then typing your search pattern. Press Esc to cancel or press Enter to perform the search. Then press <tt>n</tt> to search forwards for the next occurrence, or <tt>N</tt> to search backwards.
  +
  +
Search backwards by pressing <tt>?</tt> then typing your search pattern. Pressing <tt>n</tt> searches in the same direction (backwards), while <tt>N</tt> searches in the opposite direction (forwards).
  +
  +
==Searching for the current word==
 
In normal mode, move the cursor to any word. Press <tt>*</tt> to search forwards for the next occurrence of that word, or press <tt>#</tt> to search backwards.
   
 
Using <tt>*</tt> or <tt>#</tt> searches for the exact word at the cursor (searching for ''rain'' would not find ''rainbow'').
 
Using <tt>*</tt> or <tt>#</tt> searches for the exact word at the cursor (searching for ''rain'' would not find ''rainbow'').
Line 21: Line 26:
   
 
===Using the mouse===
 
===Using the mouse===
With the proper settings, you can search for an exact word using the mouse.
+
With appropriate settings, you can search for an exact word using the mouse: Shift-LeftClick a word to search forwards, or Shift-RightClick to search backwards.
 
Shift-LeftClick a word to search forwards, or Shift-RightClick to search backwards.
 
   
 
This needs a GUI version of Vim (gvim), or a console Vim that accepts a mouse. You may need the following line in your [[vimrc]] to enable mouse searches:
 
This needs a GUI version of Vim (gvim), or a console Vim that accepts a mouse. You may need the following line in your [[vimrc]] to enable mouse searches:
Line 32: Line 35:
 
In gvim, click the Edit menu, then Global Settings, then the "tear off" bar at the top. That will show a floating Global Settings menu with useful ''Toggle Pattern Highlight'' and ''Toggle Ignore-case'' commands.
 
In gvim, click the Edit menu, then Global Settings, then the "tear off" bar at the top. That will show a floating Global Settings menu with useful ''Toggle Pattern Highlight'' and ''Toggle Ignore-case'' commands.
   
===More searching===
+
==More searching==
After searching, press <tt>n</tt> for the next match, or <tt>N</tt> to search in the reverse direction.
 
 
 
Vim maintains a [[VimTip45|search history]]. Type <tt>/</tt> or <tt>?</tt> and use the arrow up/down keys to recall previous search patterns. You can edit a pattern, and press Enter to search for something different.
 
Vim maintains a [[VimTip45|search history]]. Type <tt>/</tt> or <tt>?</tt> and use the arrow up/down keys to recall previous search patterns. You can edit a pattern, and press Enter to search for something different.
   
 
Suppose the cursor is on a word, and you want to search for a similar word.
 
Suppose the cursor is on a word, and you want to search for a similar word.
   
Press <tt>/</tt> then <C-r><C-w> to copy the current word to the command line. You can now edit the search pattern and press Enter. Use <C-r><C-w> for a <cword>, or <C-r><C-a> for a <cWORD>.
+
Press <tt>/</tt> then Ctrl-r then Ctrl-w to copy the current word to the command line. You can now edit the search pattern and press Enter. Use Ctrl-r Ctrl-w for a <cword>, or Ctrl-r Ctrl-a for a <cWORD>.
   
 
After searching, press Ctrl-o to [[VimTip10|jump back]] to your previous position (then Ctrl-i will jump forwards).
<C-r><C-w> is Control-R then Control-W (hold down <tt>Ctrl</tt> and press <tt>r</tt> then <tt>w</tt>).
 
 
After searching, use <C-o> to jump back to your previous position (then <C-i> will jump forwards).
 
   
 
After searching, an empty search pattern will repeat the last search. This works with <tt>/</tt>, <tt>:s</tt> and <tt>:g</tt>.
 
After searching, an empty search pattern will repeat the last search. This works with <tt>/</tt>, <tt>:s</tt> and <tt>:g</tt>.
   
So, after searching for a word, use <tt>:%s//new/g</tt> to change all occurrences to 'new', or <tt>:g/</tt> to list all lines containing the word. See [[VimTip1501|Substitute last search]].
+
So, after searching for a word, use <tt>:%s//new/g</tt> to change all occurrences to 'new', or <tt>:g/</tt> to list all lines containing the word. See [[VimTip1501|substitute last search]].
   
 
You can enter a count before a search. For example <tt>3/pattern</tt> will search for the third occurrence of ''pattern'', and <tt>3*</tt> will search for the third occurrence of the current word.
 
You can enter a count before a search. For example <tt>3/pattern</tt> will search for the third occurrence of ''pattern'', and <tt>3*</tt> will search for the third occurrence of the current word.
   
  +
You can highlight all search matches (and quickly turn highlighting off), and you can use a mapping to highlight all occurrences of the current word without moving (see [[VimTip14|here]]).
====Highlight matches without moving====
 
It can be useful to highlight the word under the cursor like <tt>*</tt>, but ''without'' jumping to the next match. Then you can see the search highlights on the current screen, without any scrolling. Move to the first (<tt>ggn</tt>), last (<tt>GN</tt>), next (<tt>n</tt>) or previous (<tt>N</tt>) match as usual.
 
   
  +
==Case sensitivity==
The basic command is (type Ctrl-r then Ctrl-w to insert the current word):
 
  +
By default, searching is case sensitive (searching for "the" will not find "The").
  +
  +
With the following in your vimrc (or entered via a [[VimTip920|toggle mapping]]), searching is not case sensitive:
 
<pre>
 
<pre>
  +
set ignorecase
:let @/="<C-r><C-w>"<CR>
 
 
</pre>
 
</pre>
   
  +
Now the command <tt>/the</tt> will find "the" or "The" or "THE" etc. You can use <tt>\c</tt> to force a pattern to be case insensitive, or <tt>\C</tt> to force a pattern to be case sensitive. For example, the search <tt>/the\c</tt> is always case insensitive, and <tt>/the\C</tt> is always case sensitive, regardless of the 'ignorecase' option.
The following map uses F10 to highlight all occurrences of the current word, and F11 to unhighlight (put in your vimrc):
 
  +
<pre>
 
  +
If <tt>'ignorecase'</tt> is on, you may also want:
nnoremap <F10> :set hls<CR>:exec "let @/='\\<".expand("<cword>")."\\>'"<CR>
 
nnoremap <F11> :nohls<CR>
 
</pre>
 
It is functionally the same as doing <C-r><C-w> manually, but using "<cword>" avoids errors if the cursor is not over anything.
 
Here is another version that uses F10 to toggle search highlighting (F11 is not used).
 
 
<pre>
 
<pre>
  +
set smartcase
nnoremap <silent> <F10> :set invhls<CR>:exec "let @/='\\<".expand("<cword>")."\\>'"<CR>
 
 
</pre>
 
</pre>
   
  +
When <tt>'ignorecase'</tt> and <tt>'smartcase'</tt> are both on, if a pattern contains an uppercase letter, it is case sensitive, otherwise, it is not. For example, <tt>/The</tt> would find only "The", while <tt>/the</tt> would find "the" or "The" etc.
The 'inv' prefix on a boolean setting toggles it. <silent> doesn't echo the command on the command line
 
   
  +
The <tt>'smartcase'</tt> option only applies to search patterns that you type; it does not apply to <tt>*</tt> or <tt>#</tt> or <tt>gd</tt>. If you press <tt>*</tt> to search for a word, you can make <tt>'smartcase'</tt> apply by pressing <tt>/</tt> then up arrow then Enter (to repeat the search from history).
Position the cursor on an interesting word, then press F10 to highlight all occurrences of that word. Use commands like <tt>n</tt> and <tt>N</tt> to search up and down. When you're done, press F10 again to toggle highlighting off.
 
   
  +
When programming, there is generally no reason to want <tt>'smartcase'</tt> to apply when you press <tt>*</tt>, however, you can achieve this with these mappings:
Here is how to do the same for visually selected text:
 
 
<pre>
 
<pre>
 
nnoremap * /\<<C-R><C-W>\><CR>
function! <SID>FidgetWhitespace(pat)
 
 
nnoremap # #\<<C-R><C-W>\><CR>
let pat = substitute(a:pat,'\_s\+$','\\s\\*', '')
 
let pat = substitute(pat, '^\_s\+', '\\s\\*', '')
 
return substitute(pat, '\_s\+', '\\_s\\+','g')
 
endfunction
 
vmap <silent><M-8> :<C-U>let @/="\\V<C-R>=escape(<SID>FidgetWhitespace(escape(@*,'\')),'\"')<CR>"<CR>
 
 
</pre>
 
</pre>
   
  +
Using these mappings, if <tt>'smartcase'</tt> is on and you press <tt>*</tt> while on the word "The", you will only find "The" (case sensitive), but if you press <tt>*</tt> while on the word "the", the search will not be case sensitive.
Replace <M-8> with the key of your choice. The function FidgetWhitespace() allows changes in whitespace for potential matches, so using the mapping while "foo bar" is selected will also highlight:
 
<pre>
 
"foo bar" and "foo
 
bar"
 
</pre>
 
If you want to keep your visual selection, append 'gv' to that mapping.
 
   
====Show the next match while entering a search====
+
==Show the next match while entering a search==
 
To move the cursor to the matched string, while typing the search pattern, set the following option in your [[vimrc]]:
 
To move the cursor to the matched string, while typing the search pattern, set the following option in your [[vimrc]]:
 
<pre>
 
<pre>
Line 97: Line 85:
 
</pre>
 
</pre>
   
Complete the search by pressing Enter, or cancel the search by pressing Escape. When typing the search pattern, press Ctrl-L ({{help|c_CTRL-L}}) to insert the next character from the match or press Ctrl-R Ctrl-W ({{help|c_CTRL-R_CTRL-F}}) to complete the current matching word.
+
Complete the search by pressing Enter, or cancel by pressing Esc. When typing the search pattern, press Ctrl-L ({{help|c_CTRL-L}}) to insert the next character from the match or press Ctrl-R Ctrl-W ({{help|c_CTRL-R_CTRL-F}}) to complete the current matching word.
   
 
==See also==
 
==See also==

Revision as of 04:24, 16 December 2009

Tip 1 Printable Monobook Next

created 2001 · complexity basic · version 6.0


This tip shows how to search using Vim. The see also section links to other useful searching tips.

Basic searching

In normal mode you can search forwards by pressing / then typing your search pattern. Press Esc to cancel or press Enter to perform the search. Then press n to search forwards for the next occurrence, or N to search backwards.

Search backwards by pressing ? then typing your search pattern. Pressing n searches in the same direction (backwards), while N searches in the opposite direction (forwards).

Searching for the current word

In normal mode, move the cursor to any word. Press * to search forwards for the next occurrence of that word, or press # to search backwards.

Using * or # searches for the exact word at the cursor (searching for rain would not find rainbow).

Use g* or g# if you don't want to search for the exact word.

Using the mouse

With appropriate settings, you can search for an exact word using the mouse: Shift-LeftClick a word to search forwards, or Shift-RightClick to search backwards.

This needs a GUI version of Vim (gvim), or a console Vim that accepts a mouse. You may need the following line in your vimrc to enable mouse searches:

:set mousemodel=extend

In gvim, click the Edit menu, then Global Settings, then the "tear off" bar at the top. That will show a floating Global Settings menu with useful Toggle Pattern Highlight and Toggle Ignore-case commands.

More searching

Vim maintains a search history. Type / or ? and use the arrow up/down keys to recall previous search patterns. You can edit a pattern, and press Enter to search for something different.

Suppose the cursor is on a word, and you want to search for a similar word.

Press / then Ctrl-r then Ctrl-w to copy the current word to the command line. You can now edit the search pattern and press Enter. Use Ctrl-r Ctrl-w for a <cword>, or Ctrl-r Ctrl-a for a <cWORD>.

After searching, press Ctrl-o to jump back to your previous position (then Ctrl-i will jump forwards).

After searching, an empty search pattern will repeat the last search. This works with /, :s and :g.

So, after searching for a word, use :%s//new/g to change all occurrences to 'new', or :g/ to list all lines containing the word. See substitute last search.

You can enter a count before a search. For example 3/pattern will search for the third occurrence of pattern, and 3* will search for the third occurrence of the current word.

You can highlight all search matches (and quickly turn highlighting off), and you can use a mapping to highlight all occurrences of the current word without moving (see here).

Case sensitivity

By default, searching is case sensitive (searching for "the" will not find "The").

With the following in your vimrc (or entered via a toggle mapping), searching is not case sensitive:

set ignorecase

Now the command /the will find "the" or "The" or "THE" etc. You can use \c to force a pattern to be case insensitive, or \C to force a pattern to be case sensitive. For example, the search /the\c is always case insensitive, and /the\C is always case sensitive, regardless of the 'ignorecase' option.

If 'ignorecase' is on, you may also want:

set smartcase

When 'ignorecase' and 'smartcase' are both on, if a pattern contains an uppercase letter, it is case sensitive, otherwise, it is not. For example, /The would find only "The", while /the would find "the" or "The" etc.

The 'smartcase' option only applies to search patterns that you type; it does not apply to * or # or gd. If you press * to search for a word, you can make 'smartcase' apply by pressing / then up arrow then Enter (to repeat the search from history).

When programming, there is generally no reason to want 'smartcase' to apply when you press *, however, you can achieve this with these mappings:

nnoremap * /\<<C-R><C-W>\><CR>
nnoremap # #\<<C-R><C-W>\><CR>

Using these mappings, if 'smartcase' is on and you press * while on the word "The", you will only find "The" (case sensitive), but if you press * while on the word "the", the search will not be case sensitive.

Show the next match while entering a search

To move the cursor to the matched string, while typing the search pattern, set the following option in your vimrc:

:set incsearch

Complete the search by pressing Enter, or cancel by pressing Esc. When typing the search pattern, press Ctrl-L (:help c_CTRL-L) to insert the next character from the match or press Ctrl-R Ctrl-W (:help c_CTRL-R_CTRL-F) to complete the current matching word.

See also

References

Comments

On UK keyboards, <Shift-3> produces ₤ but works just like # for searching backwards.