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
iis insert mode, which allows you to modify textescputs 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
:wqis for write-and-quit (yes with the:):q!is for quit-without-writinguis for undo (no:and notctrl+U, justu)ctrl+Ris 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:
wfor word, which moves you word to word in a file, from left to right.bfor backwards, which moves you one word at a time, from right to left.efor end, which moves you one word at a time, from left to right, but at the ends of the words rather than beginningafor append, which puts you in insert mode (writing mode), but starts the character after wherever your cursor is (by contrast,ienters insert mode wherever your cursor is)oopen a new line and enter insert mode, below current lineOOpen a new line and enter insert mode, above current lineddelete character where cursor isppaste copied text where cursor isqquit file
You can also prepend a number to the commands to repeat an action N times.
And a couple that might be less intuitive:
ggmove to top of file- You can also use
:1for this (first line) :N, whereNis a number, will take you to the Nth line
- You can also use
Gmove to end of file$end of line^beginning of line- You can also use
0for beginning of line
- You can also use
yyank (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.oorO, but usuallyo, to start writing after the current line I’m on- A number combined with
wto delete a given number of words or$dto delete until the end of line - Doubling a command like
dorymakes it the whole line of text, so deleting a line or copying a line would beddoryyrespectively (and I use those often). - Number combinations with commands, e.g.
5wto move forwards five words- Or
5dwto delete five words - Note that you can stack commands with
vim-> this is the second example in this post, the first was:wqfor 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.