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

How do I indent a line in Vim?

Answer

>>

Explanation

The >> command shifts the current line one shiftwidth to the right, adding indentation. This is essential for manually adjusting code formatting.

How it works

  • >> indents the current line by one level (determined by the shiftwidth setting)
  • << does the reverse, removing one level of indentation
  • The amount of indentation per level is controlled by :set shiftwidth=N

Example

Given the text with shiftwidth=4:

if (true) {
return 1;
}

With the cursor on return 1;, pressing >> results in:

if (true) {
    return 1;
}

Tips

  • Use 3>> to indent the current line and the next 2 lines
  • Use << to unindent (outdent) a line by one level
  • In visual mode, select lines and press > to indent or < to unindent
  • After indenting in visual mode, press . to repeat the indentation
  • Use == to auto-indent the current line based on the surrounding context
  • Use >% when on a brace to indent everything inside the block

Next

How do I edit multiple lines at once using multiple cursors in Vim?