Search code examples
coding-styletyping

How to minimize use of arrow keys when typing code?


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.


Solution

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