Vim tip 2: indent/unindent lines
Normal mode
- >> indent the current line
- 3>> indent the current line and two lines below (same as 2>j)
- >k indent the current line and the line above (same as 1>k or >1k)
- << unindent the current line
- 5<< unindent the current line and four lines below (same as 4<j or <4j)
- 2<k unindent the current line and two lines above (same as <2k)
- = auto indent code, use motion commands to indicate the portion to be indented
- =4j auto indents the current line and four lines below
- =ip auto indents the current paragraph
You can use any motion command with > and <. For example, >} indents till the end of the paragraph.
Visual mode
- > indent the visually selected lines once
- 3> indent the visually selected lines three times
- < unindent the visually selected lines once
- = auto indent code
Consider the following unindented code:
for(i=1; i<5; i++)
{
for(j=i; j<10; j++)
{
statements
}
statements
}
Here's the result after applying vip= (you can also use =ip if you prefer Normal mode).
for(i=1; i<5; i++)
{
for(j=i; j<10; j++)
{
statements
}
statements
}
Indentation depends on the shiftwidth
setting.
See :h shift-left-right, :h = and :h 'shiftwidth' for documentation.
Video demo:
See also my Vim Reference Guide and curated list of resources for Vim.