About 12 years ago I forced myself to get comfortable using Vim. It took about a week before I felt productive using it in my day job. Now I don’t think I can code efficiently without it.
At first I was just writing code in the terminal’s Vim (seriously). Quickly I moved on to using MacVim. A bit later I moved onto using Vim key mapping plugins for PHPStorm and VS Code. Both key mapping plugins are pretty good and keep my Vim skills sharp.
Much like typing in general, the keystrokes become muscle memory. Half the time, I have to think hard about the key or key combination I’m entering to do a thing in Vim when I’m teaching others.
Now I’m by no means a Vim expert or elitist. In fact, I even cheat a bit often times (god forbid I ever use the trackpad). But I swear Vim makes you way more productive. The speed in which you can move around a document with just a few key strokes saves ever so many precious seconds in your day. Trust me, they add up.
So for this post, I’m going to share with you the key combinations that I find are the most important (at least to me). Maybe it will compel you to give Vim a real try.
:w :q :wq (write, quit, write & quit)
The big joke is once you’re in Vim you can’t get out. Well, this is how you get out. And save your file too.
Insert mode. By the nature of Vim you mostly just move around the document. Once you want to add something, you type
i and start typing what you want. In my case, it’s usually code.
esc (escape key)
Quickest way to exit insert mode and start moving around the document again. You can also use
ctrl [ but I find that awkward as hell.
ctrl r (undo and redo)
u will undo and
ctrl r will redo a change you made. Do it again and again for multiple undos and redos (there is a limit though).
dd or D (delete or cut)
Quickly delete a whole line. Also stashes that delete and you can paste it with…
Paste what you have stashed.
v (for inline highlighting) and optionally end with
V to select a whole block of text. If you hit them one after the other without moving (or just moving left and right) it will highlight the entire line you are on. Commonly you’ll use this with
y (yank or copy)
Copy what you have highlighted or if nothing’s highlighted, the line you’re on. Then just paste it with
h j k l (right, down, up, left)
I started off with the arrow keys, but once I switched to
h j k l and got used to it, I moved around the document a lot faster. I don’t even notice it anymore. I actually had to go into a document to look up which keys do what because I don’t even think about it. If you’re in insert mode and don’t want to exit it, sure, use the arrow keys. Probably better to just hit escape, but you do you…
5h 5j 5k 5l (5 spaces to the right, down, up, or left)
Want to move around faster? Type a number and
l to move that many spaces in that direction. 5 here is just an example… use any number.
:5 5gg 5G (any number, but this example will take you to line 5)
Know what line you want to get to? Type
: then the number of the line and hit enter. You’re instantly transported there. You can also use
5G. I often forget those exist and usually use
: and the number. Again, any number works that’s a line number in your document. The 5 here is just an example.
G (top of document and bottom of document)
Quickly get to the top of the document with
gg and the bottom of the document with
G. You’ll be amazed how often you actually use these key strokes.
%s/foo/bar/g (search replace)
The example above will replace every instance of
bar throughout a file. Much faster than using what’s built into your IDE. Also you can use regular expressions here.
bar here are just examples. Use any search and replace text.
^ (brings curser to end of a line and start of a line)
Quickly move to the start of the line you’re on, or the end. Works great with
% (move to matching characters)
Default supported pairs include
. There’s also plugins that support markup tags like HTML/XML tags, which is pretty handy.
:/foobar (find a word)
I use this to just quickly find a word in the document. Along with that use…
N (next and previous)
If you’re searching for something, use
n to go to the next match and
N to the previous match.
There is SO much more to Vim than this, but I found these are my goto essentials to being productive (though I may add more to this post as I think of them).
And like I said, I’m no expert. Just some guy that has been using Vim for 12 years, but never really got any better at it in the past 11 years. That said, if you get used to these basic keystrokes, I’m pretty certain after a week (maybe 2) you will feel much more productive. And heck, cheat a little (no one’s watching). Just remember, a few Vim keystrokes goes a long way to saving time and being more productive.
If you have some favorite Vim keystrokes (or want to mention an obvious one I missed) let me know in the comments.