Use Vim Like A Pro
Use Vim Like A Pro
Use Vim Like A Pro
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.
2013 - 2014 Tim Ottinger
Contents
Why Bother? (reasons) . . . . . . . . . .
Why Write This Tutorial (approach) . . .
How should one use the tutorial? (usage)
What can I do with this tutorial? (license)
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
1
1
2
2
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
3
3
3
4
5
6
7
7
8
10
12
12
13
13
14
Epilog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
15
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
CONTENTS
CONTENTS
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.
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
repeats
operation
movement
vim commands work with the pattern shown above. There are some commands that dont use
register and some that dont take movement, but for the most part this is the way it goes.
A register is essencially a cut-n-paste buffer. In most editors you get only one. In vim you have too
many, but you dont have to use them, so dont 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 dont 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. dont 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
:q!
:qa
:qa!
:wq
ZZ
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.
If you cant 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 youve 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 wasnt 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.
What to Type
What it does
vim
vim file.txt
vim +23 file.txt
vimtutor
vimdiff oldfile.txt newfile.txt
vimdiff .
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 wont 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. Youll 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 dont 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.
Option
What it does
-o
-O
-p
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 isnt 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.
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.
What it does
/
?
n
N
tx
fx
11
What to Type
What it does
gg
G
0
w
W
b
B
e
E
The following commands are handy, and are even sensible and memorable if you know regex:
What to Type
What it does
What it does
%
}
{
(
)
''
]]
[[
Finally, if you cant move by searching, jumping, etc, you can still move with the keyboard, so put
your mouse down.
What to Type
What it does
h
l
k
j
\f
\b
12
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.
13
http://www.geocities.com/volontir/%3E
http://larc.ee.nthu.edu.tw/~cthuang/vim/files/vim-regex/vim-regex.htm
14
What to Type
What it does
i
I
a
A
r
R
s
c
C
o
O
:
!
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.
Epilog
If you have followed the tutorial this far, you have a good start. There is much more to learn, and
much further to go.
As a graduate of the Vim Like A Pro school of editing, you must uphold the standards.