Thursday, October 17, 2013

The Wonderful World of Vim

When did I first come into contact with vim? I can't remember exactly, but I suspect that it was back when I was taking my first steps into the world of Linux, probably around ten years ago. I recall finding myself at a command-line prompt and in need of editing a text file, probably a configuration file of some sort. I had edited text files in a command-line environment before, back in the days of DOS. That was straightforward enough. I did a bit of googling and found that one could use the command "vi" followed by the name of the file. So I did that. Then I got stuck. Literally, I couldn't do a single thing. Cursing, I was able to learn that one had to press "i" on the keyboard to be able to actually type into the file. Then I had to press the Escape key followed by ":wq" (minus the quotation marks) to save the file and quit that uninviting program. This was enough to cover my needs for years afterward, since I only used vim as a last resort. I was annoyed at the bizarre complexity involved with even the most simple operations and the complete absence of any menus or other visual clues. At the same time, I knew that there were a lot of people who claimed that vim was incredibly complex and useful, and defended it to the point of fanaticism. As time went by, I picked up a few neat tricks and started to enjoy vim sessions a little. Then it started to dawn on me that vim was actually quite brilliant, albeit demanding. It wasn't terribly useful in my everyday life, except for when I was tinkering with my Linux servers (and there were periods of my life when I almost compulsively would turn every old decrepit computer I could get my hands on into a server, then run it mercilessly until the aging hardware fell apart). Then I discovered a way to write pretty much everything in plain text files, thanks to LaTeX (a topic I will cover in more detail in a future blog entry). This of course meant that I could use any text editor I liked. That's when I remembered vim. A few months later, I'm more interested in vim than ever and today this book arrived:

"Practical Vim: Edit Text at the Speed of Thought" by Drew Neil. He also has a bunch of helpful short video tutorials over at http://vimcasts.org. 
I could barely stop myself from diving into it immediately. As soon as my son fell asleep, I started to devour the pages. If someone had told me ten years ago that I would love reading a guidebook for a command-line text editor, I probably wouldn't have believed it. But, I have to admit, I've become a great admirer of vim. 

So, what's the story behind this humble piece of software? The original vi was developed by a guy named Bill Joy for machines running Unix, back in 1976. Vi continued to evolve over the years until another guy, this one named Brian Molenaar, released vim (Vi IMproved) in 1991. Vim is, in simplified terms, an extended version of vi. Today, vim is in version 7.4, but still no more than a few megabytes in size. It's available for numerous operating systems, including Mac OS X, Windows and Linux. While vim continues to evolve, it still has that austere design:

So, what's so great about vim? There's of course the aesthetic dimension: I can decide on a color scheme and a font, and then I won't have to bother with a single menu of any sort. The interface is extremely minimalistic, which I love. Then there's the economic dimension: vim is completely free. Unlike the apps you see on you Android phone, that doesn't mean that you'll be pestered by ads or have to surrender your e-mail address. It's free and open-source, so just download it and enjoy, no strings attached. In addition, it's small and doesn't demand much in terms of processor power, RAM or disk space, so you can easily use it on an old computer. I could probably get it up and running on a 486 using a minimal Linux installation. Most importantly, there's the work-flow dimension: vim is a very sophisticated tool for editing text. I'm not going to say it's easy to learn, but it can be incredibly rewarding in ways that no word processor can rival. 

The screen you see if you open vim without specifying a file. 
I'm a political scientist, not a programmer. I work with large chunks of text, and I spend a lot of time tinkering with them: deleting, modifying, adding, etc. Vim takes this to perfection, and you never have to touch the mouse. You don't even have to move your hands from their original resting place on the keyboard, every command you can possibly need is only a few letters away. The most fundamental aspect of vim that enables this is the modal editing. While most word processors and graphical text editors are always in the same mode, using vim you will constantly switch between at least two, and sometimes three different modes. The first mode is "normal" mode. It enables you to, for example, navigate the text and delete parts of it. But in order to type anything, you have to enter "insert" mode, which makes the keys respond differently. The lettered keys used for commands in normal mode will then correspond to the actual letters printed on them instead. Finally, there's "visual" mode, which enables you to select parts of the text. There are a few other modes, but these three are the most important ones. 

If you're using Mac OS X or Linux, you will probably already have vim installed. To try it out, just open the Terminal (in Mac OS X, it's in the Tools folder in Applications) and type "vim". The best way to get started is to instead type "vimtutor" in the Terminal, this will open an interactive tutor that guides you through the most basic vim commands. For a more complete experience, I recommend that you download MacVim from the http://www.vim.org website.
Instead of clicking and highlighting in the text, you move the cursor around. Either one character/line at a time by using the h, j, k and l keys (you can also use the arrow keys, but the point in using hjkl is that you don't have to move your hands away from their starting position). However, moving the cursor inside long paragraphs in files that cover the equivalent of dozens of printed pages isn't very effective, but vim has a lot of other ways of moving around. Using the w and b keys, you can skip back or forward one word at a time. Using "(" and ")" you can skip one sentence at a time and using "{" and "}" you can move an entire paragraph at a time. Or, you can type the letter f, followed by a character, to skip forward directly to that character on the same line. Or, type / followed by a word to skip ahead to that word. Replace the / with a "?" to skip backward. Then I can type "n" to skip to the next matching word. This is just scratching on the surface, there are commands for deleting, copying or changing a specified number of words, lines, sentences, you name it. Commands for moving around within the text can be combined with commands for editing it in a myriad of ways, creating almost infinite flexibility. Then there's the arguably most clever command of them all, press "." to repeat the previous change. If that's not enough, there are plenty of plugins you can add. Or create your own macros. In vim, creating macros is as easy as typing two letters, then the commands you want in your macro, then another letter to stop recording the macro. You can then invoke the macro by pressing two different keys.

This is what one of my chapters looks like in vim. The code included in the text (i.e. the colored text) enables LaTeX to use this text, along with several other files, to produce a PDF file, complete with a glossary and auto-generated bibliography. The color scheme, textwidth, line numbering, etc., can be adjusted to fit personal preferences.

For me, it's like playing the piano. Not that I can play the piano, but at least that's how I picture it in my head. When I get a nice flow going while working at the command-line, my hands are flying across the keyboard, every thought is almost instantly translated into commands and text, the resulting feedback comes instantaneously. I'm not that good at vim yet, but I know that in time, being that fast is a realistic goal. Like playing the piano, it's all about practice. Fortunately, you can practice while working. 

Vim is optimized, honed and perfected for touch typists. If you can't touch type, then vim is probably not going to save you any time (it could still make for a nice-looking alternative for people on a budget though). Learning vim, just like the command-line in general, is like learning a language. There are no visual clues to provide immediate help. You have to go through the process of learning the words, or in this case, commands. At first, you'll have to think about each command, just like you have to build a sentence in your head when you're struggling to speak an unfamiliar language. With practice, the commands come as naturally as words when you speak. Thoughts enter directly onto the screen, limited only by the speed of fingers. Once you get into that flow, when the commands have become embedded in your subconscious, the feeling of doing something as mundane as editing text can be as sweet as playing music. 


No comments:

Post a Comment