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

How do I indent or unindent selected lines in visual mode?

Answer

> and <

Explanation

How it works

In visual mode, you can shift selected lines to the right or left using the > and < keys. Select one or more lines with V (linewise visual mode), then press > to indent them or < to unindent them by one shiftwidth level.

After pressing > or <, the visual selection is lost. A common workflow is to use gv immediately after to reselect and indent again if you need multiple levels of indentation.

  • > shifts the selected lines one shiftwidth to the right (adds indentation)
  • < shifts the selected lines one shiftwidth to the left (removes indentation)
  • The amount of indentation is controlled by the shiftwidth option (default is 8)

You can also prefix with a count: 3> will indent the selection three shiftwidth levels at once.

Example

Suppose you have this code and want to indent lines 2-4:

if (true) {
let x = 1;
let y = 2;
let z = 3;
}
  1. Place your cursor on line 2
  2. Press V to enter linewise visual mode
  3. Press 2j to extend selection to line 4
  4. Press > to indent one level

Result:

if (true) {
    let x = 1;
    let y = 2;
    let z = 3;
}

To indent further, press gv to reselect then > again, or use a count like 2> from the start.

Next

How do you yank a single word into a named register?