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

Vimscript pattern mapping


May 24, 2021 Vim


Table of contents


In the last chapter we talked about how to map keys in Vim. T he command map use works innormal mode. If you've tossed yourself before you read this chapter, you might notice that this mapping works in visual mode.

You can use nmap vmap imap to specify that the map is only valid in normal, visual, and insert modes.

Do the following command:

:nmap \ dd

In normal mode, press \ Vim deletes the current row.

Now go into Visual mode and press \ Nothing will happen because we told Vim that this mapping only works in normal mode \ the default behavior of the s is nothing).

Run the following command:

:vmap \ U

Go into visual mode and select some text and \ Vim will convert Chinese selection into capital format.

Test the buttons in normal mode and visual mode \ paying attention to the effects in different modes.

Enhances memory

At first, mapping the same keys to different features depending on the current pattern sounds scary. W hy stop every time you press that key and think about what mode we're in now? Would that be a waste of time?

In practice we find that it's really not a problem. O nce you use Vim a lot, you won't care what key you press. Y ou'll want to "delete a line" instead of "press 'dd". Your fingers and brain will remember your mapping, and subconsciously you will press those mapping buttons.

insert mode

Now we know how to map keys in Normal mode and visual mode. N ow let's talk about mapping methods in the insert mode. Run the following command:

:imap <c-d> dd

You might guess that the effect of this command is to delete the entire line Ctrl+d This mapping is useful because you don't have to cut back to normal mode every time to delete certain rows.

Okay, let's give it a try. I t doesn't work as we think it does, it just adds two d file! It doesn't work at all.

The problem is that Vim only does what we say. I n this example, we say, "When I press <c-d> it's the equivalent of I press d And when you're in insert mode, the role of d is to enter two d

For this mapping to perform as we expect, we need clearer instructions. Modify the map and run the following command:

:imap <c-d> <esc>dd

<esc> button to exit ininsert mode.

Try this mapping again now. I t works, but pay attention to how you get back to normal mode. This is because we told <c-d> exit insert mode and delete a line, but we didn't tell it to go back to insert mode.

Run the following command to fix the mapping issue:

:imap <c-d> <esc>ddi

The i end tells Vim to go into insert mode, and then our mapping is finally complete.

Practice

Set a map, and when you're in ininsert mode, you can convert the word in which the current cursor is located into capital format by pressing <c-u> E very time I write a MAX_CONNECTIONS_ALLOWED this, I feel the usefulness of this mapping. Because that way I can write the constant in a small-case format and use this mapping to turn it into capital, without having to press the shift key all the time.

Add that map to your ~/.vimrc file.

Set a map, and when you're in normal mode, you can convert the word in which the current cursor is located into capital format by pressing <c-u> This mapping is a little different from the one above, because you have to go intonormal mode and you don't need to end up cutting into insert mode.

Add that map to your ~/.vimrc file.