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

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 breakindent makes wrapped continuation lines align with the indentation of the first line
  • :set breakindentopt=shift:2 adds an extra 2-column indent to wrapped lines for visual distinction
  • :set breakindentopt=sbr places the showbreak character 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) or set showbreak=↪ to visually mark continuation lines
  • Pair with set linebreak to wrap at word boundaries instead of mid-character
  • Add to your .vimrc for a permanent setup: set wrap breakindent linebreak

Next

How do I run a search and replace only within a visually selected region?