Reputation: 6214
I need to enclose a block of code with a for loop. As this is Python I need to take care of indenting and increment the number of tabs by one. Any easy way to do this in Vim?
Upvotes: 5
Views: 472
Reputation: 3551
As well as the other excellent answers here, I would suggest adding this to your ~/.vimrc
file:
vnoremap < <gv
vnoremap > >gv
This will make it so that when you select text in visual mode (press v) and then press > or < it won't drop you out of visual mode.
The following lines will also make vim try to auto-indent lines for you which can be useful:
set autoindent
set smartindent
Although you will need to disable this when you paste text in or it will try to indent it.
You can turn paste mode on/off with a key, mapped like this:
set pastetoggle=<F6>
Now F6 will toggle paste mode to allow you to paste text in without the auto-indent screwing it up.
Upvotes: 0
Reputation: 12803
The fastest way you can try is v i p > from inside the block of code you want to indent. That wraps Visual mode Inside Paragraph, and > indents the selected code.
Upvotes: 0
Reputation: 5135
Try hitting V
for visual line mode, select the area you want to indent, and hit >
. Other motions besides V
are good, too.
Upvotes: 3
Reputation: 17343
You can manually adjust indentation with < and >, and == will auto-indent a block of code.
Also, Indenting Python with Vim might be of help for getting some more advanced auto-indentation.
Lastly, ]p is a handy way to insert a yanked block of code, indenting it to the proper level (try yanking your block, moving the cursor to the start of your for loop, then pressing ]p).
Upvotes: 3