How to jump in parentheses in VIM

I am currently trying to learn VIM and disable the arrow keys for all modes. When introducing new functions, I always opened and closed parentheses at the same time so as not to forget them. Since I cannot return (no arrow keys), I was wondering how this is done in VIM, because after closing you are outside the parentheses.

The standard practice is to exit INSERT mode, move to parentheses and enter INSERT mode again? Or do you just leave the parenthesis open until you're done?

I know about an AutoClose script. The question helps to better understand the way VIM does things, so I would like to know how other developers overcome this problem.

+7
source share
6 answers

Forgetting the closing parenthesis will cause problems with backlighting in the future, so it is unlikely that you will not notice them.

I suggest getting used to entering arguments in the middle of the parenthesis when you type and close parsers when you're done with arguments. (If you're trying to learn vim , anyway, this change doesn't seem too big to adapt to it - the amount of other things you have to adapt is already pretty overwhelming.)

+5
source

The idea of โ€‹โ€‹the modal nature of Vim is that you spend most of your time in normal mode, and not in insert mode. You enter paste mode when you want to paste text, and whenever you want to do something that does not paste raw text, you return to normal mode.

In any case, if for some reason you do not want to leave the insert mode, you can press CTRL+o to run one command of the normal mode. But I recommend getting used to jumping between modes - switching modes should be second nature to Vim users.

+2
source

Question: The standard practice is to exit INSERT mode, go to the parenthesis and enter INSERT mode again? Or do you just leave the parenthesis open until you're done?

Answer: It would be common to just type the arguments of the function, and then enter the closing parentheses. But yes, if you have already typed ) , and then decided that you want to return to the character, most likely just press the Escape key, and then press i to insert before the current position. With only two keystrokes, and experienced vi users are quite used to keystroke Escape.

If you really typed ); , you need to press the Escape key, then h and i . If you do, you will get used to it.

Some key combinations become second nature. For example, you can transfer two characters to vi by pressing x to delete the character under the cursor and place the cursor on the next character; then p to insert this character after the cursor position. I typed xp enough time in my life that I donโ€™t even think about it ... my fingers just know where these keys are. I just think, โ€œOh, I need to change these characters,โ€ and my fingers will rip out xp .

+2
source

First of all, in command mode % move you to the corresponding brackets, brackets or parentheses.

To answer your question: yes, people who immediately close their parsers, curly braces, parentheses, etc ... most likely exit the command mode and then re-insert the paste mode to return to editing.

For me, this is not even what I notice. It will be for you very soon.

When I start a new if statement, I immediately open and close the curly braces. After adding the closing bracket, I press ESC O to open the line above my closing bracket (or ESC k i ). Then I type again.

It will never hit me as an inconvenience or a problem. All this will be for you at a second glance soon enough ...

Also, you wonโ€™t even think about these arrow keys much more ... You just use h to return (or something better).

+1
source

You do not need to stop using the arrow keys. This is a stupid idea.

hjkl "better" than the arrow keys only if you touch the type because it is on the home line. If you donโ€™t touch the type, your hands already make many journeys in a natural way and reach for the arrow keys. If you do not touch the type, there is no home line.

However, your problem is not a hjkl vs arrows problem. You can solve this by getting used to typing only the initial bracket, then the arguments, then the closing bracket or using the "auto-close" mechanism.

You can do it naively by adding this line to your ~/.vimrc :

 inoremap ( ()<Co>h 

or use one of the many available plugins .

0
source

One option is to use a key map to jump to the opening bracket / bracket. I kind of close the brackets / parentheses right away to make sure they are balanced (although vim will angrily tell me that I missed them, if that happens - the power of habit). Throw this mapping into your vimrc:

imap <CSh> <esc> :call search("[{[(]", "bes")<CR> i

Here's a breakdown of the command:

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

Therefore, I can print foo(); , then <CTRL-SHIFT-h> to return to the parenthesis to provide arguments.

A little connected: looking at the vim-surround plugin , this is a performance accelerator as soon as you hang it!

0
source

All Articles