40

I see <Leader> quite often in other people's vimrc files. Like this one.

What is it? What does it do?

Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271

2 Answers2

25

Vim is full of various commands, which are assigned to almost all keys on the keyboard. But this causes a problem: Which commands can we use for our own commands, without interfering with existing ones? And at this moment, the <Leader> key comes into play. Think about <Leader>-key like a namespace for any user-defined commands. You can assign any command to a mapping with a leading <Leader> and you can be fully confident that your mapping won't break anything.

Default key for <Leader> is backslash.

muru
  • 24,838
  • 8
  • 82
  • 143
Alexander Myshov
  • 2,118
  • 15
  • 18
15

To quote :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>

In other words, it lets the first key of mappings (specified in terms of <Leader>) be user defined.

Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271
John O'M.
  • 8,552
  • 2
  • 42
  • 58
  • 8
    I believe that the rationale behind the <Leader> is that it provides you with a "clean" way to provide custom shortcuts, without overriding existing Vim shortcuts. – Martin Tournoij Feb 16 '15 at 13:00