Every editor has its strengths, its weaknesses, and its warts. Vim is no exception. Its spartan interface is nearly impenetrable to the beginner, and it has a killer learning curve. I remember the first time I encountered Vim while in college. I was an intern at a small integrated circuit design company, and I was looking over someone's shoulder as he quickly jumped in and out of files from a Linux shell, darting around the files and making crazy edits without ever lifting his hands from the keyboard to reach for the mouse.
"What the hell kind of editor is that?!" I asked in awe.
"Oh, that's vi," he responded. "You don't want to use it. It's archaic and has a killer learning curve. You'll be happier with a modern editor."
Well, with that ringing endorsement, I just had to try it. There's no better way to get me to try something than to show me how awesome it can be and then tell me it's too hard to learn or not worth my time. He was right, though. Vi is not worth your time. Vim on the other hand, is totally worth it. Vim is definitely vi improved, and an extremely powerful, flexible text editor for quickly writing and changing text files, especially code. It is super fast in every way you can imagine—loading, opening files, editing, and exiting—and it's available on all major platforms.
The one major issue with Vim is that it's so hard to get started. (There are other issues, but they're more nit-picky things that programmers love to have heated debates about without ever reaching any kind of resolution.) We're going to try to resolve the learning curve issue here with a set of tutorials that get you up and running, and then progressively add more powerful features to your repertoire. Your editing speed will feel slow at first, like you're learning a new golf or tennis swing and you suddenly feel like a complete amateur again, but the payoff will be great. You'll learn to use an elegant, efficient programming tool. If you want to learn Vim and don't mind slowing down a bit now and grinding through some practice to get faster later, then read on.
Probably the easiest part of getting started with Vim is installing it, so this section is going to be short. I assume you have a general idea of how to install software on your operating system of choice, so here's the brief instructions for each one:
Windows: Download the self-installing executable from the Vim website, and install it.
Linux: You most likely already have Vim. If you don't, use the package manager for your distribution to install it along with the sweet graphical version, gvim.
Mac: Use homebrew to install Vim from the command
$ brew install vim && brew install macvim
Now you should be able to run Vim from the command line with either vim for the in-console version or gvim for the separate graphical window version. It should look something like this:
If you want to open a file along with Vim, use vim <filename> instead. To exit Vim, do what the welcome screen says and type :q<Enter>. That means type the colon character, a 'q', and hit the <Enter> key. The ':' brings the cursor to the bottom of the screen to enter commands at Vim's command line. The 'q' stands for 'quit,' which you could have also typed, but it's longer so stick with brevity. Many of Vim's command line commands can be abbreviated, and the most common ones will be a single letter. You can also add a '!' after the 'q' to force an exit without saving changes, but make sure you know what you're doing before getting too comfortable with this command!
The Bare Necessities
Okay, we've got Vim installed and we can open and close it, even with a file. Now if you try to edit a file as you normally would in any other text editor, you'll notice that all kinds of weird things happen, depending on what you try to type. This is because Vim has a few different modes of operation, and it is not in Insert mode by default. Vim always starts in Command mode, where any keys that you type on the keyboard are interpreted as commands for changing the text in the editor in a wide variety of ways. The other two modes are Insert (of course) and Visual (not so of course). We won't get to Visual mode for a while, but Insert mode is something we'll need pretty much immediately.
We have a multitude of choices for getting into Insert mode, but we'll start with the most common and straightforward way. Type the letter i. (I will maintain the convention of using a bold font for commands typed in Command mode or Visual mode, and a monospace font for commands typed at the Vim command line.) Now you are in Insert mode, as indicated by the "-- INSERT --" displayed at the bottom of the screen, and anything you type will be inserted into the text file. If there already is something in the text file, i will put you in Insert mode with the insertion point just before the character that the cursor was covering. Let's try typing a little something:
If you make any mistakes, you can use the <Backspace>, <Delete> and arrow keys to move to the mistake and delete it as you normally would in any text editor. You can also move the cursor around with the arrow keys and type changes into the file wherever you want. If you're using gvim, you can even use the mouse to move the cursor location or select text for deletion or replacement, but this is not the fastest way to do things in Vim, so try not to get too comfortable with it.
You'll notice that as you move to new lines in Insert mode, the turquoise tildes at the left of each line are replaced with the text that you enter. These tildes simple represent empty lines in the editor and are not part of the actual text file. They give an easy way to see where the end of the file is.
Now we want to save this important file, but if we try to enter a command, it will appear as more text in the file. That's not what we want. We need to get out of Insert mode, and to do that, we simply hit <Esc>. The <Esc> key is going to be our best friend in Vim. It will immediately exit any mode we're in and cancel any command that we're currently typing, either on the command line or in Command mode, and return us to square one in Command mode. Mashing the <Esc> key doesn't do anything more than resetting to Command mode, so if you ever forget what command you were typing or where you are in an operation, mash away.
Back in Command mode, we can save the file, which is currently just a temporary buffer in Vim, by typing :w life_advice.txt<Enter>. Again, the ':' takes you to the command line where the rest of the command is entered:
The 'w' stands for the 'write' command, and it will write the buffer to the file name specified. A path can also be included with the file name, and relative paths start at the current working directory. You can use the command :pwd<Enter> to print the current working directory on the command line, just as if you were in a shell. Handy, no? Most likely this will be the directory that you started Vim from, unless you've changed it with the command :cd <new_path>. (Assume from now on that any command on the command line is executed with <Enter>.) After saving the file, the title bar changes to the name of the file, and the command line shows some stats for the newly written file:
Once a file has been saved once, :w will write changes to the same file without needing to specify the file name. With the new file saved, we're done with the absolute basics of Vim. You can close Vim with :q, and remember that you can open a file again from the shell with vim <filename>.
In this Quick Start Guide, we only learned six commands:
Vim out of the box has some pretty good defaults, but they can be improved so next time we'll look at how to set Vim up for coding. We'll also throw in a few more useful commands to practice to start improving our editing speed.