2252

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?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Allain Lalonde
  • 85,857
  • 67
  • 175
  • 234

33 Answers33

2673

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Greg Hewgill
  • 828,234
  • 170
  • 1,097
  • 1,237
  • 1
    @akdom: interesting; that ('V') was always a key that I used for maps because it had no other use (v and g were two others). – Jonathan Leffler Oct 25 '08 at 05:45
  • 81
    I use >i} (indent inner {} block). Works in vim. Not sure it works in vi. – R. Martinho Fernandes Feb 15 '09 at 17:26
  • 1
    @Greg: consider adding to your answer that using set shiftwidth=' ' you can define the indentation size (default is 8) when using < or > – RomanM Feb 26 '09 at 06:13
  • 2
    I had been using control-v to indent by navigating to the front of the line but this is clearly much better. – gravitation Sep 20 '09 at 15:12
  • 2
    @Martinho: text objects, like i} are Vim-specific. Then again, so is visual mode. – jamessan Nov 11 '09 at 17:36
  • 11
    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) – Kamran Bigdely Feb 28 '11 at 23:25
  • 29
    @Kamran: See the [`shiftwidth`](http://vimdoc.sourceforge.net/htmldoc/options.html#'shiftwidth') setting for the way to change that. – Greg Hewgill Mar 01 '11 at 18:42
  • 2
    You can also use marks in normal mode to delimit the block, as in 'a'b> or patterns in the command line, as in :/begin/,/end/> – Pif Jan 17 '12 at 11:22
  • 4
    @MattStevens: You can find extended discussion about this phenomenon here: http://meta.stackexchange.com/questions/9731/fastest-gun-in-the-west-problem – Greg Hewgill Feb 28 '13 at 03: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 http://superuser.com/questions/220666/how-do-you-reuse-a-visual-mode-selection – David Mann May 09 '14 at 03:37
  • 3
    If you lose your selection, use gv to re-select – Will Hardwick-Smith Dec 21 '15 at 00:50
  • 2
    I've started using the [vim-pasta](https://github.com/sickill/vim-pasta) plugin for indent-aware pasting. Works like a charm! – wilson Sep 16 '16 at 16:27
  • "To indent 5 lines, `5>>`" I just tried that. It indents the fifth line, not 5 different lines starting at the cursor. – falsePockets Apr 08 '18 at 09:02
  • @falsePockets: Which version of vim are you using? The `5>>` command indents 5 lines for me. Be sure you aren't pressing Enter after the 5. – Greg Hewgill Apr 08 '18 at 20:41
  • I'm using version 8.0. The issue was that I was trying to type it in command mode (`esc :5>>`) instead of no mode. (`esc 5>>`) – falsePockets Apr 09 '18 at 10:56
  • On Mac In visual mode make sure to use arrows that are between `M` and `?` keys. – Dmitry Mar 18 '19 at 14:55
990

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
ire_and_curses
  • 64,177
  • 22
  • 110
  • 139
  • 14
    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 '11 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 '11 at 16:00
  • 1
    @Wipqozn - That's strange. It definitely indents the next five lines for me, tested on Vim 7.2.330. – ire_and_curses Aug 24 '11 at 16:21
  • 10
    >42gg Indent from where you are to line 42. – Steve Jan 06 '12 at 20:13
  • Great summary! Also note that the "indent inside block" and "indent all block" (a{ etc.) also works with parentheses and brackets: >a( 's, they also work with d,c,y etc.) – aqn Mar 06 '13 at 04:42
  • You might consider pointing out that the repeat command is a dot/full stop because depending on the font, the person's visions (which might be made worse if they're tired) it could look like something else. When I first saw it in your answer I was initially thrown off since I could have sworn it was a single quote. Just a thought that might or might not be worth considering. – Pryftan Mar 03 '20 at 21:17
125

A big selection would be:

gg=G

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Johan
  • 18,177
  • 25
  • 89
  • 107
  • 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 '14 at 21:57
  • 2
    I think `set cindent` should be in vimrc or should run `:set cindent` before running that command – Amanuel Nega May 19 '15 at 19:51
  • 3
    I think cindent must be set first. and @asgs i think this only works for cstyle programming languages. – Amanuel Nega May 19 '15 at 19:57
113

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
svec
  • 3,429
  • 2
  • 25
  • 28
  • 2
    doesn't work for me, just dumps my cursor to the line above the opening brace of 'the current code block i'm in'. – underscore_d Oct 17 '15 at 19:39
80

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Kent Fredric
  • 54,014
  • 14
  • 101
  • 148
  • 2
    This is great, but it uses spaces and not tabs. Any possible way to fix this? – Shane Reustle Mar 10 '11 at 22:24
  • 12
    If its using spaces instead of tabs, then its probably because you have indentation set to use spaces. =). – Kent Fredric Mar 16 '11 at 08:33
  • 4
    When the 'expandtab' option is off (this is the default) Vim uses s as much as possible to make the indent. ( :help :> ) – Kent Fredric Mar 16 '11 at 08:36
  • 1
    The only tab/space related vim setting I've changed is :set tabstop=3. It's actually inserting this every time I use >>: "". Same with indenting a block. Any ideas? – Shane Reustle Dec 02 '12 at 03:17
  • 3
    The three settings you want to look at for "spaces vs tabs" are 1. tabstop 2. shiftwidth 3. expandtab. You probably have "shiftwidth=5 noexpandtab", so a "tab" is 3 spaces, and an indentation level is "5" spaces, so it makes up the 5 with 1 tab, and 2 spaces. – Kent Fredric Dec 02 '12 at 17:08
  • in essence, it makes up the distance of "Shiftwidth" using tabs and spaces combined, where "tabstop" is "how many spaces is 1 tab". Here is a more visual guide that may explain: https://gist.github.com/4189922 – Kent Fredric Dec 02 '12 at 17:14
  • generally, I see people set "shiftwidth" and "tabstop" to the same value, as its less confusing. sw=8 ts=8 noet > tab inserts an 8-space-wide tab. sw=8 ts=8 et > tab inserts 8 characters. – Kent Fredric Dec 02 '12 at 17:16
69

The master of all commands is
gg=G

This indents the entire file!

And below are some of the simple and elegant commands used to indent lines quickly in Vim or gVim.

To indent the current line
==

To indent the all the lines below the current line

=G

To indent n lines below the current line

n==

For example, to indent 4 lines below the current line

4==

To indent a block of code, go to one of the braces and use command

=%

These are the simplest, yet powerful commands to indent multiple lines.

Sagar Jain
  • 6,261
  • 10
  • 40
  • 73
  • 4
    This is just in `vim`, not `vi`. – rojomoke Jul 30 '14 at 15:48
  • 3
    Not on my Solaris or AIX boxes it doesn't. The equals key has always been one of my standard ad hoc macro assignments. Are you sure you're not looking at a `vim` that's been linked to as `vi`? – rojomoke Jul 31 '14 at 10:09
  • I am damn sure!! After seeing your comment, I just opened 'vi' and tried all the commands in my answer and got the result as expected. – Sagar Jain Jul 31 '14 at 10:35
  • 3
    Yeah, on Linux, vi is almost always a link to vim. Try running the :ve command inside vi. – rojomoke Aug 01 '14 at 08:22
  • 4
    I love this kind of answers: clear, precise and succinct. Worked for me in Debian Jessie. Thanks, @SJain – digitai Dec 28 '15 at 17:28
  • 1
    Since I use vim, not vi, pretty psyched this answer is here. Thanks. – Dan Nissenbaum Aug 22 '17 at 03:58
69

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Daniel Spiewak
  • 52,267
  • 12
  • 104
  • 120
  • 1
    This is really useful. I am going to have to look up what all works with this. I know d'a and y'a, what else? – user606723 Mar 17 '11 at 15:31
  • 3
    This is very useful as it avoids the need to count how many lines you want to indent. – ziggy Aug 25 '14 at 14:14
31

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.
prayagupd
  • 27,100
  • 10
  • 130
  • 179
Michael Durrant
  • 84,444
  • 83
  • 284
  • 429
30

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Michael Ekoka
  • 15,842
  • 9
  • 67
  • 76
  • 17
    To indent the selection multiple times, you can simply press . to repeat the previous command. – sundar - Remember Monica Sep 01 '09 at 17:14
  • 2
    The problem with . in this situation is that you have to move your fingers. With @mike's solution (same one i use) you've already got your fingers on the indent key and can just keep whacking it to keep indenting rather than switching and doing something else. Using period takes longer because you have to move your hands and it requires more thought because it's a second, different, operation. – masukomi Dec 06 '13 at 21:24
29

As well as the offered solutions, I like to do things a paragraph at a time with >}

Paul Tomblin
  • 167,274
  • 56
  • 305
  • 392
  • 2
    Yup, and this is why one of my big peeves is white spaces on an otherwise empty line: they messes up vim's notion of a "paragraph". – aqn Mar 06 '13 at 04:47
21

Suppose you use 2 spaces to indent your code. Type:

:set shiftwidth=2
  • Type v (to enter visual block editing mode)
  • Move the cursor with the arrow keys (or with h/j/k/l) to highlight the lines you want to indent or unindent.

Then:

  • Type > to indent once (2 spaces).
  • Type 2> to indent twice (4 spaces).
  • Type 3> to indent thrice (6 spaces).
  • ...
  • Type < to unindent once (2 spaces).
  • Type 2< to unindent twice (4 spaces).
  • Type 3< to unindent thrice (6 spaces).
  • ...

You get the idea.

(Empty lines will not get indented, which I think is kind of nice.)


I found the answer in the (g)vim documentation for indenting blocks:

:help visual-block
/indent

If you want to give a count to the command, do this just before typing the operator character: "v{move-around}3>" (move lines 3 indents to the right).

John Sonderson
  • 2,852
  • 6
  • 26
  • 41
19

The beauty of Vim's UI is that its consistency. Editing commands are made up of the command and a cursor move. The cursor moves are always the same:

  • H to top of screen, L to bottom, M to middle
  • nG to go to line n, G alone to bottom of file, gg to top
  • n to move to next search match, N to previous
  • } to end of paragraph
  • % to next matching bracket, either of the parentheses or the tag kind
  • enter to the next line
  • 'x to mark x where x is a letter or another '.
  • many more, including w and W for word, $ or 0 to tips of the line, etc., that don't apply here because are not line movements.

So, in order to use vim you have to learn to move the cursor and remember a repertoire of commands like, for example, > to indent (and < to "outdent").

Thus, for indenting the lines from the cursor position to the top of the screen you do >H, >G to indent to the bottom of the file.

If, instead of typing >H, you type dH then you are deleting the same block of lines, cH for replacing it, etc.

Some cursor movements fit better with specific commands. In particular, the % command is handy to indent a whole HTML or XML block. If the file has syntax highlighted (:syn on) then setting the cursor in the text of a tag (like, in the "i" of <div> and entering >% will indent up to the closing </div> tag.

This is how Vim works: one has to remember only the cursor movements and the commands, and how to mix them. So my answer to this question would be "go to one end of the block of lines you want to indent, and then type the > command and a movement to the other end of the block" if indent is interpreted as shifting the lines, = if indent is interpreted as in pretty-printing.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Juan Lanus
  • 2,037
  • 22
  • 15
  • 2
    I would say that vi/vim is _mostly_ consistent. For instance, D does not behave the same as S and Y! :) – aqn Mar 06 '13 at 04:38
16

You can use the norm i command to insert given text at the beginning of the line. To insert 10 spaces before lines 2-10:

:2,10norm 10i 

Remember that there has to be a space character at the end of the command - this will be the character we want to have inserted. We can also indent a line with any other text, for example to indent every line in a file with five underscore characters:

:%norm 5i_

Or something even more fancy:

:%norm 2i[ ]

More practical example is commenting Bash/Python/etc code with # character:

:1,20norm i#

To re-indent use x instead of i. For example, to remove first 5 characters from every line:

:%norm 5x
Nykakin
  • 7,917
  • 2
  • 24
  • 35
14

Do this:

$vi .vimrc

And add this line:

autocmd FileType cpp setlocal expandtab shiftwidth=4 softtabstop=4 cindent

This is only for a cpp file. You can do this for another file type, also just by modifying the filetype...

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
pankaj ukumar
  • 141
  • 1
  • 2
12

A quick way to do this using VISUAL MODE uses the same process as commenting a block of code.

This is useful if you would prefer not to change your shiftwidth or use any set directives and is flexible enough to work with TABS or SPACES or any other character.

  1. Position cursor at the beginning on the block
  2. v to switch to -- VISUAL MODE --
  3. Select the text to be indented
  4. Type : to switch to the prompt
  5. Replacing with 3 leading spaces:

    :'<,'>s/^/ /g

  6. Or replacing with leading tabs:

    :'<,'>s/^/\t/g

  7. Brief Explanation:

    '<,'> - Within the Visually Selected Range

    s/^/ /g - Insert 3 spaces at the beginning of every line within the whole range

    (or)

    s/^/\t/g - Insert Tab at the beginning of every line within the whole range

Eric Kigathi
  • 1,457
  • 18
  • 21
12

>} or >{ indent from current line up to next paragraph

<} or <{ same un-indent

animuson
  • 50,765
  • 27
  • 132
  • 142
jash
  • 121
  • 1
  • 2
11

I like to mark text for indentation:

  1. go to beginning of line of text then type ma (a is the label from the 'm'ark: it could be any letter)
  2. go to end line of text and type mz (again, z could be any letter)
  3. :'a,'z> or :'a,'z< will indent or outdent (is this a word?)
  4. Voila! The text is moved (empty lines remain empty with no spaces)

PS: you can use the :'a,'z technique to mark a range for any operation (d, y, s///, etc.) where you might use lines, numbers, or %.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
SteveO
  • 111
  • 1
  • 2
10

For me, the MacVim (Visual) solution was, select with mouse and press ">", but after putting the following lines in "~/.vimrc" since I like spaces instead of tabs:

set expandtab
set tabstop=2
set shiftwidth=2

Also it's useful to be able to call MacVim from the command-line (Terminal.app), so since I have the following helper directory "~/bin", where I place a script called "macvim":

#!/usr/bin/env bash
/usr/bin/open -a /Applications/MacPorts/MacVim.app $@

And of course in "~/.bashrc":

export PATH=$PATH:$HOME/bin

MacPorts messes with "~/.profile" a lot, so the PATH environment variable can get quite long.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
mda
  • 1,048
  • 13
  • 18
10
:line_num_start,line_num_end>

For example,

14,21> shifts line number 14 to 21 to one tab

Increase the '>' symbol for more tabs.

For example,

14,21>>> for three tabs
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
attaboyabhipro
  • 995
  • 1
  • 12
  • 29
  • 1
    There are clearly a lot of ways to solve this, but this is the easiest to implement, as line numbers show by default in vim and it doesn't require math. – HoldOffHunger Dec 05 '17 at 15:50
  • @HoldOffHunger Line numbers do not show by default. You need to use `:set number` to toggle line numbers (they are *off* by default), or put `set number` in your vimrc. – DryLabRebel Mar 04 '20 at 04:58
10

:help left

In ex mode you can use :left or :le to align lines a specified amount. Specifically, :left will Left align lines in the [range]. It sets the indent in the lines to [indent] (default 0).

:%le3 or :%le 3 or :%left3 or :%left 3 will align the entire file by padding with three spaces.

:5,7 le 3 will align lines 5 through 7 by padding them with three spaces.

:le without any value or :le 0 will left align with a padding of 0.

This works in Vim and gVim.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
zundarz
  • 1,430
  • 3
  • 21
  • 38
  • 1
    Awesome, just what I was looking for (a way to insert a specific number of spaces -- 4 spaces for markdown code -- to override my normal indent). In my case I wanted to indent a specific number of lines in visual mode, so shift-v to highlight the lines, then `:'le4` to insert the spaces. Thanks! – Subfuzion Aug 11 '17 at 22:02
9

5== will indent five lines from the current cursor position.

So you can type any number before ==. It will indent the number of lines. This is in command mode.

gg=G will indent the whole file from top to bottom.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
rohitkadam19
  • 1,534
  • 4
  • 19
  • 38
9

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
NickSoft
  • 2,880
  • 5
  • 22
  • 43
9

I don’t know why it's so difficult to find a simple answer like this one...

I myself had to struggle a lot to know this. It's very simple:

  • Edit your .vimrc file under the home directory.
  • Add this line

    set cindent
    

    in your file where you want to indent properly.

  • In normal/command mode type

    10==   (This will indent 10 lines from the current cursor location)
    gg=G   (Complete file will be properly indented)
    
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
7

Using Python a lot, I find myself needing frequently needing to shift blocks by more than one indent. You can do this by using any of the block selection methods, and then just enter the number of indents you wish to jump right before the >

For example, V5j3> will indent five lines three times - which is 12 spaces if you use four spaces for indents.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
John La Rooy
  • 263,347
  • 47
  • 334
  • 476
7

To indent every line in a file type, Esc and then G=gg.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
kapil
  • 577
  • 11
  • 20
7

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...
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
sqqqrly
  • 131
  • 1
  • 3
  • This is cumbersome, but is the way to go _if you do formatting outside of core VIM_ (for instance, using `vim-prettier` instead of the default indenting engine). Using `>` will otherwise royally scew up the formatting done by Prettier. – oligofren Mar 27 '18 at 15:23
  • Funny, I find it anything but cumbersome. This is not a uni-tasker! Learning this method has many uses beyond indenting. – sqqqrly Jun 15 '18 at 16:30
  • I find it better than the accepted answer, as I can see what is happening, the lines I'm selecting and the action I'm doing, and not just type some sort of vim incantation. – user4052054 Aug 17 '18 at 17:50
7
  • 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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Kulamani
  • 461
  • 6
  • 13
6

How to indent highlighted code in vi immediately by a number of spaces:

Option 1: Indent a block of code in vi to three spaces with Visual Block mode:

  1. Select the block of code you want to indent. Do this using Ctrl+V in normal mode and arrowing down to select text. While it is selected, enter : to give a command to the block of selected text.

  2. The following will appear in the command line: :'<,'>

  3. To set indent to three spaces, type le 3 and press enter. This is what appears: :'<,'>le 3

  4. The selected text is immediately indented to three spaces.

Option 2: Indent a block of code in vi to three spaces with Visual Line mode:

  1. Open your file in vi.
  2. Put your cursor over some code
  3. Be in normal mode and press the following keys:

    Vjjjj:le 3
    

    Interpretation of what you did:

    V means start selecting text.

    jjjj arrows down four lines, highlighting four lines.

    : tells vi you will enter an instruction for the highlighted text.

    le 3 means indent highlighted text three lines.

    The selected code is immediately increased or decreased to three spaces indentation.

Option 3: use Visual Block mode and special insert mode to increase indent:

  1. Open your file in vi.
  2. Put your cursor over some code
  3. Be in normal mode press the following keys:

    Ctrl+V

    jjjj
    

    (press the spacebar five times)

    Esc Shift+i

    All the highlighted text is indented an additional five spaces.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Eric Leschinski
  • 123,728
  • 82
  • 382
  • 321
4
  1. Press "SHIFT + v" to enter VISUAL LINE mode.
  2. Select the text you wish to indent but using either the cursor keys or the "j" and "k" keys.
  3. To indent right press "SHIFT + dot" (> character). To indent left press "SHIFT + comma" (< character).

Source: https://www.fir3net.com/UNIX/General/how-do-i-tab-multiple-lines-within-vi.html

ABN
  • 374
  • 5
  • 16
3

To indent all of the file by four:

esc 4G=G
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Michael
  • 299
  • 6
  • 10
  • 3
    ...what? 'indent by 4 spaces'? No, this jumps to line 4 and then indents everything from there to the end of the file, using the currently selected indent mode (if any). – underscore_d Oct 17 '15 at 19:35
1

For who like modern editors to indent selected line with <TAB> and <S-TAB>:

vnoremap <TAB> >gv
vnoremap <S-TAB> <gv

Usage: Hit V for line-wise visual-mode, select lines you want, then hit Tab(may be with shift), then indention applies as you want and selection remains...

Nima
  • 655
  • 8
  • 15
1

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

KayV
  • 9,560
  • 8
  • 68
  • 111
0

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

MAGA
  • 6,370
  • 3
  • 14
  • 35