How do you tab a block of code, to the right to the left, on Vim?
-
http://stackoverflow.com/questions/442302/tabbing-selected-section-in-vim – Sam Saffron Sep 10 '09 at 21:52
-
3what do you mean up and down? – Sam Saffron Sep 10 '09 at 21:53
-
say I have a block of code and I want to move it [a few lines] down – vehomzzz Sep 10 '09 at 21:53
-
as in take the line below the block and insert it above? – Sam Saffron Sep 10 '09 at 21:54
-
yes, NOT line,but a blocks of code or block of text. In visual mode, you can select a block by Ctrl-v... and then move the block up or down to tab it to the right or tab it to the left – – vehomzzz Sep 10 '09 at 22:00
8 Answers
Short Answer:
V select lines by ↓ and then >
and for 3 tabs:
V, 3 and then >
My favorite way is to select your block of code (with [V]isual line mode normally), then press > or If you want to tab more than once, 2> or 3> to repeat it.
If you didn't tab enough (or tabbed too much) then type "gv" to reselect your selection and try again.
To move a block of code, select it with [V]isual line mode and then press "d". This is the "Cut" operation.
Then move your cursor to the place you want it to go, and press "p". This is the "Paste" operation.
You can also try auto-tabbing a block of code by selecting it with [V]isual line mode, and pressing "=".

- 2,924
- 2
- 22
- 31

- 33,919
- 27
- 84
- 97
-
9If the code didn't tab enough, you can use the dot `.` command to repeat the last identation... – Christian C. Salvadó Sep 11 '09 at 03:35
-
1
-
Sounds like a huge pain in the ass where other editors just let you high a bunch of lines and press tab... – airtonix Dec 25 '22 at 12:42
To indent the internal block containing the cursor, do: >iB
To indent the internal block including the enclosing braces, do: >aB
You can replace '>' with '<' to indent left.
To auto-indent press == (or = if you have highlighted text).

- 36,042
- 30
- 97
- 119
I use a handy remap for visual mode that allows indenting the text multiple times while keeping your text selected. Similar to how some IDEs lets you select and hit tab (or shift-tab) to indent.
Add the following to your .vimrc
" Pressing < or > will let you indent/unident selected lines
vnoremap < <gv
vnoremap > >gv
Also you can use == to have vim try and determine the correct indenting automatically. It will work on any line buy just placing the cursor there and pressing == or you can do fancy stuff like select the entire file and press == to fix all the indenting (works wonders on html generated by wysiwyg editors).

- 24,061
- 15
- 54
- 57
In command mode:
>
As any other command you could prepend the number of line you want to have it applied:
2+2+>
Will "tab" 22 lines.
Press . if you want to "re-tab"

- 196,001
- 113
- 385
- 569
(from learnbyexample.github.io)
"v"- visually select the current character, use any motion command to extend the selection
i.e. v to enter visual mode, then tab down or up with the arrow key to select however many lines, then 'shift + >' for example, to tab a whole block of code right

- 11
- 1
Place your cursor at start/end of block
Enter visual mode holding Shift+Alt
(Shift+Option
on macOS)
Use up/down arrows to select block
press >
(to add a tab) or <
(to remove a tab)

- 19
- 1
- 4