It should be trivial, and it might even be in the help, but I can't figure out how to navigate it. How do I indent multiple lines quickly in vi?
It should be trivial, and it might even be in the help, but I can't figure out how to navigate it. How do I indent multiple lines quickly in vi?
Use the > command. To indent five lines, 5>>. To mark a block of lines and indent it, Vjj> to indent three lines (Vim only). To indent a curly-braces block, put your cursor on one of the curly braces and use >% or from anywhere inside block use >iB.
If you’re copying blocks of text around and need to align the indent of a block in its new location, use ]p instead of just p. This aligns the pasted block with the surrounding text.
Also, the shiftwidth setting allows you to control how many spaces to indent.
Also try this for C-indenting indentation. Do :help = for more information:
={
That will auto-indent the current code block you're in.
Or just:
==
to auto-indent the current line.
Key presses for more visual people:
Enter Command Mode:
Escape
Move around to the start of the area to indent:
hjkl↑↓←→
Start a block:
v
Move around to the end of the area to indent:
hjkl↑↓←→
(Optional) Type the number of indentation levels you want
0..9
Execute the indentation on the block:
>
In addition to the answer already given and accepted, it is also possible to place a marker and then indent everything from the current cursor to the marker.
Thus, enter ma where you want the top of your indented block, cursor down as far as you need and then type >'a (note that "a" can be substituted for any valid marker name). This is sometimes easier than 5>> or vjjj>.
When you select a block and use > to indent, it indents then goes back to normal mode. I have this in my .vimrc file:
vnoremap < <gv
vnoremap > >gv
It lets you indent your selection as many time as you want.
Go to the start of the text
As well as the offered solutions, I like to do things a paragraph at a time with >}
I didn't find a method I use in the comments, so I'll share it (I think Vim only):
This is useful when you don't want to change indentation/tab settings in vimrc or to remember them to change it while editing.
To unindent I use the same Ctrl + V block select to select spaces and delete it with D.
I use block-mode visual selection:
I (Shift + I) to enter insert mode.This is not a uni-tasker. It works:
c instead of I).For a block of code, {}: = + %
For a selected line: Shift + v select using the up/down arrow keys, and then press =.
For the entire file: gg + = + G
Note: 'gg' means go to line 1, '=' is the indent command, and 'G' moves the cursor to the end of file.
Source: https://www.fir3net.com/UNIX/General/how-do-i-tab-multiple-lines-within-vi.html
For who like modern editors to indent selected line with <TAB> -> Tab and <S-TAB> -> Shift+Tab:
vnoremap <TAB> >gv
vnoremap <S-TAB> <gv
Usage:
Hit V for line-wise visual-mode, select lines you want, then hit Tab(maybe with shift), then indention applies as you want and selection remains...
For mac,
Suppose | represents the position of the cursor in Vim. If the text to be indented is enclosed in a code block like:
int main() {
line1
line2|
line3
}
you can do >i{ which means "indent (>) inside (i) block ({)" and get:
int main() {
line1
line2|
line3
}
Now suppose the lines are contiguous but outside a block, like:
do
line2|
line3
line4
done
To indent lines 2 thru 4 you can visually select the lines and type >. Or even faster you can do >2j to get:
do
line2|
line3
line4
done
Note that >Nj means indent from current line to N lines below. If the number of lines to be indented is large, it could take some seconds for the user to count the proper value of N. To save valuable seconds you can activate the option of relative number with set relativenumber (available since Vim version 7.3).