views:

377

answers:

5

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.

+14  A: 

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
Shouldn't it be 'outdent' instead of 'dedent'?
Vinko Vrsalovic
@Vinko should be, but isn't :) an outdent is a backwards indent; a dedent is an indent undone. perhaps it should really be undent.
Peter
You don't need to re-select the lines to indent more. Just use the dot command: >... will indent four times.
Marius Andersen
+8  A: 

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.

Brian Rasmussen
+2  A: 

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

Ton van den Heuvel
+3  A: 

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
This works perfect. Thank you.
Nadal
It works Perfect for me too! @ MacVim
Daniel Silveira
+2  A: 

There's a Vim Cast on this topic:

http://vimcasts.org/episodes/indentation-commands/

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

jps