Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Vimscript exercise your fingers


May 24, 2021 Vim


Table of contents


This chapter will talk about how to learn Vim more effectively, but before that we need to do a little preparation.

Let's start by creating a mapping that will lighten a lot of the burden on your left hand. Execute the following command:

:inoremap jk <esc>

ok, now go into insertion mode and tap jk . Vim returns to the usual mode as if you had hit the escape button.

There are many default ways to exit insert mode in Vim:

  • <esc>
  • <c-c>
  • <c-[>

In each of these ways, you'll need to stick out your paws, which will make you feel uncomfortable. Using jk is great because these two buttons are right under your two most powerful fingers, and you don't have to move your fingers as if you were playing guitar chords.

Some people may prefer jj but I prefer jk for two reasons:

  • With two different keys, you can "roll" your finger instead of pressing the same button twice.
  • If you press jk out of habit jk you will only move the cursor down a little and then up again, and eventually the cursor will remain in its original position. But pressing jj in a common mode will only move the cursor to a different place.

Note, however, that if jk is often combined in your language (e.g. German), you may need to choose a different mapping.

Learn Map

ok, now that you have a new mapping, how will you learn to use it? Especially since you've been using the escape button for so long that you've carved it into your head so much that you'll tap it without thinking when you edit it.

The trick to re-learning a mapping is to force the previous keys to be unavailable and force yourself to use the new mapping. Execute the following command:

:inoremap <esc> <nop>

This command tells Vim to perform <nop> after tapping the escape key in insert mode, so that the escape key is invalid in insert mode. ok, and now you have to use jk this mapping to exit the insertion mode.

At first you may not get used to it, you'll still tap the escape button in insert mode and think you've exited the usual mode, and then start tapping the key to get ready to do something in the usual mode, causing some unwanted characters to appear in your text. T his may make you feel a little uncomfortable, but if you stick to it for a while, you'll be surprised to see how quickly your mind and fingers will adapt to the new mapping. In less than an hour or two you won't be tapping escape in insert mode.

This approach applies to all new mappings that replace the original mode of operation, including in life. When you want to break a bad habit, you'd better think of ways to make it hard or even impossible.

If you want to learn to cook your own meals and don't want to eat covered rice every day, you shouldn't go to Chengdu for snacks when you leave work every day. This way you'll find a way to do something to eat when you're hungry, so make sure you don't have any bubble noodles in your house.

If you want to give up smoking, don't put the cigarette on your body and put it in the car. So when your addiction is again, you'll feel like it's a pain to walk into the car and get your cigarette, so you don't smoke.

Practice

If you still use arrow keys to move cursors in Vim's common mode, map them to <nop>

If you also use the arrow keys in edit mode, similarly, map to <nop>

The key to using Vim correctly is to be able to move quickly out of insert mode and then in common mode.