Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 396
Allain Lalonde Avatar asked Oct 25 '08 03:10

Allain Lalonde


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

like image 108
Greg Hewgill Avatar answered Sep 23 '22 05:09

Greg Hewgill


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

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

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

References

  • Indent a code block
  • Shifting blocks visually
  • Indenting source code
  • :help =
like image 25
ire_and_curses Avatar answered Sep 21 '22 05:09

ire_and_curses