In Vim? Don’t Panic
For when you forgot to use -m
on that last git commit
and
other foibles.
What, why?
When I was getting started with tech working in college, I would
be filled with dread any time I accidentally found myself in vi
/ vim
. I felt like I was stuck in there and no matter how may
times someone told me how to get out of it, for some reason my
brain just shut down. I’m sure part of it was that I was learning
a lot at the time, and as much as you need to learn to use your
tools, learning to use what felt like a very complicated tool was
just one thing too many.
In fact, my brain didn’t calm down until I did some rounds of Vim Adventures, and honestly we should have more fun education. We spend so much of our time glued to monitors, especially during the pandemic.
Unfortunately, I am not here with a suite of gamified educational tools.
What I am here with is some basics of vim
, though, so that
forgetting -m
doesn’t take 10 years off your life.
The very, very, very, very basics
There are only a few things you actually need to know if you
find yourself in vim
and want to just escapte hatch out of it
instead of using it on the regular:
- Don’t panic
i
is insert mode, which allows you to modify textesc
puts you in command mode, which is also the default mode- This is what allows you to use the commands I’ll outline below, like save and quit
:wq
is for write-and-quit (yes with the:
):q!
is for quit-without-writingu
is for undo (no:
and notctrl+U
, justu
)ctrl+R
is for redo (this time you needctrl
)
That’s really it. These few things can save you from almost
anything you do to yourself in vim
. Entered a different command in
vim
and don’t know what you did? Undo it. Wanted to enter text
and entered a string of commands because you weren’t in insert
mode? Undo it, enter i
, and then start typing away! Made a bunch
of changes that you didn’t want, that you don’t want to undo your
way through one at a time? :q!
and get outta there!
A little extra
Many basic vim
commands can be guessed as they are single letter
abbreviations of what the command is. A few examples:
w
for word, which moves you word to word in a file, from left to right.b
for backwards, which moves you one word at a time, from right to left.e
for end, which moves you one word at a time, from left to right, but at the ends of the words rather than beginninga
for append, which puts you in insert mode (writing mode), but starts the character after wherever your cursor is (by contrast,i
enters insert mode wherever your cursor is)o
open a new line and enter insert mode, below current lineO
Open a new line and enter insert mode, above current lined
delete character where cursor isp
paste copied text where cursor isq
quit file
You can also prepend a number to the commands to repeat an action N times.
And a couple that might be less intuitive:
gg
move to top of file- You can also use
:1
for this (first line) :N
, whereN
is a number, will take you to the Nth line
- You can also use
G
move to end of file$
end of line^
beginning of line- You can also use
0
for beginning of line
- You can also use
y
yank (copy) a given character
These can help you to move around in your text file faster than if you used arrow keys alone. That said, you definitely still can use arrow keys - especially when combined with number keys.
Some common patterns when “just editing”
I find that my most commonly used patterns when editing are:
$
+a
-> moves me to the end of line, appends to start writing after end of that line.o
orO
, but usuallyo
, to start writing after the current line I’m on- A number combined with
w
to delete a given number of words or$d
to delete until the end of line - Doubling a command like
d
ory
makes it the whole line of text, so deleting a line or copying a line would bedd
oryy
respectively (and I use those often). - Number combinations with commands, e.g.
5w
to move forwards five words- Or
5dw
to delete five words - Note that you can stack commands with
vim
-> this is the second example in this post, the first was:wq
for write-quit
- Or
Before I head back off to the real world, it’s worth mentioning
that vim
does respect regex for those familiar, which can be an
awesome extension if you want to do more advanced file
manipulation (which I may cover in a later post, but that
definitely exceeds scope of this one). The reason I mention here
is that it is also common for me to do search-and-replaces this
way, if I noticed I haven’t used a consistent pattern usually for
casing or hyphens.