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.
<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.
<leader>
is, use :echo mapleader
or :let mapleader
. If undefined, then it will use the default, which is a backslash "\"
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.
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.
<leader>
.
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
ggdG
means to press those four keys sequentially. A syntax like <C-w><C-j>
means to press CTRL+w, followed by CTRL+j.
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.
The default leader key is backslash(\). It's used in bindings as a chord with other keystrokes.
Success story sharing
,
is a good one. Much easier to reach than\\
, and who uses,
in vim anyway?(' ')
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. :)