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

How do I search backward for text in Vim?

Answer

?{pattern}

Explanation

The ? command searches backward from the cursor position. It works exactly like / but in reverse.

How it works

  • Press ? to open the backward search prompt
  • Type your search pattern
  • Press <CR> to find the previous match before the cursor

Example

line one
line two
line three

With the cursor on line three, typing ?one<CR> jumps to one on the first line.

Tips

  • n continues searching in the backward direction
  • N reverses to search forward
  • ? also wraps around the beginning of the file
  • You can use the same regex patterns as with /

Next

How do you yank a single word into a named register?