Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change depth of indentation in vi

Tags:

vi

editing

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

like image 672
Steve M Avatar asked Oct 31 '08 01:10

Steve M


People also ask

How do I indent 4 spaces in Vim?

The shiftwidth parameter controls your indentation size; if you want four space indents, use :set shiftwidth=4 , or the abbreviation :set sw=4 .

How do I change the indentation level of the block of code in Vim?

In normal mode, press Tab or Shift-Tab to adjust the indent on the current line and position the cursor on the first nonblank character; in insert mode, press Shift-Tab to unindent; in visual mode, press Tab or Shift-Tab to adjust the indent on selected lines.


2 Answers

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

like image 161
Harley Holcombe Avatar answered Oct 13 '22 12:10

Harley Holcombe


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

like image 38
Christian C. Salvadó Avatar answered Oct 13 '22 13:10

Christian C. Salvadó