How to minimize use of arrow keys when typing code?

吃可爱长大的小学妹 提交于 2019-12-04 16:35:03

问题


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.


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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




回答7:


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.




回答8:


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.




回答9:


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.




回答10:


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.




回答11:


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



来源:https://stackoverflow.com/questions/1023002/how-to-minimize-use-of-arrow-keys-when-typing-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!