1177

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.

Arsen Khachaturyan
  • 6,472
  • 4
  • 32
  • 36
Bob Martens
  • 12,142
  • 3
  • 14
  • 9
  • 14
    I'm coming to realize that I think the key understanding is that by using `` 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. – User May 10 '14 at 06:05
  • 7
    If you want to check what your `` is, use `:echo mapleader` or `:let mapleader`. If undefined, then it will use the default, which is a backslash `"\"` – wisbucky Jun 26 '19 at 20:49

5 Answers5

1083

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.


Fredrick Brennan
  • 6,195
  • 2
  • 23
  • 50
Vereb
  • 12,882
  • 2
  • 22
  • 29
  • 54
    The change to `,` is a good one. Much easier to reach than `\\ `, and who uses `,` in vim anyway? – Gabe Moothart Nov 19 '09 at 16:05
  • Thanks for the help, this definitely points me in the right direction. – Bob Martens Nov 19 '09 at 16:09
  • 47
    @Gabe Moothart. :h , gives you "Repeat latest f, t, F or T in opposite direction [count] times." It is quite convenient. – Maxim Kim Nov 20 '09 at 12:34
  • 11
    There's also a a good writeup here: http://stevelosh.com/blog/2010/09/coming-home-to-vim/#using-the-leader – Sukotto May 27 '11 at 18:45
  • @sehe Yes you're right, not sure how I missed that given it's right at the top! I've deleted my comment. – KomodoDave Nov 20 '12 at 11:08
  • 76
    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 Jul 28 '13 at 16:29
  • 2
    I don't see how this answers what is the purpose of the leader or why you would want to use it. – User May 08 '14 at 01:26
  • 1
    @User It's basically a personal namespace for commands. You know won't be used by plugins or newer versions of vim. – Miles Rout Oct 14 '14 at 09:33
  • 3
    @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. – off99555 Nov 02 '16 at 21:34
  • As a touch typer, I have never used my thumb for anything other than space, so space seems to be the perfect leader key for me. – shaahiin Mar 20 '18 at 11:29
289

Be aware that when you do press your <leader> key you have only 1000ms (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.

davetapley
  • 14,759
  • 11
  • 54
  • 74
  • 12
    « :set showcmd » shows the current command hence the leader key the time it is active. – greg Sep 05 '14 at 12:32
  • 4
    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 ``. – Bailey Parker Jul 21 '15 at 12:40
128

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 <Leader>Q, you can perform that action by typing \Q.

Mikeage
  • 6,174
  • 4
  • 33
  • 52
  • 19
    @ArnoldRoa Whenever vim commands are listed, the syntax `ggdG` means to press those four keys sequentially. A syntax like `` means to press CTRL+w, followed by CTRL+j. – gobernador Jun 30 '17 at 15:24
107

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.

Pete Schlette
  • 1,698
  • 1
  • 12
  • 18
10

In my system its the \ key. it's used for commands so that you can combine it with other chars.

K.Dᴀᴠɪs
  • 9,384
  • 11
  • 31
  • 39
Arkaitz Jimenez
  • 20,424
  • 9
  • 68
  • 99