Drew Neil describes this in his fabulous book, Practical Vim, as being akin to an artist with his paint brush hovering over a canvas. When you are painting, you don’t spend the majority of your time with your brush making strokes. A good painter plans his strokes, choosing carefully where to put down his brush before making a careful and tactical mark.
Putting the brush to the page is akin to Insert Mode in vim. You get what you type, for the most part. You can enter Insert mode from Normal mode by typing the
There are other modes in vim too, like
Command-Line Mode, and
Ex Mode. For now lets just focus on Normal and Insert, though.
Normal mode allows you to move quickly across your document, skipping around by paragraphs, code blocks, jumping to function definitions, visually to the middle or bottom of the screen, and pretty much anywhere else you want to go in a matter of a few keystrokes. No, seriously, I’m not exaggerating here. Here’s a great diagram covering the movement keys available to you in vim without any special plugins or configuration.
Most commands in vim, whether they be actions or movement, have a mnemonic device to help you remember. A number of these are listed in the graphic above.
So what about when you want to do something? Delete something, copy to the clipboard, replace a string? These are the verbs of vim and their mnemonics are strong as well.
y= yank (like copy, but from a time before copy/paste existed)
p= put (like paste)
v= visual (select)
There are a few more, but in day-to-day usage, these will get 99% of your work done. Not a whole lot to memorize, is it?
If you want to delete something, press
d. But wait… what are you deleting? The power of vim starts to become apparent when you realize that the
d command on its own does nothing at all. In order to complete the verb, we need to tell it where to apply it.
Think of this combo as “Verb -> Movement”, and it is the introduction to composition.
All of those fancy movement keys you learned earlier aren’t just for jumping around Normal mode. They’re also part of every action you’ll take. I think this is best illustrated with an example.
If we want to delete a word we add the delete command with the movement command for a single word:
dw. If you want to yank everything to the bottom of the current paragraph, following the same pattern you would get:
In vim you can see the keystrokes being entered in the lower right corner of the screen. This is related to something called Operator Pending Mode, but for our purposes it means you can get a sense of the commands I’m entering to accomplish the text manipulations seen here.
“Verb -> Movement” is a powerful thing, and it can be made more so by understanding the benefits of this encapsulation. If a full command is considered one of these complete compositions, then we can operate on the whole piece as well.
You can enter a number prior to your verb and the entire command will be repeated that number of times. Also, you can repeat the last command, whatever that composition might have been, by pressing the
. key. Finally, your undo levels are defined by your commands as well. Each time you press
u it will undo the last full composition.
There’s a lot to vim. It is a tool you can keep learning for years and years and never fully master. It is also easily accessible to new learners. It has a fantastic support group (#vim on irc.freenode.net for starters), tons of tutorials, guides, books, etc. It is quick to pick up the basics. It is installed on almost every system in the world, and easily added on the few (Windows) that it’s missing from. It is an investment that will pay for itself immediately, and continue to benefit you for as long as you write.
Now, if you haven’t already, go launch
vimtutor and start vimming.
Have questions about vim or want more guidance? Disqus below!
The gifs included in this post were generated with imagemagick, ttyrec, and sugyan’s version of ttygif. Here’s the script I put together to make things easier.
#!/bin/bash # This will record a vim session with no .vimrc and create vim.gif. # The scripts are designed to run on default sized term window. # The cropping is sized for iTerm, so you may need to tweak it. # # Dependencies # brew (or apt-get) install ttyrec imagemagick # ttygif - https://github.com/sugyan/ttygif/releases # Record the vim session with no .vimrc ttyrec -e "vim -u NONE" # Convert session to gif ttygif # Crop gif and get rid of the ugly header convert tty.gif -coalesce -repage 0x0 -crop 570x354+0+22 +repage vim.gif # cleanup rm tty.gif rm ttyrecord