Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 36 ยท Registers, Macros and Search

Searching in Vim: Patterns, Offsets and Highlights

Search forward and backward with / and ?, repeat with n and N, use * and #, set ignorecase and smartcase, write Vim regex with \< \> and \v, and use offsets.

Searching is a motion in Vim, which means it combines with operators and works from anywhere. This lesson covers the search commands, the options that make them pleasant, and the Vim regular-expression dialect, which differs from the regex you may know from other tools.

The search commands

Command Effect
/pattern<Enter> search forward
?pattern<Enter> search backward
n next match in the same direction
N previous match (opposite direction)
* search forward for the whole word under the cursor
# the same backward
g* / g# as * and # but also match inside longer words
//<Enter> repeat the last search pattern
/<Up> recall earlier searches

After a search, the cursor sits on the match, and n/N step through the others. Searching wraps at the end of the file with a message search hit BOTTOM, continuing at TOP. Because search is a motion, d/end<Enter> deletes up to the next end and v/end<Enter> selects to it.

Options that make search comfortable

Put these in your config (chapter 6):

VIM
set ignorecase
set smartcase
set incsearch
set hlsearch
  • ignorecase ignores case, and smartcase makes the search case-sensitive when your pattern has a capital letter. /foo matches Foo; /Foo matches only Foo.
  • incsearch jumps to matches as you type, which is a live preview.
  • hlsearch highlights every match. Turn off the highlight with :nohlsearch (many people map <Esc> or <leader>h to it).

You can force case for one search: \c anywhere in the pattern ignores case, \C requires it. /foo\c matches FOO.

Search offsets

After the pattern, an offset adjusts where the cursor ends:

  • /foo/e ends at the last character of the match.
  • /foo/e+1 one past the end.
  • /foo/b+2 two characters after the start of the match.
  • /foo/+1 the line after the match.

With an operator, d/foo/e<Enter> deletes through the end of the match instead of stopping before it. That is how you delete "up to and including" a pattern.

Vim regular expressions

Vim's default mode, called magic, treats some characters as special without a backslash (. * [] ^ $) and others only with a backslash. This is the biggest difference from PCRE or JavaScript.

Meaning Vim JavaScript or PCRE
any character . .
zero or more * *
one or more \+ +
zero or one \? or \= ?
grouping \( \) ( )
word start / end \< \> \b
digit \d \d
word character \w \w
whitespace \s \s
non-greedy zero or more \{-} *?
count \{2,5} {2,5}

Very magic

Put \v at the start of a pattern and almost everything behaves like PCRE: /\v(foo|bar)+\d{2,}. It saves a lot of backslashes, and most experienced users type \v out of habit. The opposite, \V (very nomagic), treats everything literally except the backslash, handy for searching special characters: /\Va.b*c.

JS Vim pattern to JavaScript regex (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first pattern matches only the standalone word cat, not catalog, because \< and \> mean word boundaries. This is exactly what * does for you: it searches for \<word\>.

Common patterns

  • Whole word: /\<end\>
  • Lines starting with a word: /^import
  • Empty lines: /^$
  • Trailing whitespace: /\s\+$
  • A number: /\d\+
  • A line containing both words (any order): /\vfoo.*bar|bar.*foo
  • Last visual selection: /\%Vtext searches only inside it

Using search results

  • :%s//new/g uses the last search as the pattern for a substitution (next lesson).
  • ciw then n. is the selective-replace rhythm from the dot lesson.
  • :g/pattern/cmd runs a command on every matching line.
  • :vimgrep /pattern/ **/*.py searches many files and fills the quickfix list: browse it with :copen, :cnext and :cprev. Neovim and modern Vim also support :grep with ripgrep set as grepprg.
  • q/ opens the search history in an editable window; q: does so for commands.

Searching within a line only

For short hops, remember f and t from basic motions: they are faster than / when the target is on the current line.

JS n and N with wrap-around (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does * do in Normal mode?

    • is shorthand for /<word> using the word under the cursor, and # searches backward.
  2. 2. With smartcase and ignorecase set, what does /Foo match?

    A capital letter in the pattern makes the search case-sensitive under smartcase.
  3. 3. What does \v at the start of a pattern do?

    \v very magic lets you write (foo|bar)+ without backslashes.
  4. 4. Which command deletes from the cursor through the end of the next match of foo?

    The /e offset places the end at the last character of the match, so d includes it.

Summary

  • / and ? search; n and N repeat; * and # search for the word under the cursor.
  • Set ignorecase, smartcase, incsearch and hlsearch for a friendlier search.
  • Offsets such as /e and /b+1 fine-tune where the cursor lands, which matters with operators.
  • Vim regex differs from PCRE: \+, \(\), \<\>; use \v to simplify.
  • Search is a motion, so it works with d, c, y and v.

Next: Substitute and Global: Find and Replace at Scale