8

Currently, I'm trying to learn VIM and disabled the arrow keys for all modes. When typing out new functions I always used to open and close the parenthesis at the same time in order to not forget them. Since, I cannot go back (no arrow keys) I was wondering how it is done in VIM because once closed, you are outside of the parenthesis.

Is it standard practice to exit INSERT mode, move into the parenthesis and enter INSERT mode again? Or do you simply leave the parenthesis open until you are finished?

I do know about the AutoClose script. The question serves more to understand the VIM way of doing things so I would like to know how other developers are overcoming this problem.

3
  • 4
    Are you talking about % or f)? Commented Jun 12, 2012 at 1:44
  • No, I actually mean how I get back into the bracket once it is closed (); //cursor is here now. In other editors you just use the arrow keys, but I disabled mine to get used to the VIM modes. Commented Jun 12, 2012 at 1:49
  • 1
    After a couple more days of using vim, you won't even notice entering and exiting modes. The point of vim is to make everything you do to manipulate text second nature. It won't take long, young jedi. Commented Jun 12, 2012 at 2:04

6 Answers 6

7

Forgetting a closing parenthesis will cause highlighting problems later on, making it unlikely that you'd overlook them further.

I suggest getting in the habit of typing the arguments in the middle of the parenthesis as you're typing and close the parens once you're finished with the arguments. (If you're trying to learn vim, anyway, this change doesn't seem too large to adapt to -- the amount of other things you'll have to adapt to is already pretty staggering.)

Sign up to request clarification or add additional context in comments.

Comments

4

First of all, in command mode % moves you to the corresponding parenthesis, brace, or bracket.

To answer your question: yes, people who immediately close their parens, braces, brackets, etc... most likely exit command mode then re-enter insert mode to return to editing.

To me, this isn't even something I notice. It'll be the same for you quite soon.

Whenever I start a new if statement, I immediately open and close the braces. After I add the closing brace, I hit ESCO to open the line above my closing brace (or ESCki). Then I start typing again.

This would never strike me as an inconvenience or a problem. All of this will be second nature to you soon enough...

Also you won't even think about those arrow keys much longer... You'll just use h to go back (or something better).

Comments

3

The idea behind Vim's modal nature is that you spend most of your time in normal mode, not in insert mode. You only enter insert mode when you want to insert text, and whenever you want to do something that is not inserting raw text - you go back to normal mode.

Anyways, if for some reason you don't want to leave insert mode, you can press CTRL+o to run a single normal-mode command. But I recommend to get used to jumping between modes - switching modes should be a second nature for Vim users.

Comments

2

Question: Is it standard practice to exit INSERT mode, move into the parenthesis and enter INSERT mode again? Or do you simply leave the parenthesis open until you are finished?

Answer: It would be more usual to just type the function arguments and then type the closing parenthesis. But yes, if you had already typed the ) and then you decided you wanted to go back a character, you would likely just hit the Escape key, then hit i to insert before the current position. That's only two keystrokes, and experienced vi users are pretty used to hitting the Escape key.

If you actually typed ); then you need to hit the Escape key, then h and i. If you do that a lot, you will get used to it.

Some combinations of keys become second nature. For example, you can transpose two characters in vi by hitting x, to delete the character under the cursor and makes the cursor be on the next character; then p, to paste that character after the cursor position. I have typed xp enough times in my life that I don't even think about it... my fingers just know where those keys are. I just think "Oh, I need to swap those characters" and my fingers slam out the xp.

1 Comment

Agreed... The OP won't even think about any of these things after a few more days using vim. The whole point of vi is that everything you do to manipulate text becomes second nature.
1

One option is to use a key map to jump to the opening bracket/parenthesis. I tend to close brackets/parentheses right away to insure they're balanced (even though vim will angrily let me know I've missed them should that happen 😅 -- force of habit). Throw this mapping in your vimrc:

imap <C-S-h> <esc> :call search("[{[(]", "bes")<CR> i

Here's the command breakdown:

  • map <CTRL-SHIFT-h> in edit mode to
  • <ESC> (to get back to normal mode),
  • :call search("[({[]", "bes")<CR> to search backwards for an open bracket/parenthesis and jump to it, and finally
  • i return to insert mode

So I can type foo();, then <CTRL-SHIFT-h> to jump back into the parenthesis to provide arguments.

Somewhat related: looking into the vim-surround plugin, it is a productivity booster once you get a hang of it!

Comments

0

You don't need to stop using the arrow keys at all. That's a silly idea.

hjkl is "better" than the arrow keys only if you touch type, because it's on the home row. If you don't touch type, your hands are already doing a lot of mouvements naturally and reaching for the arrow keys goes with the rest. If you don't touch type there is no home row.

Your issue is not really a hjkl vs arrows issue, though. You can solve it by getting into the habit of typing only the opening parenthesis, then the arguments, then the closing parenthesis or by using an "autoclosing" mechanism.

You could do it the naive way by adding this line to your ~/.vimrc:

inoremap ( ()<C-o>h 

or use one of the many plugins available.

2 Comments

haha, I've tried giving up the arrow keys, because that's the general recommendation for vim users, hjkl is supposed to be faster, blah blah blah. Half the time, I found myself hitting j when I really wanted h, vice-versa. The arrow keys weren't so bad after all lol.
Those who insist on ditching the arrows for hjkl are either newbies repeating things they don't understand in order to fit in, or touch-typists who forget they are a very very minority.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.