How do I make wrapped lines preserve their indentation level?
Answer
:set breakindent
Explanation
When wrap is enabled, long lines wrap to the next screen row starting at column 1 by default, which makes indented code look messy. The breakindent option fixes this by making wrapped lines visually continue at the same indentation level as the beginning of the line.
How it works
:set breakindentmakes wrapped continuation lines align with the indentation of the first line:set breakindentopt=shift:2adds an extra 2-column indent to wrapped lines for visual distinction:set breakindentopt=sbrplaces theshowbreakcharacter at the indentation level rather than column 0
This option only affects how the line is displayed — it does not change the actual text in the file.
Example
Without breakindent, a wrapped indented line looks like:
function processData(input,
output, callback, options) {
With breakindent enabled:
function processData(input,
output, callback, options) {
The wrapped portion now starts at the same indent level, making the code much easier to read.
Tips
- Combine with
set showbreak=\ \(two spaces) orset showbreak=↪to visually mark continuation lines - Pair with
set linebreakto wrap at word boundaries instead of mid-character - Add to your
.vimrcfor a permanent setup:set wrap breakindent linebreak