How to Change Your Leader Key in Vim
Posted in these interests:
One of the strengths of Vim is that it’s highly customizable. And while remapping keys is possible, we often want to create shortcuts without changing the default key bindings. The leader key allows us to do this elegantly.
The leader key, which is basically a command prefix, allows us to create shortcuts to some of our most commonly used commands. Sadly, though, the default leader key is
\. So if you’re concerned with efficiency,
\ might cost you a few seconds a day, in contrast with something a little closer to home row.
1 – Use the mapleader variable in your .vimrc file
First, open your
.vimrc file. This is typically found in your home directory:
Now use the following to set the leader key.
let mapleader = ","
In the above example, I mapped the leader to
,. This is much easier to access than
\, but you can map the leader to whatever key you’d like!
For this change to take effect, you’ll have to re-launch Vim.
2 – An example shortcut using the leader key
Even though it isn’t the main purpose of this guide, I figured it would be useful to provide an example shortcut using the leader key.
One of the commands I use frequently is
:buffdo e!. Sometimes the files in my current project have been changed by another program (like changing branches in git). This command force reloads the files in each of my buffers.
As you can imagine, typing this is a pain. Typing
,e is much easier. Add the following to your
.vimrc file to create shortcuts like this one:
map <leader>e :bufdo e!<CR>
,e is typed, it will be as if I typed
:buffdo e!<CR> (