Merge.
author |
Steve Losh <steve@stevelosh.com> |
date |
Sun, 13 Nov 2011 17:59:27 -0500 |
parents |
b63f0efbd25a |
children |
e66e6a4e104d b0383a6b84a1 |
Editing Your Vimrc
==================
Before we move on to learning more Vimscript lets find a way to make it easier
to add new mappings to our `~/.vimrc` file.
When you're coding away furiously at a problem and realize a new mapping would
make your life 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 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 is that you want to (make it easier to (make it easier to (edit
text))).
Editing Mapping
---------------
Lets add a mapping that will open our `~/.vimrc` file in a split so we can edit
it and get back to coding. Run this command:
: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 that variable works.
`:vsplit` opens a new vertical split. If you'd prefer a horizontal split you
can replace it with `:split`, but in this age of widescreen monitors I don't
know why you would.
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.
Meta Efficiency
---------------
This is important because part of becoming more efficient is making it *easier*
for yourself to become more efficient!
Think of it this way: suppose you're trying to become a better digital
photographer. When you're practicing taking photos and want to see how they
came out, you:
* Take a photo.
* Upload it to your computer.
* Open it and see how it looks.
That process probably takes a minute. What if you could improve that?
Let's say you invest $50 and buy an [Eye-Fi](http://www.eye.fi/) memory card.
The Eye-Fi is a memory card for your camera that has a built-in wifi card, so as
soon as you snap a photo it gets transferred to your computer.
I know, we're definitely living in the future. Isn't it awesome?
Now you spend an hour and write a little script to automatically open the photos
that get transferred to your computer by the Eye-Fi.
You've spent $50 and one hour, but now instead of taking a full minute to check
your work, it takes ten seconds.
Assuming you charge $100 per hour for freelance work, you've got to make up one
an a half hours of time for this investment to be worthwhile. If you're saving
50 seconds per photo, you need to take about 109 photos for project to pay for
itself.
109 photos is *nothing*. It's a drop in the ocean. A blip that doesn't even
register on the radar. You'd blow past that number in a day's practice without
even noticing. Hell, you'd take more photos than that in two hours!
The same goes for our new mapping. It saves us only a few seconds each time we
use it, but it takes far, *far* less than an hour and a half to set up.
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:
: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 we can easily add new mappings during the heat of coding. We:
* Use `<leader>ev` to open the file.
* Add the mapping.
* Use `:wq` to write the file and close the split, bringing us back to where we
were.
* Use `<leader>sv` to source the file and make our changes take effect.
That's six keystrokes plus whatever it takes to define the mapping. It's very
little overhead, which reduces the chance that we break our concentration.
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`.