First steps with Vim

Ebook creation has always involved a lot of html and css editing. Because it also tends to require InDesign, I’ve been unable to do much of it in Linux. So, for the past many years, I’ve been using Notepad++ as my editor of choice. Now that I’m doing Code School, I’m spending more and more of my time in Linux. I fumbled around for a while with various editors: Gedit was the first one I started using, but it lacked the power of Np++. Eventually I found my way to Geany, which worked quite well, and eventually from there to Brackets.
When I started tinkering around more with the Ubuntu server I’ve got running at home though, I found I needed something I could use from the command line without a GUI. For a while I was happy enough with Nano; I wasn’t doing much, and it did the job well enough. With more time in Code School and more time using the terminal, I started to feel like I needed something more powerful. I’d heard of Vim, and even seen it once or twice accidentally, mostly when adding messages to git commits, and it always seemed arcane and pointlessly difficult. People kept singing its praises though, so one day I decided that my brain wasn’t melting quite enough learning about closures and scope, and that I needed to make everything much harder on myself, so I switched over to Vim.
Vim is very different from any other text editor you have ever used. It’s the iMproved version of Vi, which is a text editor that is nearly old enough to have kids who can hit the bar. It’s built for command-line use, so no clicking on menus or using your mouse to navigate the file. It is what’s called a “modal editor”, meaning that it has several different modes that it operates in. Switching between modes will completely change the behavior of the editor. There are a bunch of modes, most of which I know nothing about yet. The basic important ones are normal mode, insert mode, and visual mode.

Insert Mode

This is the mode that is most like a “normal” text editor. When you’re in normal mode, you have a cursor, and you can type stuff where the cursor is. You can backspace and delete things as well. You can even use the arrow keys to move around in your document (if you don’t mind the snickers and eyerolls from experienced Vim users—the arrow keys are taboo because they’re so far from the home row). You could conceivably spend the majority of your Vim time in insert mode, and get your work done. Slowly. This would be a lot like having a brand-new Nexus 5 and only using it to make phone calls as a clock. As it turns out, insert mode is something you should strive to minimize your time in. Most of the time, you want to be in.&hellpu;

Normal Mode

This is the mode that Vim starts in, which probably accounts for some of the frustration of new users. Remember how Vim is a modal editor, which means that the way the program works changes based on what mode you’re in? Vim is not kidding around about this. Everything is completely different in normal mode. This is where you do all your editing, your text manipulation, your navigation—everything but actually writing stuff. In order to optimize keyboard usage (remember, it’s traditionally the only way to interact with Vim), Vim makes every virtually every individual key a hotkey for something, and puts the most important ones on the home row. h, j, k, and l are your new arrow keys—they move left, down, up, and right respectively. x deletes the character under the cursor. i gets you into insert mode if you really have to type something (just hit Esc to get back to the goodness that is normal mode). Other keys do other things—this is getting long already, so we’ll leave discussion of the handiest keys that I’ve learned about for another post.

Visual Mode

This is used for selecting chunks of text. Simply hit v from normal mode (and from what I know so far, it seems like all the other modes are accessed through normal mode), and then move around as you would in normal mode. You should see everything between where your cursor is now and where it was when you entered visual mode get highlighed. From there, you can do all sorts of stuff with your selected text. At this point, that probably just means deleting it.

Resources

There’s a ton of great info about Vim out there on the web. Probably the easiest place to start, though, is the stuff that comes with Vim. Once you’ve got it installed, just enter

at the terminal and you’ll be taking your first steps down a long, long road. Once you’ve checked that out, Vim Adventures should be a walk in the park (at least up through the 3rd level, which is as far as you can get without paying). If you’re itching for still more, it’s hard to go wrong with the in-editor help, accessed by typing

from normal mode. After that, there are any number of online resources, like the Vim Tips Wiki. And finally, you can always just come back here to check out the next installment.

Leave a Reply

Your email address will not be published. Required fields are marked *