views:

414

answers:

10

When typing code, I would normally close brackets, go back inside, go outside, type semicolon, etc:

I might start with (| is the caret):

System.out.println()|

Then go left:

System.out.println(|)

Then this:

System.out.println(foo()|)

Again backtracking a space:

System.out.println(foo(|))

Typing quotes are similar:

System.out.println(foo(""|))

...etc.

My right hand is constantly moving between the home row and the arrow keys. I've tried vim and although I know the basics, it still feels very awkward to me.

How should I do this? Should I just type from left to right (opening bracket, then contents, then closing brackets, then semicolon)?

Thanks.

+5  A: 

If you're already in vim, try playing around with the h,j,k, and l keys. They do the same thing as the arrow keys but are much more convenient. Trying to get in the habit of typing in order would probably also help, but that's something that takes some effort.

Ben Hughes
I was just about to suggest the same. :-)
Alan Haggai Alavi
That's correct, but maybe what you gain with the right hand you lose with the other hand, by changing and changing modes.
Eliseo Ocampos
at least it only involves changing modes -- not contorting your hand to hit keys like escape meta alternate control shift to do anything.
Ben Hughes
The good part of switching modes is that you can hit the ESC key without even looking at the keyboard (it is hard to miss up there in the corner) and just go back to the regular position. In vi, what the user request can be achieved with ESC (go out of edit) i (insert before the current position), which is a keypress with the left hand and another with the right hand, in keys you don't need to visually seek for.
David Rodríguez - dribeas
+2  A: 

I would totally recommend vim... as it will help a lot of this! Also, look into something that will auto-close your parenthesis, square brackets, and curly brackets for you... I have something in vim that does this and it helps with this type of problem because I'm already inside the parenthesis.

Boushley
+5  A: 

Well, that's Java, If you use a more or less good IDE you should be able to autocomplete, that way when you type "System.out.println" and hit enter to accept autocomplete, the brackets will show up and the caret will be in the middle (oh, and there will be the quotes too!)

Eliseo Ocampos
+4  A: 

Don't.

Your habit of ending something that you started - whether it be the closing parenthesis, bracket, brace, or the .Close() call to match .Open(), or delete/free call to match your new/malloc - is an excellent one. Even if you intend to "close" your object in a different scope (like a terminate function), your habit forces you to think about properly releasing resources.

Yes, there are helpful editors out that allow you to code faster, which you should definitely use, but don't let them become a crutch that allow you to forget to close objects/release resources.

The direct answer to your question: Most good programmer editors can be customized/configured, so you'll just have to do some reading about advanced configuration of the editor of your choice - vim, emacs, the Visual Studio editor.

Uh, what? Not sure what you meant
unknown
+2  A: 

You can save keystrokes by holding the Ctrl key and using the arrow keys. Instead of moving one character, it moves one word at a time. This also works when backspacing. So Ctrl-Backspace will delete the entire word instead of just the last character.

ReadySquid
+1  A: 

Another vote for Vim. Also, there are some great plugins for more standard IDEs that use Vi keybindings. I use jVI in Netbeans from time to time.

You'll find that the more you use Vim, the easier it is on your wrists. You'll also find that a sufficiently clever find/replace can save you quite a few keystrokes, as can a global action regex-y thing.

Bind :tabn and :tabp to something accessible like and and force yourself to get stuff done without giving up and using a proper GUI editor.

C. Alan Zoppa
+1  A: 

I used to type completely linearly (yes, in vim), never could get the hang of the dashing back and forth that writing closing elements immediately created.

However, I now use Eclipse - it creates them for me as I go, so at the end of something with a )")) mess I just hit end and type a ;, no need to deal with it manually at all. Which sometimes confuses me, but that's ok.

Kim Reece
+1  A: 

I find the number pad makes navigation very easy because the home and pgup keys are so close. For actually typing numbers you just use the top row of the keyboard (which is difficult to learn I agree but sufficiently fast after a while).

The only downsides of this for me are using laptop keyboards and using other people's machines where I have to turn off num lock every time.

+6  A: 

First and foremost, there is much speed to be gained in Vim by using h, j, k and l instead of the arrow keys. See Learning Vim the Pragmatic Way for a overview of the keys.

However, what you probably want in this case is the AutoClose plugin. It automatically inserts the closing parenthesis (or quote) along with the opening, and places the caret between them. Thus you go from

System.out.println(|)

to

System.out.println(foo(|))

to

System.out.println(foo("|"))

If you then type ")), the caret will "move over" the closing characters instead of inserting new ones. Although, a faster way to get to the end of line is probably <Esc>A.

System.out.println(foo(""));

So, to sum up, the above can be produced by typing System.out.println(foo("<Esc>A;.

For editing paired characters, as opposed to inserting them, see surround.vim.

Marius Andersen
+1. If you want to stay with Vim I think this a good option (Thanks for the plugin Marius!)
Eliseo Ocampos
+1  A: 

A good IDE (galileo is almost here) will auto close brackets, parentheses, etc, and will intelligently insert a semicolon at the end of the statement too. No need to use arrows at all!

Of course for a println in Eclipse you can just type sysout but that's probably a bad habit to get into.

But be careful! If you get too quick your colleagues will always make you drive :P

CurtainDog