61

How can I figure out which key is set as my <Leader>, and how do I remap it?

58

By default your <leader> is \, backslash. You can check it with:

:echo mapleader

If this gives you an E121: Undefined variable: mapleader, it means it's set to the default of \. If it gives you something else, then it's that :-)

You can easily remap it. I mapped it to the space-bar:

:let mapleader = "\<Space>"

Note that the value of mapleader is used at the moment the mapping is defined. So this example:

let mapleader = ","
nnoremap <Leader>a :echo "Hey there ,"<CR>

let mapleader = "\<Space>"
nnoremap <Leader>a :echo "Hey there space"<CR>

Will produce two mappings: ,a and <Space>a.

This means that the current value of mapleader is not necessarily the value that was used to define your mappings!

In addition, there's the maplocalleader, which is the same as mapleader, except that it's used by <LocalLeader> and that it's local to the current buffer.

More information about <Leader> can be found in Vim's help with :help mapleader.

  • 3
    Why do we need a backslash in "\<Space>"? – thameera Feb 4 '15 at 9:52
  • 10
    The backslash is required because otherwise vim will recognize "<Space>" as a normal string and not the code for the space key. See :help expr-string for examples. – tokoyami Feb 4 '15 at 14:38
  • 1
    Just a note: e.g. let mapleader = ' ' also works for setting <Leader> to space. If there are any possible side-effects by using this syntax, feel free to mention them. – Daniel Andersson Feb 20 '15 at 12:32
  • It appears that maplocalleader is also set to backslash by default. Maybe that's what was meant by "is the same as mapleader, but I wasn't sure at first. – Mars Jan 5 '18 at 4:11
7

You can display the current leader key like this :let mapleader

  • 4
    Trying this, I get E121: Undefined variable: mapleader. – Praxeolitic Feb 4 '15 at 9:37
  • Then for some reason the mapleader was not set or has been unset – craigp Feb 4 '15 at 9:38
  • 4
    Oh, got it. Just a note for others then, if you don't explicitly set the leader key then you get the error above. – Praxeolitic Feb 4 '15 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.