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?
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.
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.
Press V then move the cursor to select a range of lines, then press = to indent the selection.
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.
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>
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With