16

Say I have this code:

var users = [
  { name: 'TJ', email: '[email protected]' },
  { name: 'Loki', email: '[email protected]' },
  { name: 'Jane', email: '[email protected]' }
  ];

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

var users = [
    { name: 'TJ', email: '[email protected]' },
    { name: 'Loki', email: '[email protected]' },
    { name: 'Jane', email: '[email protected]' }
  ];

How can I accomplish that in vim?

0

4 Answers 4

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.

5
  • in visual mode, you need a single >. Also, in block visual capital A and I are better than s or c if you need to append/insert and not replace.
    – Benoit
    Oct 27, 2011 at 8:13
  • Correct on both accounts. The first one, I fixed, the second left as is, because what I've written is not wrong :) But thanks. Oct 27, 2011 at 8:27
  • Try also %vi[k> Normal mode command (assuming that the cursor is on the var users-line).
    – ib.
    Oct 27, 2011 at 8:59
  • ib, that would sure do, but this is, basically a macro to record ;-) Oct 27, 2011 at 9:06
  • 2
    I had to use set sw=2 first to get the two spaces, in case anyone didn't know how to set the space width :) Feb 21, 2014 at 12:59
10

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>
1
  • 1
    This did exactly what I needed. It bumps every line to the right X number of spaces. Thanks. Sep 6, 2017 at 18:04
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)

2
  • The numbers 2 and 5 are just examples btw. You need to change them :) Oct 27, 2011 at 8:19
  • 5
    That is not indenting. It will mess up existing levels of indentation in the selected text. Jan 15, 2014 at 14:41
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.