vimtricks.wiki Concise Vim tricks, one at a time.

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 n to jump to the next match, or N to 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 \c anywhere in the pattern)
  • /pattern\C — case-sensitive search (append \C)
  • /\<word\> — match the whole word only (word boundaries)
  • /pattern wraps around the end of the file back to the top by default (controlled by the wrapscan option)

Tips

  • Use ?pattern to 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 hlsearch to highlight all matches, and :noh to clear the highlighting
  • Use :set incsearch to see matches incrementally as you type the pattern
  • The search pattern supports regular expressions by default — use \V prefix for literal ("very nomagic") matching
  • Your search history is saved — press / then <Up> to recall previous searches

Next

How do I edit multiple lines at once using multiple cursors in Vim?