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

Vimscript Leaders


May 24, 2021 Vim


Table of contents


We've learned a keyboard mapping method that doesn't drive us crazy, but you can notice another problem.

Every time we map a button like :nnoremap <space> dd and space, we <space> override the original functionality of the . What if one day we want to <space>

Some keys you don't normally use are not required. Y ou'll almost never use - keys - , H L <space> <cr> <bs> and, of course, in normal mode). Depending on how you work, there may be other keys you won't use.

These keys can be mapped at will, but only these six buttons do not seem to be enough. Is there a problem with the customizable legend that Vim claims?

Map the key sequence

Unlike Emacs, Vim can map multiple keys. Run the following command:

:nnoremap -d dd
:nnoremap -c ddO

Quick read-in -d or -c in norma mode to see the effect. The first mapping function is to delete a row, and the second is to delete a row and enter ininsert mode.

This means that you can use a key you don't use - such as - , as a prefix, followed by other characters as a whole. You need to press one more button to perform these mappings, just one more button, and it's easy to remember.

If you think this is a good approach, I can tell you that Vim already supports this mechanism.

Leader

We call this "prefix" "leader". Y ou can set your leader key to your preference. Run the command:

:let mapleader = "-"

You can replace - for the keys you like. Although a useful feature is blocked, my personal use , because this key is easier to press.

When you create a new map, you can use <leader> button I set" to indicate . Run the command:

:nnoremap <leader>d dd

Now try pressing your leader button and d . Vim deletes the current row.

But why do you have to make tedious settings every <leader> W hy don't you just tap your prefix button when you create a map? There are three main reasons.

First, you may one day want to replace your "leader". Defining it in one place makes it easier to replace it.

Second, when others look at ~/.vimrc file, they will know what you mean as soon as they see it. <leader> If they like ~/.vimrc configuration, they can simply copy your mapping configuration even if they use a different leader.

Finally, many Vim plug-ins <leader> with . If you've set up leader, you'll be more likely to get started with those plug-ins.

Local Leader

Vim has another "leader" that becomes "local leader". This leader is used for mapping that is set only for certain types of files (such as Python files, HTML files).

This book will cover how to create a map for a particular type of file in a later section, but you can now create a "localleader":

:let maplocalleader = "\\"

Note that we \\ \ of the \ because the character is escaped in Vimscript. We'll cover this in a later chapter.

Now you can use <localleader> method in the map <leader> course, you'll want to use another prefix).

If you don't like the backslash, feel free to change it.

Practice

Read :help mapleader .

Read :help maplocalleader .

Set up mapleader your maplocalleader ~/.vimrc

Add <leader> mapping command that you added to the ~/.vimrc in the previous section to prevent those maps from overwriting the default keystrokes.