In normal mode
type >> to indent the current line, or << to unindent (shift). Each command can be used with a count. The operators > and < do the same for motions, text objects and visual selections. For all commands, pressing . repeats the operation.
For example, typing 5>> . . shifts the first five lines to the right (starting from the line of the cursor),
and then repeats the operation twice so that the five lines are shifted three times.
Try to see! It's the best thing! But otherwise 5>> will activate the first indent and it happen as long as you hit the third stroke! And . will repeat! And it happen immediatly, u if you want to undo!
In insert mode
CtrlT indents the current line, and CtrlD unindents.
When indenting or unindenting, lines are shifted one 'shiftwidth' to the right or left.
Basic examples
To adjust the indent on three lines:
Put the cursor anywhere in the first line.
Press V then jj to visually select the three lines.
Or just navigate with Up Down.
Press > to indent (shift text one 'shiftwidth' to the right), or press < to shift left.
Press . to repeat the indent, or u to undo if you have shifted too far.
Type gv if you want to reselect the lines (not needed).
Alternatively, if you know that you want to adjust three lines, you can simply:
Type 3>>
to shift right or 3<<
to shift left.
Or:
Type >2j
to shift right or <2j
to shift left.
As mentioned above, the > and < commands combine with arbitrary Vim movements and text objects. For example, >}
to indent from the cursor to the next blank line, or <aB
to un-indent the current C-like {...} "block" structure.
Check more
There is more you can check on this awesome tuto! It's from where i get the information! And added some lines!
https://vim.fandom.com/wiki/Shifting_blocks_visually
For code blocks
https://vim.fandom.com/wiki/Indent_a_code_block
int myfunction(int a)
{
if ( a == 1 ) {
printf("one");
return 1; // the cursor is in this line
}
return 0;
}
These commands will fix the indents:
=i{ reindents "inner block" (inside the braces).
=a{ reindents "a block" (including the braces).
=2a{ reindents 2 blocks (this block and containing block).
Instead of {, you can use } or B, for example, =aB indents a block.
These commands will decrease or increase indents:
>i{ increase indent for inner block.
. repeat last change (increase indent of block again).
<i{ decrease indent for inner block.
With the cursor on { or }:
=% indents the block (including matching brace).
>% or <% indents or unindents the block.
gg=G