ChatGPT解决这个技术问题 Extra ChatGPT

What is the <leader> in a .vimrc file?

I see <leader> in many .vimrc files, and I am wondering what does it mean?

What is it used for?

Just a general overview of the purpose and usage would be great.

I'm coming to realize that I think the key understanding is that by using <leader> in your keyboard shortcuts you are effectively creating a namespace so that your custom shortcuts don't step on built-in vim behavior. See @Pete Schlette's answer below for more.
If you want to check what your <leader> is, use :echo mapleader or :let mapleader. If undefined, then it will use the default, which is a backslash "\"

F
Fredrick Brennan

The <Leader> key is mapped to \ by default. So if you have a map of <Leader>t, you can execute it by default with \+t. For more detail or re-assigning it using the mapleader variable, see

:help leader

To define a mapping which uses the "mapleader" variable, the special string
"<Leader>" can be used.  It is replaced with the string value of "mapleader".
If "mapleader" is not set or empty, a backslash is used instead.  
Example:
    :map <Leader>A  oanother line <Esc>
Works like:
    :map \A  oanother line <Esc>
But after:
    :let mapleader = ","
It works like:
    :map ,A  oanother line <Esc>

Note that the value of "mapleader" is used at the moment the mapping is
defined.  Changing "mapleader" after that has no effect for already defined
mappings.

The change to , is a good one. Much easier to reach than \\ , and who uses , in vim anyway?
@Gabe Moothart. :h , gives you "Repeat latest f, t, F or T in opposite direction [count] times." It is quite convenient.
I'm liking space (' ') as a leader key because I can hit it with either hand, making the follow up key (whether it's on the left or right side of the keyboard) equally quick to trigger afterward. Having a leader key that itself is only on one side of the keyboard makes hitting follow up keys on the same side of the keyboard feel slightly more cumbersome. Nitpicky, I know. :)
@jefflunt My right thumb is already week because I've never used it for typing. So I only use left thumb for hitting spaces. That space leader key does not provide the same benefit to me, I guess.
j
jasonleonhard

Be aware that when you do press your <leader> key you have only 1000ms or 1 second (by default) to enter the command following it.

This is exacerbated because there is no visual feedback (by default) that you have pressed your <leader> key and vim is awaiting the command; and so there is also no visual way to know when this time out has happened.

If you add set showcmd to your vimrc then you will see your <leader> key appear in the bottom right hand corner of vim (to the left of the cursor location) and perhaps more importantly you will see it disappear when the time out happens.

The length of the timeout can also be set in your vimrc, see :help timeoutlen for more information.


« :set showcmd » shows the current command hence the leader key the time it is active.
To the lack of feedback, while by default there is no visual feedback, it appears that vim will sound a system bell if it doesn't receive a command in the 1000ms after you press <leader>.
M
Mikeage

The "Leader key" is a way of extending the power of VIM's shortcuts by using sequences of keys to perform a command. The default leader key is backslash. Therefore, if you have a map of Q, you can perform that action by typing \Q.


@ArnoldRoa Whenever vim commands are listed, the syntax ggdG means to press those four keys sequentially. A syntax like <C-w><C-j> means to press CTRL+w, followed by CTRL+j.
P
Pete Schlette

Vim's <leader> key is a way of creating a namespace for commands you want to define. Vim already maps most keys and combinations of Ctrl + (some key), so <leader>(some key) is where you (or plugins) can add custom behavior.

For example, if you find yourself frequently deleting exactly 3 words and 7 characters, you might find it convenient to map a command via nmap <leader>d 3dw7x so that pressing the leader key followed by d will delete 3 words and 7 characters. Because it uses the leader key as a prefix, you can be (relatively) assured that you're not stomping on any pre-existing behavior.

The default key for <leader> is \, but you can use the command :let mapleader = "," to remap it to another key (, in this case).

Usevim's page on the leader key has more information.


U
UnrealApex

The default leader key is backslash(\). It's used in bindings as a chord with other keystrokes.