What is the Vim <leader> key?
Summary
The <leader>
key in Vim is a customizable prefix used for user-defined shortcuts, with a default value of \
(backslash). This article explains how to identify the current <leader>
key, and how to customize it.
Introduction #
In Vim, the <leader>
key is a customizable prefix key used to create user-defined shortcuts (mappings). It acts as a placeholder in mappings, allowing to easily create memorable commands by combining <leader>
with other keys.
Default <leader>
Key #
By default, the <leader>
key is \
(backslash).
If you read documentation, or, e.g. a Vim cheat sheet, and see something like <Leader>t
, you can execute it by default by pressing \
+t
.
Identify Current <leader>
Key #
If you want to check what your current <leader>
is, you can use the Vim commands :echo mapleader
or :let mapleader
from normal mode (press Esc
if you are not in normal mode).
If the result is undefined (E121: Undefined variable: mapleader), Vim will use the default <leader>
key.
Customizing the <leader>
Key #
You can redefine <leader>
to a more convenient key in the .vimrc
or init.vim
files:
let mapleader = " "
This sets the <leader>
key to the spacebar (space
), a common and ergonomic choice.
let mapleader = ","
This sets the <leader>
key to the comma (,
), which is also a good choice.
Make sure to use spaces on each side of the equal sign (=
) and double quotes ("
), not single quotes ('
).
Note that the value of g:mapleader
is used at the moment the mapping is defined. Changing g:mapleader
after that has no effect for already defined mappings.
Further readings #
Sources and recommended, further resources on the topic:
License
License: What is the Vim <leader> key? by Jonas Jared Jacek is licensed under CC BY-NC-SA 4.0.
This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. If others modify or adapt the material, they must license the modified material under identical terms. To give credit, provide a link back to the original source, the author, and the license e.g. like this:
<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/publications/leader-key-in-vim">What is the Vim <leader> key?</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-NC-SA 4.0</a>.</p>
For more information see the DITig legal page.