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
:setchanges an option from command-line modewrapscanis the search option that enables wrap-around behavior!toggles boolean options (onbecomesoff,offbecomeson)
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
wrapscanon,/TODOcan wrap and jump back to line 10 - With
wrapscanoff, Vim reports that the pattern is not found past your current position
Tips
- Use
:set wrapscanto force-enable and:set nowrapscanto force-disable - Pair with
nandNwhen doing directional review passes so movement stays predictable