38

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?

Sam Saffron
  • 128,308
  • 78
  • 326
  • 506

12 Answers12

57

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
  • 951,095
  • 183
  • 1,149
  • 1,285
40
vmap <Tab> >gv
vmap <S-Tab> <gv
Brian Carper
  • 71,150
  • 28
  • 166
  • 168
8

With visual block selection

Steps:
Select the block(Ctrl + v)
type c(change)
type needed space
type esc

enter image description here

vusan
  • 5,221
  • 4
  • 46
  • 81
8

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
  • 122,701
  • 101
  • 260
  • 319
5

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
  • 21,063
  • 9
  • 42
  • 57
3

The following key map works like Brian's solution, But also keep the cursor position related to the word.

" tap indent movement (use mark `m' for cursor position)
vmap <S-Tab>  mm<`m:<C-U>exec "normal ".&shiftwidth."h"<CR>mmgv`m
vmap <Tab>    mm>`m:<C-U>exec "normal ".&shiftwidth."l"<CR>mmgv`m
yuxio
  • 351
  • 2
  • 6
3

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.

Srikanth
  • 11,780
  • 23
  • 72
  • 92
3

for multi-line select using cursor and press shift + < or shift + >

for single line press shift + << or shift + >>

for undo press u

cigien
  • 57,834
  • 11
  • 73
  • 112
nude
  • 31
  • 1
  • 2
1

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

chaos
  • 122,029
  • 33
  • 303
  • 309
1

absolutely the easiest way is:

v (highlight what you want tabbed)

shift + .

escape 

jb0932
  • 11
  • 3
0

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
  • 31,979
  • 7
  • 69
  • 83
0

Just add

vmap > >gv
vmap < <gv 

to virtual mode key-map .vimrc or init.vim. Now using > and < for indent.