How to minimize use of arrow keys when typing code

2019-03-24 14:09发布

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.

11条回答
相关推荐>>
2楼-- · 2019-03-24 14:50

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.

ゆ 、 Hurt°
3楼-- · 2019-03-24 14:54

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.

查看更多
祖国的老花朵
4楼-- · 2019-03-24 14:54

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.

孤傲高冷的网名
5楼-- · 2019-03-24 14:54

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.

SAY GOODBYE
6楼-- · 2019-03-24 14:57

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

查看更多
放我归山
7楼-- · 2019-03-24 14:58

Try AutoHotKey and my script:

*!I::SendInput, % (GetKeyState("Shift", "P") ? "+" : "") (GetKeyState("Ctrl", "P") ? "^" : "") "{Up}"
*!K::SendInput, % (GetKeyState("Shift", "P") ? "+" : "") (GetKeyState("Ctrl", "P") ? "^" : "") "{Down}"
*!J::SendInput, % (GetKeyState("Shift", "P") ? "+" : "") (GetKeyState("Ctrl", "P") ? "^" : "") "{Left}"
*!L::SendInput, % (GetKeyState("Shift", "P") ? "+" : "") (GetKeyState("Ctrl", "P") ? "^" : "") "{Right}"
*!U::SendInput, % (GetKeyState("Shift", "P") ? "+" : "") (GetKeyState("Ctrl", "P") ? "^" : "") "{Home}"
*!O::SendInput, % (GetKeyState("Shift", "P") ? "+" : "") (GetKeyState("Ctrl", "P") ? "^" : "") "{End}"
*!h::SendInput, % (GetKeyState("Shift", "P") ? "+" : "") (GetKeyState("Ctrl", "P") ? "^" : "") "{Del}"

LAlt & Shift:: ('optional line')

It's about holding LAlt + pressing something from: i, k,j, l (arrow keys), u, o (home, end) or h (delete). Last line is optional if you don't want to change keyboard lang. layout by LAlt +Shift

You can use it even in combination with modificators like shift and ctrl.

enjoy ;)

查看更多
登录 后发表回答