3

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?

1
  • Just a tip . will repeat your last change.
    – tidbeck
    Mar 6, 2012 at 10:08

4 Answers 4

8

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.

4
  • That's great too, too bad I can't select two different answers haha. Can't up vote yet either =( Thank you
    – flumpb
    Dec 24, 2010 at 3:43
  • 1
    This is way easier and more intuitive in my opinion, +1. Dec 24, 2010 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. Dec 24, 2010 at 3:46
  • We all helped, so I don't mind. :-)
    – Mikel
    Dec 24, 2010 at 3:50
2

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

2
  • 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
    – flumpb
    Dec 24, 2010 at 3:28
  • ^ denotes the beginning of the line when not used in a character range. Dec 24, 2010 at 3:34
0

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.

0

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.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .