Vim Leader
The leader key is one vim feature among many that gives vim its customizability. Vim lets you choose one key to use as a prefix for all of your custom command needs. That key is called the leader.
By default, the leader key is ‘\’ by default. I mapped my leader to my spacebar. The spacebar is the largest key, most easily reached key on the keyboard, and yet it has no function in vim’s normal mode. Problem solved by using the spacebar as my leader.
let mapleader = "\<Space>"
The leader key has no commands mapped to it by default. To use it, you must map commands to it in your vimrc. This is very easy to do. Let me show you some of the leader mappings I have in my vimrc.
My full vim setup can be found here.
Clearing Highlighted Search
Vim’s search command ‘/’ is very powerful, and is a useful tool for jumping to different parts of your code. For example, if I’m writing in python, I can search for ‘def ‘ to jump from one function to another. However, when I’m done, all of the ‘def ‘s will still be highlighted, which can get a little annoying. Thus, I mapped ‘leader n’ to clear the highlighting.
nmap <leader>n :noh<cr>
:noh
is vim’s command-line mode command for clearing highlight, and <cr>
is the “enter” key. This mapping saves me three key strokes every time I want
to clear highlight, and my eyes don’t need to move to look at the vim’s
command-line at the bottom of the screen.
Finding Files and Exploring the File Tree
I used to use plugins like “FZF” and “NERDTree” for fuzzy file search
and file tree respectively, but over the last summer, I removed almost
every plugin from my vim and am using built-in features for my needs
as much as possible. For fuzzy file search, I’m using vim’s :find
, and
for my file tree, I’m using Netrw, vim’s default file explorer that shows
up when you enter vim in a directory instead of a file. To access these
efficiently, I had them mapped as leader commands.
nmap <leader>f :find " fuzzy search
nmap <leader>j :edit .<cr> " file tree
Swapping Buffers
For times when I don’t want to open the file tree, I created a leader command for swapping to the previously open file.
nmap <leader><tab> <c-^>
Toggle visual wrap movement behavior
If you have line wrapping on in vim (set wrap
), any line that is too long
will continue visually onto another line below. However, a visual line
is not the same as a line, so j
and k
will still move over an entire line,
even if visually, that line takes up multiple lines. To move over visual lines,
vim offers gj
and gk
, so some people have found it useful to map j
to gj
and k
to gk
. This solution didn’t satisfy me, as I wanted to be able to
access both the visual-line and non-visual-line depending on the situation. Thus,
I mapped ‘leader v’ to toggle between mappings.
nnoremap <leader>v :call Wrap_mode_on()<cr>
function! Wrap_mode_on()
noremap <silent> j gj
noremap <silent> k gk
noremap <silent> ^ g^
noremap <silent> $ g$
noremap <silent> 0 g0
noremap <leader>v :call Wrap_mode_off()<cr>
endfunction
function! Wrap_mode_off()
noremap <silent> j j
noremap <silent> k k
noremap <silent> ^ ^
noremap <silent> $ $
noremap <silent> 0 0
noremap <leader>v :call Wrap_mode_on()<cr>
endfunction
Colorscheme light mode and dark mode
I did something similar with toggling light mode and dark mode.
function! ToggleBackground()
let &background = ( &background == "dark"? "light" : "dark" )
endfunction
noremap <silent> <leader>b :call ToggleBackground()<cr>
Relative Line Numbers
…and for relative line numbers. I love relative line numbers, but sometimes I just need to be able to see the absolute line numbers. So, I bound “leader r” to toggle between them.
let s:relative = 1
function! ToggleRelative()
if s:relative == 0
let s:relative = 1
set nu rnu
else
let s:relative = 0
set nu nornu
endif
endfunction
noremap <silent> <leader>r :call ToggleRelative()<cr>
Conclusion
These were some of the leader mappings in my vimrc. I hope they’ll inspire you to use the leader key in new ways for your vim workflow.