chapters/07.markdown @ c6cd81623f93

Update acknowledgements
author Steve Losh <steve@stevelosh.com>
date Sun, 20 Nov 2016 11:58:14 +0000
parents b0ca11bfb7a8
children (none)
Editing Your Vimrc
==================

Before we move on to learning more Vimscript, let's find a way to make it easier
to add new mappings to our `~/.vimrc` file.

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
concentration.

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))).

Editing Mapping
---------------

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:

    :::vim
    :nnoremap <leader>ev :vsplit $MYVIMRC<cr>

I like to think of this command as "**e**dit my **v**imrc 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 `:split`.

Take a minute and think through that command in your mind.  The goal is: "open
my `~/.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 `~/.vimrc` file.

Sourcing Mapping
----------------

Once you've added a mapping to your `~/.vimrc` file, it doesn't immediately take
effect.  Your `~/.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:

    :::vim
    :nnoremap <leader>sv :source $MYVIMRC<cr>

I like to think of this command as "**s**ource my **v**imrc file".

The `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:

* Use `<leader>ev` to open the file.
* Add the mapping.
* Use `:wq<cr>` (or `ZZ`) to write the file and close the split, bringing you
  back to where you were.
* Use `<leader>sv` to 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.

Exercises
---------

Add mappings to "edit my `~/.vimrc`" and "source my `~/.vimrc`" to your
`~/.vimrc` file.

Try them out a few times, adding dummy mappings each time.

Read `:help myvimrc`.