Vim Tip #3: THE FIRST TIP / START HERE

2018-11-14(Wed)

tags: Vim

Vim Tips

(Should have been tip #1, but none of these are going to be in any damn order.)

So here's your first tip: Vim is a "Modal Editor," and this is a very weird thing. There are several modes, but as a beginner there are only two that matter: "Insert" and "Normal" (aka "Command") mode. Others include "Visual," "Select," "Ex-mode," and - my personal favourite - "Operator Pending." If anyone tries to tell you about "Operator Pending" mode at this point of your Vim journey, stab them: it's weird and complex and now is not the time.

Files are opened like any other command line editor: vi filename.txt. If you're using NeoVim (recommended!) or regular Vim (much better than 'vi'), substitute nvim or vim as appropriate. The editor will start in Normal mode: it always does. Read on before touching any keys, because every single key on your keyboard represents some form of command (this is why it's sometimes called "Command" mode, but I'll be calling it "Normal" which is the more common name). It's truly astounding how quickly you can mess up a text file with a few mis-pressed keys in Normal mode ...

Get used to hjkl for moving around. It's weird at first, but once you're used to it, it's a LOT faster than the arrow keys.

And here's probably the most important thing to remember, even more than those directional keys: :wq writes and quits. Since you're just starting, the other thing you'll probably really want to know (if not now, then 15 minutes into your first editing session) is how to quit without saving, which is :q!. You might think that :q would be "quit," and you'd even be right ... but it won't let you escape without saving if you've made any changes. So remember :q!.

If you want to add some text, you need to change into Insert mode. To do that, you press either i or a (keeping in mind that capitalized letters are almost always completely different commands from their lower-case equivalent). i is "insert," meaning "add text just behind where the cursor box is," and a is "append" meaning "add text just after where the cursor box is." This may seem a little redundant initially, but trust me: you'll want to remember both of these. You'll use them both a lot.

To get back to Normal mode, press <Escape>. There's an expression I've always liked explaining people like myself: "An Escape-hammering vi monkey." This is because you always want to get back to Normal mode and/or a known state: hit <Escape> two (or even three) times. Then you know what state you're in and you can go back to Insert mode if you want to, or issue a command.

Next steps