The leader key
One particular problem with remaps is that it’s quite easy to accidentally shadow existing Vim behaviour. For example, nothing is stopping you from creating a normal mode mapping for the i key. This would be super annoying, though, because we would have overloaded some built-in Vim functionality with our own stuff.
The problem is worse for plugin authors, because nobody wants to ship a plugin that overrides built-in Vim functionality that users might depend on heavily.
To answer this problem, the leader key exists. The leader key works as a namespace for your mappings, or for mappings provided by plugins. The idea is that you pick a leader key — say space — and start all of your custom mappings with your chosen leader. With a well-chosen leader key, you won’t be overriding any of Vim’s built-in maps, and you’ll have a safe space to build your own functionality.
The default leader key in Vim is \ — but you can customise this by setting the mapleader variable. I use the space bar as my leader key, because it’s super easy to type. So, my mapleader variable is set like this:
let mapleader = " "
Let’s try setting the mapleader variable and creating a leader map. In the editor:
- Set the
mapleadervariable to space with:let mapleader = " ". - Create a normal mode leader mapping of your choosing with
:nnoremap <leader>x ....