How do I toggle line wrapping on or off in Vim?
Answer
:set wrap! or :set nowrap
Explanation
How it works
By default, Vim wraps long lines that extend past the window width, displaying them across multiple screen lines. You can control this behavior with the wrap option.
:set wrap- Enable line wrapping (default):set nowrap- Disable line wrapping (long lines extend off-screen):set wrap!- Toggle wrapping on or off:set wrap?- Check the current wrap state
When wrapping is disabled, you can scroll horizontally with zl and zh (one character at a time) or zL and zH (half a screen width).
Related options that control how wrapping looks:
:set linebreak- Wrap at word boundaries instead of mid-word:set breakindent- Preserve indentation on wrapped lines:set showbreak=>>- Show a visual indicator at the start of wrapped lines
To navigate within wrapped lines (screen lines vs actual lines):
gj/gk- Move down/up by screen line instead of file lineg0/g$- Move to start/end of screen line
To make this permanent, add set nowrap or set wrap to your ~/.vimrc.
Example
When editing a file with long lines like CSV data:
id,name,email,address,phone,city,state,zip,country,notes
1,John Doe,[email protected],123 Main St,555-0100,Springfield,IL,62701,US,Regular customer since 2020
:set nowrap- Lines no longer wrap, you can see structure more clearly- Use
zlandzhto scroll horizontally :set wrap- Turn wrapping back on when done
Adding :set linebreak with wrapping ensures Vim breaks at word boundaries rather than in the middle of words, making wrapped text much more readable.