As far as using IDEs go, I wouldn’t say I’ve been around the block, but I’ve seen my fair share. It was only when I started running Arch Linux that I first switched to vim. At this point you might be expecting me to sing vim’s praises and how every developer worth their salt should be using it. I’m not going to.
The learning curve for vim is steep. It took me a long time to wrap my head around what the different modes meant, and everyday I use it I learn something new. Hopefully, from now on I’ll be sharing those discoveries with you, dear reader.
I also didn’t stay with vim once I started using it. I switched between vim, spacemacs (which is great, I highly recommend you give it a spin), IntelliJ IDEA, Visual Studio, and a whole plethora of others.
But now I’m back on the vim train. And I’ve discovered the leader key.
What does a leader do?
The leader key is essentially a prefix that you can set for remapping keys/key
sequences in vim. The notion is strongly implemented in spacemacs where almost
every command is started by pressing <Space>
. In, vim, this means that you can
remap a whole bunch of commands that you can run easily by pressing the leader
key followed by your mapping.
For example, if my leader key is <Space>
and I can have vim exit whenever I
press <Space>q
by mapping it to the vim command :q
.
This means that you can set custom key sequences (not just keys) to execute any kind of vim command. Literally any command you can execute in vim.
Any. Command.
How to use the leader key
In your .vimrc
file you first designate which key to set as the leader key
with:
let mapleader="{key here}"
Personally, I would recommend the <Space>
key as it is really comfortable to
use.
Then you can remap any sequence to a command in the form of:
[nnore]map <leader>{key sequence} :{vim command}<CR>
For example, if I want to exit vim, saving all the files before I do so I would have the following:
nnoremap <leader>qq :wqa<CR>
Why are you using nnoremap
and not just map
?
Using nnoremap
instead of map
just means that the sequences you’ve set in your
.vimrc
file will be mapped explicitly as is for the normal mode in vim. By
setting it for normal mode only I’m making sure that the leader key only works
in the normal mode for vim and not the insert or visual modes.
And that’s it for my knowledge of vim leader key use. I’ll leave a list of my current remappings below for you to use should you wish. This has been a very brief and shallow dip into the abyss that is vim and vim customization. If you want me to do a whole series on getting into vim and my experiences please let me know either here or on twitter!
Remappings
Set the leader key
let mapleader="<Space>"
Quit and save all files
nnoremap <leader>qq :wqa<CR>
Edit .vimrc
file
nnoremap <leader>fed :e $MYVIMRC<CR>
Reload .vimrc
file
nnoremap <leader>fer :source $MYVIMRC<CR>