16

Say I have this code:

var users = [
  { name: 'TJ', email: 'tj@earnboost.com' },
  { name: 'Loki', email: 'loki@earnboost.com' },
  { name: 'Jane', email: 'jane@earnboost.com' }
  ];

I want to select the three lines inside the variable (in visual mode) and indent it two spaces:

var users = [
    { name: 'TJ', email: 'tj@earnboost.com' },
    { name: 'Loki', email: 'loki@earnboost.com' },
    { name: 'Jane', email: 'jane@earnboost.com' }
  ];

How can I accomplish that in vim?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
alexchenco
  • 53,565
  • 76
  • 241
  • 413

4 Answers4

17

Provided your sw is two, you can do visually select three lines (V) and do a >. You can also select column of one space and three lines (ctrl-v), hit s and replace the said space with three spaces, then hit esc and all three lines will benefit from this improvements. And there are more ways I'm sure. Normally, you'd do the > and have your sw set to whatever indentation you want in your code.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
9

after you select lines in visual mode. enter ":", the "'<,'>" will auto added, then enter norm I and two space(space key).

:'<,'>norm I<space><space>
LF00
  • 27,015
  • 29
  • 156
  • 295
5

Youre friend here are :le and :ri:

:[range]ri[ght] [width]                                 :ri :right  

     Right-align lines in [range] at [width] columns
     (default 'textwidth' or 80 when 'textwidth' is 0). 
     {not in Vi}

:[range]le[ft] [indent]                                 :le :left     

       Left-align lines in [range].  Sets the indent in the        
       lines to [indent] (default 0).  {not in Vi}

Thus just visually select your lines, and then execute one of the aboves like:

:'<,'>ri 2

or

:'<,'>le 5

(Note: the '<,'> part is automatically created by VIM, it's the content that you have visually selected)

Fatih Arslan
  • 16,499
  • 9
  • 54
  • 55
0

For indenting a block between { and } (or between (), []) the way it should be indented, I use =% with the cursor on the first { (resp. (, [).

For indenting 3 lines to the right, I use 3>> with the cursor on the first line.

mouviciel
  • 66,855
  • 13
  • 106
  • 140