vehomzzz
vehomzzz

Reputation: 44578

How to tab (back, forward) a block of code in Vim?

How do you tab a block of code, to the right to the left, on Vim?

Upvotes: 30

Views: 29923

Answers (8)

Pythoneer
Pythoneer

Reputation: 11

(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

Upvotes: 1

Fragsworth
Fragsworth

Reputation: 35497

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

Upvotes: 74

Mia Altieri
Mia Altieri

Reputation: 39

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)

Upvotes: 0

Nivir
Nivir

Reputation: 31178

Just go in visual mode typing v and then use the < or > character :)

Upvotes: 0

csexton
csexton

Reputation: 24783

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

Upvotes: 5

Jordan Parmer
Jordan Parmer

Reputation: 37174

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

Upvotes: 5

OscarRyz
OscarRyz

Reputation: 199215

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"

Upvotes: 1

dcruz
dcruz

Reputation: 1094

The page "Indenting source code" should give you all the information you need.

Upvotes: 6

Related Questions