tags:

views:

763

answers:

7

In many GUIs when I select a section of text and then hit the TAB or Shift-TAB button the selected section will indent in or out.

In VIM I can select a visual section and then hit the "<" or ">" key to change indenting, however once I hit the "<" key my selection is cleared so I am stuck selecting the section again and hitting "<" again. This gets really annoying when I am trying to indent something a few stops in and want visual feedback.

Is there anyway to indent stuff in and out in vim while keeping the current selected text selected?

Is there a trick to re-map TAB and Shift TAB so they behave in this way in visual mode?

+13  A: 

You can prefix a number, ie. "2>" to indent two tab stops. Or, you can use ">" to indent once, then "." to indent again (this works even though the block is no longer highlighted). If you go too far, "u" will undo one step at a time.

Another useful command is "gv" to restore the last visual block, if you need to apply a different command.

Greg Hewgill
This is great advice!
Sam Saffron
+3  A: 

Or, you can go the nearest brace and type =% in normal mode -- it indents the block covered by the brace and its matching one. But I'm not sure how this is useful in a language such as Python.

artknish
A: 

Since I've abandoned vi to vim, I've never used > nor < again. I exploit vim automated indentation that can be explicitly triggered with =

It works very well to motions like =a{ which is even more efficient than =%

Otherwise, If one > isn't enough, just redo it with ., or undo the change with u.

Luc Hermitte
+7  A: 
vmap <Tab> >gv
vmap <S-Tab> <gv
Brian Carper
Note that while this works well for gVim, it may not work for console vim unless vim and your terminal agree on how to represent the Shift-Tab keystroke.
Greg Hewgill
+2  A: 

Another way is to select a block and insert an indent at the beginning of the line using this sequence:

  1. ctrl+V + arrow keys to select the block.
  2. I to switch to insert mode such that the inserted text is inserted at the beginning of the selection in each line in the selected block.
  3. ctrl+T to increase the indent or ctrl+D to decrease the indent. You can add any number of indents like this. Note: The indentation will be seen only the first line of the block, but when insert mode is exited the indentation will be replicated on all the lines in the block.
Nathan Fellman
This does work ... Im learning a lot today :)
Sam Saffron
+1  A: 

Try >} for 'indent next paragraph one level'.

chaos
+1  A: 

Try using "." to repeat the command. It remembers the range, and you can use "u" to undo one level if you go too far. No configuration needed.

Douglas Mayle