Steve M
Steve M

Reputation: 10573

Change depth of indentation in vi

What's the easiest way to increase or decrease indentation for a large block of code in vi?

Upvotes: 13

Views: 7039

Answers (11)

Markus Jarderot
Markus Jarderot

Reputation: 89171

By default in insert mode, you can use Ctrl-T and Ctrl-D to de-/intent the current line.

Upvotes: 2

kjohri
kjohri

Reputation: 1224

First find the line numbers of the start and end lines using the Control-g command. Let these be m and n. Suppose we wish to indent line numbers m through n by 5 spaces. Then, the following command does the job,

:m,ns/^/     /g 

To decrease the indentation, first convert all tabs to (say 4) spaces.

:m,ns/\t/    /g

Then let's say we wish to remove 3 spaces from lines numbered m through n.

:m,ns/^   //g

Upvotes: 0

Doug Royal
Doug Royal

Reputation:

:50,100>

Will indent lines 50 through 100 once

:50,100>>

Will indent lines 50 through 100 twice

also works with < and << etc.

Upvotes: 0

Jamie Love
Jamie Love

Reputation: 5626

Also, you can look at http://vim.wikia.com/ for tips on this sort of thing.

Upvotes: 2

Ana Betts
Ana Betts

Reputation: 74654

Another useful command is, once you indent using '<' or '>', use '.' to repeat the command until it's lined up how you want it.

Upvotes: 2

luke
luke

Reputation: 14788

hit v to go into visual mode and arrow down so the whole block is selected then 12>>

Upvotes: 1

Christian C. Salvad&#243;
Christian C. Salvad&#243;

Reputation: 827306

Autoidenting:

For a { } block I use the command: =iB (with the cursor inside the block to ident)

For re-identing a complete file, I use gg=G

Now for increase or decrease identation on a block, you have to select it (I use viB command) and then you do >> or << and if you want to repeat the identation just use the dot .

Also remember to set your identation settings with

:set shiftwidth=NUMOFSPACES

and

:set softtabstop=NUMOFSPACES

Upvotes: 9

Harley Holcombe
Harley Holcombe

Reputation: 181770

In vim (not sure if this applies to you too), you use >> to indent one line. As with nearly every command in vim, type in a number before the command to perform is multiple times. So to indent the next 50 lines, type 50>>.

Upvotes: 14

dkretz
dkretz

Reputation: 37645

use two angle-brackets ("<<" or ">>") for one line left or right by shiftwidth characters. You can do this with the common line-range indicators - ":m,n", brace/bracket/paren matching, etc.

Or "<", then a motion indicator, then another "<".

Or "<12<" shifts the next 12 lines left.

Upvotes: 0

Greg Hewgill
Greg Hewgill

Reputation: 993005

If your code is between curly braces, then put your cursor on one of the curly braces and use >% or <%.

Upvotes: 3

Svante
Svante

Reputation: 51501

Mark it, then use > and <.

Upvotes: 3

Related Questions