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 oneshiftwidthto the right (adds indentation)<shifts the selected lines oneshiftwidthto the left (removes indentation)- The amount of indentation is controlled by the
shiftwidthoption (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;
}
- Place your cursor on line 2
- Press
Vto enter linewise visual mode - Press
2jto extend selection to line 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.