Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indenting two spaces a selection (visual mode) in vim?

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?

like image 711
alexchenco Avatar asked Oct 27 '11 07:10

alexchenco


People also ask

How do I set tab to two spaces in Vim?

To convert tabs to spaces in the currently opened file in Vim, enter the Normal mode by pressing Esc key. Now use the retab command by pressing the ':' (colon) character and Vim will convert the existing tabs to spaces.

How do I indent multiple lines in Vim?

Using Command “>”: You need to open the visual block mode first for indentation, using the “ctrl+v” shortcut. After that, press the “next” button to select the number of characters to be indented and press “j” to cover the lines to tab along, i.e., 2 to 4.

How do I indent highlighted text in Vim?

Press V then move the cursor to select a range of lines, then press = to indent the selection.


3 Answers

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.

like image 81
Michael Krelin - hacker Avatar answered Oct 06 '22 14:10

Michael Krelin - hacker


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>
like image 35
LF00 Avatar answered Oct 06 '22 13:10

LF00


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)

like image 45
Fatih Arslan Avatar answered Oct 06 '22 14:10

Fatih Arslan