VimLikeAPro Sample
VimLikeAPro Sample
Tim Ottinger
This book is for sale at http://leanpub.com/VimLikeAPro
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and
many iterations to get reader feedback, pivot until you have the right book and build traction once
you do.
Epilog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
CONTENTS 1
• With the sudden rise in Unix use (Linux and Mac OS X, in particular) the text editor known
as vim (“vi improved”) has become ubiquitous
• vim has a small footprint in RAM and on the CPU. A given system can support a great many
vim users at once.
• vim has a lot of “superpowers”, which make editing quite efficient.
• vim has “geek appeal”.
• vim has a very active user/developer community. It always has.
• Learning new stuff is good for your brain.
• the line number (I have line numbers turned on by default, you might not),
• a bunch of tildes (∼) marking empty lines
• a line of status at the bottom of the screen.
You can usually tell by the blank line markers that you are in vim.
The status line can be turned off, but in this case it shows:
Modality
The original vi was invented back when “green screen” ascii terminals were the UI innovation of
the day (ask your dad about ascii terminals). There were not so many shift-like keys (shift, alt, ctrl,
windows, fn) and there was no such thing as a pointing device. Pretend that there was only a “ctrl”
key and a “shift” key, whether it is true or not.
Programming and all other computer use)was done with your eyes on the screen and two hands on
the keyboard. Vi made it possible to do so quickly, because vi is a bit like a video game, where any
little gesture on the keyboard causes something to happen.
Master The Basics 5
If you are using vim and pressing Whatkeys causes either cool or unfortunate things to happen, you
know you are in the command mode, which is the default state of the editor. Commands are assigned
to the ordinary everyday keys like ‘p’ and ‘y’ and ‘g’, not chords like Control-Alt-Shift-Escape.
vim has combinations and sequences to get the special power-ups like navigating between functions
in separate files and reformatting entire lists in the middle of a document, code completion,
abbreviations, templates and the like but that is for later.
There has to also be a way to type text into a document, but most of the keys already have special
meanings! The only reasonable option was for the developers to create an “insert mode” which would
make the ‘a’ key type an ‘a’ character, just like a typewriter (ask your dad what a typewriter is). This
is called “insert mode”. Not much happens in “insert mode” except normal, old, boring typing. You
only want to use insert mode when you must do typing, but all the cool stuff happens in the normal
(control) mode.
You will learn many convenient ways to get into insert mode, but for now you should know that the
way out of insert mode, back to the video-game-like control mode, is to press the ESCAPE key.
Understanding that you have basically two modes of operation will make your stay in vim less
confusing, and starts you on your way to vim guruhood.
item meaning
register Register name (optional, with default cut/paste register
used if not otherwise specified)
repeats Repeats (optional): 13
operation Operation: y (for yank)
movement Movement (depending on the operation):
yy (repeated to take current line, a convention used in
vi)
vim commands work with the pattern shown above. There are some commands that don’t use
register and some that don’t take movement, but for the most part this is the way it goes.
Master The Basics 6
A register is essencially a cut-n-paste buffer. In most editors you get only one. In vim you have too
many, but you don’t have to use them, so don’t worry about it untl you get to the lesson on registers.
A repeat is a number of times you want to do something. If you don’t type in a number, the default
is 1.
An operation is a keystroke that tells vim to do something. These are mostly normal keypresses, and
most operators do not require shifts or alts or controls.
Movement is a command that takes the cursor somewhere. There are a lot of them, because there
are lots of ways you need to move. don’t panic, though, because you can use the arrow keys if you
really have to. There is a whole section of this tutor on moving around.
Lets try an example to clarify how the pattern works. If I want to copy 13 lines into my copy/paste
register, I can skip specifying a register name, type 13 for a repeat count, press ‘y’ for yank, and then
press one more ‘y’ as a movement command (meaning current line). That yanks 13 lines into the
default cut-n-paste register. If I press ‘p’ (choosing to use no register name and no repeat, recognizing
that put has no movement command), then those lines are pasted back into my document just after
my current line.
If you know this pattern, then you will know how to leverage everything else you learn about vi.
Learn all the convenient ways to move, and you will know how to cut, paste, reformat, shift, and
many other things you want to do.
GET OUT!
You should be able to get out of a vim session once you are in it. There are a few ways to do so. Try
these:
What to Type What it does
:q Quit the current window (or editor if you’re out of windows) if
there are no unsaved changes.
:q! Quit the current window even if there are unsaved changes.
:qa Quit all windows unless there are unsaved changes.
:qa! Quit all windows even if there are unsaved changes.
:wq Save changes and quit the current window.
ZZ Save changes and quit current window
When you type a colon, the cursor drops to the lower left corner of the screen. Later you will know
why. For now, it is enough to know that it is supposed to do that, and that these :q commands will
work. Notice that there is no : in front of ZZ.
Master The Basics 7
If you can’t get out of vim, you should check to be sure the caps lock is OFF, and press the escape
button. If it feels good, press it a couple of times. If it beeps, you know that you’ve escaped enough.
Then these exit commands should work.
Mnemonics
Not all commands are mnemonic. They tried, but there are more than 26 things you might want
to do in a text editor, and the distribution of letters means that not that many words start with a
‘q’ and happen to be meaningful in editing. However, many commands are mnemonic. There are
commands for moving Forward, Back, a Word at a time, etc.
A great many are mnemonic if you know the jargon. Since “copy” and “cut” both start with “c”, we
have the vernacular of “yank” (for copy), “delete” (for cut), and “put” (for paste). Y, D, P. It seems a
little funky but it is possible to remember these. Remember, eventually it becomes muscle memory,
but the authors of VI and vim tried not to be arbitrary when it was totally up to them. Sometimes,
there wasn’t much of an option.
Invocation
Now that you know how to get out of vim, maybe it is time to learn how to get into vim. We typically
start vim from the command line, though you may have menues or other ways.
There are a few ways you can start vim.
Master The Basics 8
There is more, not shown. For now, knowing these will help you to get started. DO try out the
vimtutor and the vimdiff. Some of these won’t work until you set up a .vimrc, but that is explained
later.
starting vim
If you type gvim instead of vim (mvim on OS X) then you will get the gee-whiz, cool, gui version of
vim (if it is installed). It has some extra powers. You’ll typically like it better than the plain vim. It
is like vim with chocolate icing. Everything we say about vim here is also true of GVIM, so you can
use the same tutorial with either.
You don’t have to edit one file at a time. You can start (g)vim with multiple filename arguments.
When you do, there are a few options you can pass to get some fun additional effects. Of course,
these are more fun after you learn how to work with split windows, so you can refer back to it later.
Wow. That is far worse. As a pro vim user, I press the u button for undo.
starting vim
There is a lot more to undo and redo, but this is enough. Be happy that you can revert changes, and
un-revert them. vim isn’t as powerless and unforgiving as you feared it might be, though you might
still not like it very much. Just wait for that muscle memory to kick in.
Master The Basics 10
If you get into a real mess, then exit the editor without saving.
If you are really afraid, or really cautious, then you should have version control for your text files. I
recommend you start editing with junk files in a junk directory anyway, but when you are working
on something important, you should not be afraid to make changes. Version control is a good security
blanket and a useful backup strategy. Consider using Git or Mercurial, both of which are easy and
powerful.
The following commands are handy, and are even sensible and memorable if you know regex:
Finally, if you can’t move by searching, jumping, etc, you can still move with the keyboard, so put
your mouse down.
You want to use the option hls (for “highlight search”) in your vimrc. You will learn about that soon
enough. In the short term you can type “:set hls” and press enter.
³http://www.geocities.com/volontir/%3E
⁴http://larc.ee.nthu.edu.tw/~cthuang/vim/files/vim-regex/vim-regex.htm
Master The Basics 14
Consider the value of the c command. If you use it with the ‘ t or f commands, it becomes very
powerful. If you were at the C at the beginning of the previous sentence, you could
type ct. and retype the whole first sentence, preserving the period. The same is true
with other commands, such as the d‘ for delete. The movement commands add a lot of power to
the change command, and that is one reason why it is important to learn to move well.