24

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
  • 28,342
  • 21
  • 95
  • 123
umar
  • 4,139
  • 8
  • 31
  • 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

39

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
  • 28,342
  • 21
  • 95
  • 123
Peter
  • 114,224
  • 47
  • 167
  • 205
19

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
  • 28,342
  • 21
  • 95
  • 123
Brian Rasmussen
  • 109,816
  • 33
  • 208
  • 305
  • 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
10

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,095
  • 21
  • 21
4

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

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
jps
  • 898
  • 6
  • 6
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
  • 8,849
  • 5
  • 36
  • 76