26

Is there a way to indent a selection of lines in Vim, like we have in text editors where we select a bunch of lines and press tab (or shift tab) to indent/unindent the selected lines?

I am talking about general indentation and not related to code indentation.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
umar
  • 4,309
  • 9
  • 34
  • 47
  • 1
    I made a screencast on this topic: http://vimcasts.org/episodes/indentation-commands/ – nelstrom Feb 25 '10 at 09:03
  • 1
    possible duplicate of [Indent multiple lines quickly in vi](http://stackoverflow.com/questions/235839/indent-multiple-lines-quickly-in-vi) – user Jul 23 '15 at 23:39

5 Answers5

42

You can select a set of lines with visual line mode (via Shift + V), and then type

>

and, to dedent,

<

You can also add numeric arguments. Find out you didn't indent enough? Hit gv to re-select your previous selection.

While typing in normal mode, try out Ctrl + T or Ctrl + D to indent or dedent.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Peter
  • 127,331
  • 53
  • 180
  • 211
20

Use visual mode as Peter suggests. You can also use X>> where X is the number of lines you want to indent. E.g. 5>> indents five lines from current line and down.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
  • 2
    One of the things I do most, due to Stack Overflow and writing code examples, is `>G`, to indent from the current position to the end of the file. I write in Ruby generally, so, it's actually `>G.` because Rubyists use two-space indent and SO requires four, but YMMV. – the Tin Man Jan 28 '13 at 01:18
11

I use the following mappings to indent/unindent:

vmap <TAB> >gv
vmap <S-TAB> <gv

Use TAB to indent and shift-TAB to unindent the visually selected lines.

If a block is selected Vim indents/unindents what is right of the start of the block.

Habi
  • 3,262
  • 25
  • 23
3

As suggested by the other answers you can use >. Alternatively, you can automatically correctly indent your code by selecting the set of line in visual mode (using shift+V), and then using =, or using == to indent the current line.

Ton van den Heuvel
  • 10,157
  • 6
  • 43
  • 82
3

There's a Vim Cast on this topic: Indentation commands

I like Vim Casts. They are informative and pleasant to watch.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jps
  • 956
  • 6
  • 8