Vim 101: What is the Leader Key?
You’re happily using Vim, slowly mastering file navigation, split windows and tabs, search and replace, and then you discover a hot new .vimrc on Hacker News that includes lots of commands like this:
noremap <Leader>W :w !sudo tee % > /dev/null
What the devil is <Leader>
? Well, in this example it simply means typing \W
(backslash then w) in Normal mode will save the current file using the sudo
command. Does that mean <Leader>
means backslash? Well, not quite. The <Leader>
key is a reference to a specific key defined by the mapleader
variable. A lot of people change to comma because they find it easier to type:
let mapleader=","
The mapleader
variable is easy to change, and if you always remember to map keys with <Leader>
then you'll avoid confusing your own customisation with Vim's default keyboard shortcuts.
The beauty of the <Leader>
key is it effectively gives us a namespace for customised keyboard shortcuts. You don't need to worry about treading on Vim's toes when you set a keyboard shortcut using <Leader>
.
Vim waits for 1000 milliseconds after the <Leader>
key has been pressed, so if you take too long to press the next key in the sequence it won't be matched. This timeout can be changed by using :set timeoutlen
to set specific value.