Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've been coding in Java for a job recently and I've noticed that I'll write some code and then determine that I need to wrap the code in a try/catch block.

I've just been moving to the beginning of a line and adding a tab.

0 i <tab> <esc> k (repeat process until at beginning or end of block)

Now this was fine the first three or four times I had to indent but now it's just become tedious and I'm a lazy person.

Is there an easier way I could deal with this problem?

share|improve this question
    
Just a tip . will repeat your last change. –  tidbeck Mar 6 '12 at 10:08

4 Answers 4

up vote 8 down vote accepted

Another option is to select the lines using Shift+V as Ignacio suggests, but then press > (greater than sign). That does an indent according to your indentation settings (shiftwidth, expandtab, etc).

You can also indent more than one level with number>, e.g. 2> to indent two levels.

Finally, if Vim recognizes the file type (type :set filetype?, it should print filetype=java), then you can select any code as above and press =, and it will fix up the indentation using the rules for that type of file.

share|improve this answer
    
That's great too, too bad I can't select two different answers haha. Can't up vote yet either =( Thank you –  kisplit Dec 24 '10 at 3:43
1  
This is way easier and more intuitive in my opinion, +1. –  Sasha Chedygov Dec 24 '10 at 3:44
    
@Mack: Welcome to Super User. :) Generally, to avoid this, we recommend waiting a day or two before accepting an answer, because that way, an even better answer might come up and you can accept that one. Otherwise, there's less motivation to post an answer (since you already accepted one), and if someone does post one, they won't be rewarded as much for it even if it's clearly better. –  Sasha Chedygov Dec 24 '10 at 3:46
    
We all helped, so I don't mind. :-) –  Mikel Dec 24 '10 at 3:50

Use visual (V) or visual line (ShiftV) mode to select the lines to indent, press :, then enter s/^/CtrlVTab/.

share|improve this answer
    
Thank you so much that works like a charm. =) I haven't used visual mode before. Does ^ denote a wildcard? Heh, have to wait 1 more minute before I can accept as answer –  kisplit Dec 24 '10 at 3:28
    
^ denotes the beginning of the line when not used in a character range. –  Ignacio Vazquez-Abrams Dec 24 '10 at 3:34
    
Thanks again for the help –  kisplit Dec 24 '10 at 3:42

Another way:
CTRL+v (this starts the visual block mode)
xj or xk (where x represents the number of lines down or up)
> (aka shift+.)

This isn't very different from the answers above, but introduces the block mode, which is really nice for editing multiple rows.

share|improve this answer

Vim is much more intelligent than what the other answers would let you think: it is able to automatically reindent a zone of code. Typically, from within a try block, you'll just have to type =i{ -- actually this will work from any curly-brackets block. And that's all ... as long as your indent options (cindent, expandtab, sw) are correctly set for your needs and tastes.

The next step would be to use a mapping that knows how to surround any set of selected lines with your try block (and that doesn't forget to reindent at the end of the process). I have such a thing for C++, it would not be to complex to adapt it to java. (It just requires two other library plugins: lh-vim-lib and lh-map-tools.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.