How to change your leader key in Vim

Tyler Tyler (270)
0

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.

Posted in these interests:

vim
PRIMARY
23 guides

First, open your .vimrc file. This is typically found in your home directory:

vim ~/.vimrc

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.

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>

So whenever ,e is typed, it will be as if I typed :buffdo e!<CR> (<CR> means return).

Tyler's profile pictureTyler
Joined in 2015
Software Engineer and creator of howchoo.
Share this guide!
Related to this guide:
Vim undo branches
You probably already know how to use vim's basic undo and redo features. For a quick refresher, read this short tutorial on how to use vim redo and undo.
Dayne's profile picture DayneView
In these interests: vim
3 ways to use tabs in vim
Vim tabs can be a great way to visualize which files you currently have open. Many vim users get bogged down by them and either end up working too hard to use them or give up altogether.
Dayne's profile picture DayneView
In these interests: vim
Vim redo and undo
Learn how to undo and redo changes using vim. If you are brand new to vim, learn more about the basics in this intro to vim guide.
Dayne's profile picture DayneView
In these interests: vim
People also read:
Once you learn to think in text objects, you'll never be the same
Vim is a powerful tool. I often paste raw data into Vim in order to format it in a specific way, and sometimes I need to clean up the file by removing many blank lines.
If you find yourself highlighting text in Vim with your mouse to copy and paste it elsewhere, stop. There's a better way, using the yank command, to copy text into your clipboard on macOS or Windows.
Basic vim commands
Because you've accidentally opened Vim and don't know how to exit
Vim is amazing. Well, Python is doing all the work, but I still like Vim. Learn how to format JSON in Vim like a pro with this short guide! tl;dr Paste your json, and run this in vim normal mode. :%!
Tabs are evil. This guide will show you how to convert tabs to spaces in Vim.
Need to change the case of characters to all caps or all lowercase? This is easily done using Vim. This guide will show you how to change the case of characters in Vim.
Posted in these interests:
vim
vim
23 guides 16 subscribers 
PRIMARY
Explore
Discuss this guide: