Indent multiple lines quickly in vi

Viewed 895373

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?

33 Answers

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.

A big selection would be:

gg=G

It is really fast, and everything gets indented ;-)

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:

  1. Enter Command Mode:
    Escape

  2. Move around to the start of the area to indent:
    hjkl

  3. Start a block:
    v

  4. Move around to the end of the area to indent:
    hjkl

  5. (Optional) Type the number of indentation levels you want
    0..9

  6. 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

  • press v for visual mode.
  • use up/down arrow to highlight text.
  • press = to indent all the lines you highlighted.

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):

  1. Esc to enter command mode
  2. Move to the first character of the last line you want to indent
  3. Ctrl + V to start block select
  4. Move to the first character of the first line you want to indent
  5. Shift + I to enter special insert mode
  6. Type as many space/tabs as you need to indent to (two for example
  7. Press Esc and spaces will appear in all lines

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:

  • Go to the front of the block to move (at the top or bottom).
  • Press Ctrl + V to enter visual block mode.
  • Navigate to select a column in front of the lines.
  • Press I (Shift + I) to enter insert mode.
  • Type some spaces.
  • Press Esc. All lines will shift.

This is not a uni-tasker. It works:

  • In the middle of lines.
  • To insert any string on all lines.
  • To change a column (use c instead of I).
  • yank, delete, substitute, etc...
  • 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.

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,

  1. Open the file using vim

    vim deploy1.yml

  2. Select lines using Shift + 'v' and then using 'up' or 'down' key

    enter image description here

  3. Indent selected lines using Shift + '>' or Shift + '<'

    enter image description here

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).

Related