How do I search for text in Vim?
Answer
/pattern
Explanation
The /pattern command searches forward through the file for the given pattern. It is the most fundamental search command in Vim and one of the first things every user should learn.
How it works
- Press
/to enter search mode — the cursor moves to the command line at the bottom of the screen - Type your search pattern and press
<CR>(Enter) - Vim jumps to the next occurrence of the pattern
- Press
nto jump to the next match, orNto jump to the previous match
Example
Given the text:
The quick brown fox jumps over the lazy dog.
The fox is quick.
Typing /fox<CR> jumps the cursor to the first fox on line 1. Pressing n moves to the fox on line 2. Pressing N jumps back to the first match.
Search options
/pattern\c— case-insensitive search (append\canywhere in the pattern)/pattern\C— case-sensitive search (append\C)/\<word\>— match the whole word only (word boundaries)/patternwraps around the end of the file back to the top by default (controlled by thewrapscanoption)
Tips
- Use
?patternto search backward instead of forward - Use
*to instantly search forward for the word under the cursor - Use
#to instantly search backward for the word under the cursor - Use
:set hlsearchto highlight all matches, and:nohto clear the highlighting - Use
:set incsearchto see matches incrementally as you type the pattern - The search pattern supports regular expressions by default — use
\Vprefix for literal ("very nomagic") matching - Your search history is saved — press
/then<Up>to recall previous searches