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

How do I toggle whether / and ? searches wrap around the file?

Answer

:set wrapscan!

Explanation

wrapscan controls what happens when a / or ? search reaches the end (or beginning) of the file. With wrapscan enabled, searching continues from the other side of the buffer. Toggling it quickly is useful when you want to avoid accidental wrap-around during careful refactors, then turn wrap-around back on for normal navigation.

How it works

  • :set changes an option from command-line mode
  • wrapscan is the search option that enables wrap-around behavior
  • ! toggles boolean options (on becomes off, off becomes on)

You can check the current state with :set wrapscan?. Many users leave it enabled globally, but temporarily disabling it can prevent confusing jumps when reviewing long files.

Example

Suppose your cursor is near the bottom of this file and you search for TODO:

line 10: TODO: refactor parser
...
line 600: cursor here
  • With wrapscan on, /TODO can wrap and jump back to line 10
  • With wrapscan off, Vim reports that the pattern is not found past your current position

Tips

  • Use :set wrapscan to force-enable and :set nowrapscan to force-disable
  • Pair with n and N when doing directional review passes so movement stays predictable

Next

How do I store the current file path in a named Vim register?