Reputation: 4017
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.
Upvotes: 3
Views: 4842
Reputation: 89
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
.
Upvotes: 8
Reputation: 196896
Most (if not all) Ex commands take a range so you could just visually select the lines and do :'<,'>retab
.
Or :12,16retab
.
Or :.,+9retab
.
And so on.
See :help :retab
and :help :range
.
Upvotes: 0
Reputation: 7657
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:
Position your cursor in the first line to be indented
Enter visual mode by typing shift + v
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).
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).
Upvotes: 1
Reputation: 7689
To answer both of your questions at the same time, you can use the >
and <
operators. Since they are operators, they behave just like y
, d
, c
, and all the other operators do, so you must supply them with a motion. For example, >>
will indent the current line (with 4 spaces), and >j
will indent the current line and the line below. >G
will indent everything to the end of the buffer, etc.
Similarly, <
will unindent whatever you specify.
In your specific example, there are two different approaches I would recommend.
Use normal mode. This one kinda depends on how large your function is. If it's just the two lines, you could put your cursor on line1, and type <j>,
. (Of course, if your function has more than 2 lines, you'll need to adjust). Unfortunately you need to unindent before reindenting because otherwise you'll end up with 5 spaces, which I assume you don't want.
Use visual mode. This one is slightly less convenient unless you modify your .vimrc
because calling >
or <
in visual mode will drop your visual selection. That's why I have the following in my .vimrc
:
"Make it easier to indent a visual selection several times.
xnoremap > >gv
xnoremap < <gv
With this setup, you can visually select the lines you would like to reindent, and then do <>
. IMO, this is the best solution, and I frequently use this kind of workflow.
Upvotes: 0