Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

autoindent in vim for selected text

1) Is there anyway to autoindent/retab on a set of lines, rather than the entire file?

set shiftwidth=4
set expandtab
set tabstop=4 

has been defined in my .vimrc.

What I want is to select a bunch of lines and apply indentation only on them. This is because the file is large and I just want to clean my line of codes. By doing :retab, I would have to force the whole file to be reindented.

For example select the following code and reindent automatically

def hello(self):
 line1
 line2

to

def hello(self)
    line1
    line2

2) Is there any way that I can reverse tab? Something like Shift-Tab in Eclipse. It goes back 4 spaces.

like image 410
infoclogged Avatar asked Oct 21 '25 03:10

infoclogged


2 Answers

You can try this one...

Shift + V to enter in Visual mode

Then press j until you reach the text you want to be ident.

Finally press =, it will ident automatically your selected code.

If you want to ident the whole code just type gg=G.

like image 110
OrteipX Avatar answered Oct 24 '25 11:10

OrteipX


What I want is to select a bunch of lines and apply indentation only on them

You can apply indentation to certain lines in Visual mode. To do this follow these steps:

  1. Position your cursor in the first line to be indented

  2. Enter visual mode by typing shift + v

  3. Move down, typing j until you reach the last line to be indented (alternately you can type line # + G if you know the line number of the last line to be indented).

  4. Type >

Another solution is to do this with a regex in command-line mode:

2,5s/\v(.*)/\t\1/g

Here the line range is specified in the first two numbers of the regex (in this case from line 2 to 5).

like image 45
builder-7000 Avatar answered Oct 24 '25 11:10

builder-7000



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!