Step 4 of 6

10. Make Vim Your Own With Maps

The leader key

One particular problem with remaps is that it’s quite easy to accidentally shadow existing Vim behavior. 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 customize 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. Work through the checklist below: set the leader to space, define the <leader>h mapping from the demo, then trigger it. (Outside this course, the mapping behind your leader can be anything you like — we prescribe this one so the editor can tell when it fires.)

  1. Set the leader key to space Nothing visible changes yet — type :let mapleader = " " and hit Enter.
  2. Create a leader mapping This one prefixes a line with a bullet — type :nnoremap <leader>h I- <esc> and hit Enter.
  3. Use your leader map With your cursor still on the top line, press Space then h — the line grows a - bullet prefix.
Set the leader key to space Nothing visible changes yet — type :let mapleader = " " and hit Enter.

Next: Create a leader mapping

Loading editor…