editor indentation vi vim

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.


    • 81

      I use >i} (indent inner {} block). Works in vim. Not sure it works in vi.

      Feb 15, 2009 at 17:26

    • 12

      My problem(in gVim) is that the command > indents much more than 2 blanks (I want just two blanks but > indent something like 5 blanks)

      Feb 28, 2011 at 23:25

    • 31

      @Kamran: See the shiftwidth setting for the way to change that.

      Mar 1, 2011 at 18:42

    • 4

      @MattStevens: You can find extended discussion about this phenomenon here:…

      Feb 28, 2013 at 3:36

    • 6

      I often indent visual blocks multiple times in a row, such as fixing some tags pasted in to an XML file. Rather than re-select the block in visual mode each time, one can use ‘gv’ to reuse the last visual block. Reference…

      May 9, 2014 at 3:37


    This answer summarises the other answers and comments of this question, and it adds extra information based on the Vim documentation and the Vim wiki. For conciseness, this answer doesn’t distinguish between Vi and Vim-specific commands.

    In the commands below, “re-indent” means “indent lines according to your indentation settings.” shiftwidth is the primary variable that controls indentation.

    General Commands

    >>   Indent line by shiftwidth spaces
    <<   De-indent line by shiftwidth spaces
    5>>  Indent 5 lines
    5==  Re-indent 5 lines
    >%   Increase indent of a braced or bracketed block (place cursor on brace first)
    =%   Reindent a braced or bracketed block (cursor on brace)
    <%   Decrease indent of a braced or bracketed block (cursor on brace)
    ]p   Paste text, aligning indentation with surroundings
    =i{  Re-indent the 'inner block', i.e. the contents of the block
    =a{  Re-indent 'a block', i.e. block and containing braces
    =2a{ Re-indent '2 blocks', i.e. this block and containing block
    >i{  Increase inner block indent
    <i{  Decrease inner block indent

    You can replace { with } or B, e.g. =iB is a valid block indent command. Take a look at “Indent a Code Block” for a nice example to try these commands out on.

    Also, remember that

    .    Repeat last command

    , so indentation commands can be easily and conveniently repeated.

    Re-indenting complete files

    Another common situation is requiring indentation to be fixed throughout a source file:

    gg=G  Re-indent entire buffer

    You can extend this idea to multiple files:

    " Re-indent all your C source code:
    :args *.c
    :argdo normal gg=G

    Or multiple buffers:

    " Re-indent all open buffers:
    :bufdo normal gg=G:wall

    In Visual Mode

    Vjj> Visually mark and then indent three lines

    In insert mode

    These commands apply to the current line:

    CTRL-t   insert indent at start of line
    CTRL-d   remove indent at start of line
    0 CTRL-d remove all indentation from line

    Ex commands

    These are useful when you want to indent a specific range of lines, without moving your

    :< and :> Given a range, apply indentation e.g.
    :4,8>   indent lines 4 to 8, inclusive

    Indenting using markers

    Another approach is via markers:

    ma     Mark top of block to indent as marker 'a'

    …move cursor to end location

    >'a    Indent from marker 'a' to current location

    Variables that govern indentation

    You can set these in your .vimrc file.

    set expandtab       "Use softtabstop spaces instead of tab characters for indentation
    set shiftwidth=4    "Indent by 4 spaces when using >>, <<, == etc.
    set softtabstop=4   "Indent by 4 spaces when pressing <TAB>
    set autoindent      "Keep indentation from previous line
    set smartindent     "Automatically inserts indentation in some cases
    set cindent         "Like smartindent, but stricter and more customisable

    Vim has intelligent indentation based on filetype. Try adding this to your .vimrc:

    if has ("autocmd")
        " File type detection. Indent based on filetype. Recommended.
        filetype plugin indent on



    • 15

      Both this answer and the one above it were great. But I +1’d this because it reminded me of the ‘dot’ operator, which repeats the last command. This is extremely useful when needing to indent an entire block several shiftspaces (or indentations) without needing to keep pressing >}. Thanks a long

      – Amit

      Aug 10, 2011 at 13:26

    • 1

      5>> Indent 5 lines : This command indents the fifth line, not 5 lines. Could this be due to my VIM settings, or is your wording incorrect?

      – Wipqozn

      Aug 24, 2011 at 16:00

    • 1

      @Wipqozn – That’s strange. It definitely indents the next five lines for me, tested on Vim 7.2.330.

      Aug 24, 2011 at 16:21

    • 10

      >42gg Indent from where you are to line 42.

      – Steve

      Jan 6, 2012 at 20:13

    • Great summary! Also note that the “indent inside block” and “indent all block” (<i{ >a{ etc.) also works with parentheses and brackets: >a( <i] etc. (And while I’m at it, in addition to <>’s, they also work with d,c,y etc.)

      – aqn

      Mar 6, 2013 at 4:42


    A big selection would be:


    It is really fast, and everything gets indented 😉


    • 1

      I’ve an XML file and turned on syntax highlighting. Typing gg=G just puts every line starting from position 1. All the white spaces have been removed. Is there anything else specific to XML?

      – asgs

      Jan 28, 2014 at 21:57

    • 2

      I think set cindent should be in vimrc or should run :set cindent before running that command

      May 19, 2015 at 19:51

    • 3

      I think cindent must be set first. and @asgs i think this only works for cstyle programming languages.

      May 19, 2015 at 19:57