Before we move on to learning more Vimscript, let's find a way to make it easier
to add new mappings to our
Sometimes you're coding away furiously at a problem and realize a new mapping
would make your editing easier. You should add it to your
~/.vimrc file right
then and there to make sure you don't forget, but you don't want to lose your
The idea of this chapter is that you want to make it easier to make it easier to edit text.
That's not a typo. Read it again.
The idea of this chapter is that you want to (make it easier to (make it easier to (edit text))).
Let's add a mapping that will open your
~/.vimrc file in a split so you can
quickly edit it and get back to coding. Run this command:
:nnoremap <leader>ev :vsplit $MYVIMRC<cr>
I like to think of this command as "edit my vimrc file".
$MYVIMRC is a special Vim variable that points to your
~/.vimrc file. Don't
worry about that for right now, just trust me that it works.
:vsplit opens a new vertical split. If you'd prefer a horizontal split you
can replace it with
Take a minute and think through that command in your mind. The goal is: "open
~/.vimrc file in a new split". Why does it work? Why is every single
piece of that mapping necessary?
With that mapping you can open up your
~/.vimrc file with three keystrokes.
Once you use it a few times it will burn its way into your muscle memory and
take less than half a second to type.
When you're in the middle of coding and come up with a new mapping that would
save you time it's now trivial to add it to your
Once you've added a mapping to your
~/.vimrc file, it doesn't immediately take
~/.vimrc file is only read when you start Vim. This means you
need to also run the command manually to make it work in the current session,
which is a pain.
Let's add a mapping to make this easier:
:nnoremap <leader>sv :source $MYVIMRC<cr>
I like to think of this command as "source my vimrc file".
source command tells Vim to take the contents of the given file and
execute it as Vimscript.
Now you can easily add new mappings during the heat of coding:
<leader>evto open the file.
ZZ) to write the file and close the split, bringing you back to where you were.
<leader>svto source the file and make our changes take effect.
That's eight keystrokes plus whatever it takes to define the mapping. It's very little overhead, which reduces the chance of breaking focus.
Add mappings to "edit my
~/.vimrc" and "source my
~/.vimrc" to your
Try them out a few times, adding dummy mappings each time.