10

I'm trying to get into the often recommended habit of not using the arrow keys but only the hjkl ones. Now I often find myself in the following situation. I have just typed

someFunction()

and the cursor is in insert mode in between the ( and ). Next I only need to type ;

someFunction();

and press esc to finish. Pressing on the right arrow key is probably easiest but is starting to feel awkward and too much time needed to complete the action. Plus some people disable their arrow keys to get into the habit of not using them. Is there something faster than hitting esc and shift-A or is the right arrow key fastest in this case?

Kache
  • 15,647
  • 12
  • 51
  • 79
koen
  • 13,349
  • 10
  • 46
  • 51

5 Answers5

15

I would argue that Control-oa is faster than escshift-A and the right arrow key. Control-o lets you execute one normal mode keystroke from insert mode.

Zach
  • 4,652
  • 18
  • 22
9

Why would your cursor be in between the ( and )? Shouldn't it be after the ) if you typed it?

If it was automatically closed via the vim plugin delimitMate, what you should be doing is typing the ) even though the plugin auto-completed for you. The plugin will notice that you're closing the parenthesis pair and just move your cursor after the auto-closed ).

Kache
  • 15,647
  • 12
  • 51
  • 79
  • I got several good suggestions. I choose this one because it seems the easiest but will try the others also to get a feel of it and learn more. – koen Aug 13 '14 at 17:14
3

Autoclosing doesn't save key presses so, if you don't plan to put anything (at least right now) between them, don't autoclose your parentheses.

f       1    f        1    f      1    f      1
fo      2    fo       2    fo     2    fo     2
foo     3    foo      3    foo    3    foo    3
foo(    4    foo()    4    foo()  4    foo()  4
foo()   5    <right>  5    <Esc>  5    <C-o>  5
foo();  6    ;        6    A      6    l      6
                           ;      7    ;      7
romainl
  • 186,200
  • 21
  • 280
  • 313
  • That's something to think about. I like the facts presented. I'll try to feel how it works in practice. – koen Aug 13 '14 at 17:12
3

In this situation you describe the absolute fastest way is as you discovered the <right> key in insert mode. This method does go against the Vim Way and breaks following Vim features:

  • Redo or the . command. I find it best to usually alter your workflow to make sure the . command works. (See :h .)
  • Vim has a chunky undo because of its modes. This means that each chunk is typically one action and makes navigation between undo states a bit easier. Using <right> will break undo in many cases

Vimmers switch modes so often that it becomes second nature to switch between them. Switching out of insert to normal mode often is typically a good thing as insert mode is meant for short bursts.

A few parting thoughts:

  • Learn to love <esc>
  • Insert mode is for short burst of text
  • Normal mode is called normal mode because that is the mode you are normally in
  • Your problem with Vim is that you don't grok vi.
  • If this is a common problem then make a mapping or abbreviation
  • Tim Pope's Surround.vim plugin or some kind of auto pair plugin may help with your parenthesis
  • Work with Vim not against it. aka: Sharpen the saw
Community
  • 1
  • 1
Peter Rincker
  • 43,539
  • 9
  • 74
  • 101
2

Speaking about trailing semicolon on the end of lines.. Probably people will hate me for this, nevertheless I'll leave it here:

imap ;; <Esc>A;
map ;; A;<Esc>
Daniel Khoroshko
  • 2,623
  • 15
  • 26