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. 


Saturday, October 12, 2013

Powerbook G3 "Wallstreet"

When I grew up, Macs were the ultimate computer mystery to me. In waiting rooms where glossy magazines were on display, I occasionally found an issue of MacWorld and stared at the hypnotic designs bearing Apple's logo. This was the 1990s, before the iPhones and even iPods, a time when Apple was struggling for its very survival. Macintosh computers, as they were often still called back then, were as far as I knew intended for connaisseurs, people working in graphic design and possibly authors. I had never even though of getting one. To someone who had just started to get acquainted with PCs and Windows, the Apple computers were as alien as they were fascinating. The prohibitive cost of getting one ruled them out, I was still stuck with my 486, constantly trying to find new ways to squeeze more out of that old workhorse.

Many years later, I had discovered Linux and tinkered with more PCs than I could possibly remember. The PC hardware had become very familiar, and I had often gone to electronic waste bins to dig up old computers and scavenge all the working parts I could find. Having worked so many hours with the cheap and common components and standards, I started to feel an attraction to the high-end stuff I only knew by name, things bearing designations like SCSI and Firewire. The Macs still had a completely different type of processor back in those days, called PowerPC. The PowerPC processors were based on RISC technology, which in turn was even more mysterious, the stuff of supercomputers and high-powered servers. That was something very different from the Intel-based processors I knew so well. I decided that I had to have one.

Finally, in 2007, I bought my very first Mac in an online auction. It was a Powerbook G3 "Wallstreet" from 1998 (the Powerbooks in the G3 series have different codenames to distinguish the models. Kanga was the first, followed by Wallstreet I, Wallstreet II, Bronze Keyboard and finally Pismo). It was cheap and it looked gorgeous in the pictures. A 233 Mhz G3 (PowerPC 750) processor, 64 MB of RAM and a 4GB hard drive. When it arrived in the mail, the imbecile seller had wrapped in nothing more than brown paper. I gasped in horror as I took the parcel in my hands. How could anyone treat a computer with such negligence? I carried my prize home with gentle hands, assuring it that it had would henceforth be taken care of with more affection. I was almost trembling with excitement as I plugged it in. Hours later, I was overcome with disappointment. The machine I had bought had several serious hardware defects. Unwilling to part with my Powerbook so soon, I decided to fix it.
I still think that the combination of rubber and plastic, separated by gentle curves, along with the subtle roundness of the case, is one of the best-looking laptop designs of all time.
There's something about how the display curves inward that just feel perfect, it's like it stretches out to embrace one's eyes. The splash of color in the Apple logo makes for a nice contrast while the exquisite font used for the designation is just perfect.
 The months that followed were probably the demanding computer repair I have ever undertook. Had it been any other machine, I would have discarded it after a week. But not this one. It was too beautiful in its black case, with the gorgeous font for the model designation and the amazingly inviting keyboard. Painstakingly diagnosing all the things that had to be fixed, I concluded that the hard drive was on the verge of giving up (and that parts of the operating system was corrupt), that the built-in Ethernet port was completely ruined, and that the CD-ROM drive only worked intermittently (as I recall, about 10-20% of all attempts to boot from the drive worked). In addition, the display cable was damaged, leading to an annoying flickering across the whole screen at certain angles.
The flickering screen today.
The numerous dents and marks on the bottom bear witness to the wear this machine has seen.
 I first bought a new hard drive and installed it. Efforts to find a new display cable turned out nothing. Then I had managed to get a hold of a CD with Mac OS 8 on it. It took a long time just to get the machine to boot from the disc due to the faulty CD-ROM. But I did it, patiently trying to get it to read the disc, rebooting over and over again. Then I had a working operating system, and I rejoiced. I bought an Ethernet adapter in PC Card format, but it didn't work. After doing more research, I could conclude that it was incompatible with my particular Mac. I tried to get a new CD-ROM but it was difficult to find that kind of old hardware. When I finally did locate a DVD-ROM drive for Powerbook G3s and bought it, I again noticed that it was incompatible with my Wallstreet I (it would have worked with any of the later models of the Powerbook G3). Not being one to give up, I still managed to install a few games on the machine, upgrade to Mac OS 9, and get WordPerfect and MS Office 2001 to run on it. That's when I could actually start to use it.
View from the side showing the CD-ROM drive and fan exhaust.
The hardware design has several clever aspects, in typical Apple fashion. For example, the CD-ROM drive can be replaced with an extra battery. Of course I had to get one of those. I even bought a white single-button ADB mouse for it.  I was a student at the time, and I proudly brought my Powerbook G3 to group working sessions in cafés. To everyone else, it was archaic, dented and unwieldy. To me, it was the most beautiful computer in the whole world. And it worked, thanks to my untiring efforts. The keyboard was so smooth, each key being perfectly rounded. I liked my Powerbook so much that I bought a new processor for it, a 250 Mhz version with L2 cache and 128 MB of RAM, something that promised to boost performance noticeably. I installed the hardware upgrade and was happier than ever. A few days later, the processor broke down mysteriously, forcing me to go back to the old one. Little did I know that this was only the first backlash. A few weeks later, the whole machine started to act up. A couple of months later, I could no longer boot it up. Faced with the daunting prospect of once again having to struggle with that dysfunctional CD-ROM drive, that had only gotten worse with time, I decided that enough was enough. The Powerbook G3 went into long-term storage.
This is all I get from this machine these days.
View of the rear, with the port cover open, showing the printed reset instructions (a nice touch) and the archaic ADB port.
Close-up of the font and Apple logo.
The touchpad and single button, from an era when Apple stubbornly refused to have more than one button on any mouse.
The rounded keys of one of my all-time favorite keyboards.
The extra battery drive I bought, which for a time extended the battery time considerably.
When I dug the machine up from storage to take a few pictures, I was almost tempted to try to fix it. But then I remembered the pain of having to work with its obsolete "Old World ROM" that makes it excruciatingly difficult to boot anything but Mac OS 7/8/9 discs. So, I won't fix it. But I can't throw it away either, it means too much to me for that. So after this post, it goes back to long-term storage. Maybe some day in the future I'll have time to spare and the determination required to get it up and running again. Still, we had a good run and I'll never forget the short months when I was the proud owner of a beautiful matte black Powerbook G3.

Monday, October 7, 2013

The command-line vs. my work e-mails

I think most of us can relate to the frustration you can feel when your inbox at work gets flooded. Obviously, some e-mails are important, while others are little more than a nuisance. Then there are a bunch of e-mails that are somewhat interesting, but you want to save them for later reading. One solution to this mess is to create folders for your e-mails, on the server. Then you can sort them into categories and put them in folders, allowing you to deal with more urgent stuff first. If you have to do this manually, it kind of defeats the whole purpose, so you'll want an automatic filter that does it for you, in accordance with rules that are tailored to your specific needs. Any decent e-mail client should support that (while I noticed that Apple Mail tends to be very buggy when it comes to filtering, Mozilla Thunderbird works just fine).

So far so good, but then you might find yourself in my situation. I use three different computers, one Android phone and one Android tablet to access my e-mails. Setting up and maintaining all the filters I need on these five platforms is a daunting task. Consequently, I want all my filters to be run on the server instead of on these five machines. When I access my e-mails, they're already filtered and ready, making everything so much easier.

Now, I could create server-side rules using the webmail interface my employer provides. But I won't. Because, as I have already stated in a previous post, I consider form to be more important that function. And there's no form like the command-line. So, I decided to use my Linux server (it runs on an old decrepit laptop at the moment) to handle the task for me. After some quick googling, I found the very nice "imapfilter" program, which I installed on my server. Being a command-line application, it reads the settings and filter rules from a configuration file, which you edit manually. I skimmed through a few sample configuration files that I also found by googling, and it didn't take more than a few minutes to create a set of rules that covers my current needs.

This may look complicated, but all you have to do is copy a template from someone and then adapt it to your needs. It's really not hard at all, stuff like mark_seen or move_messages are pretty self-explanatory. I had no idea how this syntax worked before, I just looked at the samples and copied relevant parts of them. Most of my rules simply move e-mails to the appropriate folder. Then there's one rule that marks unimportant e-mails as read, and then moves them to the trash folder. By the way, this is not my actual configuration file, as you may have figured out.

When I run it from the command-line in verbose mode (i.e., I instruct it to tell me what it's doing by simply adding "-v" to the command), it spews out a lovely amount of capital-lettered computer-speak. As much as I love watching that, it has to run automatically without my input, so I used the very handy application "crontab" that comes pre-installed on pretty much every Linux and Unix system I've ever tried. Now it runs automatically every ten minutes (I can set the interval to be exactly what I want, but ten minutes works for now) and when I check my e-mails on my phone or any of my computers, everything is already sorted into the appropriate folders or deleted. Wonderful, and very stylish.

The sheer amount of feedback from this application is wonderful, and the capital letters gives a most appealing old-school vibe. The numbers added to each line is a big plus, making it look even more like something out of a 1980s movie. [some lines have been partially censored for reasons related to privacy and paranoia]

Tuesday, October 1, 2013

Language vs. Visualization

I've already outlined the aesthetical and nostalgic reasons for using a command-line. But, there's more to it. On an abstract level, I like to think of the command-line and the graphical user interface (i.e. what most people use without reflecting on it) as a language-visualization dichotomy. I love languages. Not that I'm particularly good at them, I've only ever grown somewhat comfortable with two (Swedish and English). My French is abysmal and while I can read a bit of German, I couldn't put together a grammatically acceptable phrase if my life depended on it. That doesn't prevent me from thoroughly enjoying all these languages. While I read 19th century English literature, say, Oscar Wilde's "The Picture of Dorian Gray", I marvel at the exquisite polish of every single sentence. When I pick up Strindberg's "Röda Rummet", I read and re-read the pages over and over again, just to taste the words over and over again in my mind. Well-crafted prose is a vice for me.

Now, when it comes to visualizations, things get a bit more complicated for me. I enjoy visual art, colors, shapes and designs as much as anyone (probably more than most, actually). I can visualize some things in my head, such as blueprints, but I often find myself unable to describe a route, even one that I've driven dozens of times. I just can't replay the memories that I should have. I'm able to find my way, but only by actually seeing the places again. In a similar manner, I find it very difficult to visualize people's faces. I'll recognize them when I see them, but that's all. I could probably never give an accurate description of a person I'd only seen briefly.

What does this have to do with command-lines and graphical user interfaces? For me personally, it means that I prefer the flawless precision of language over the annoyance of having to remember visual details by repeating them over and over again. If I use the command-line, I will type up a command, include the appropriate parameters, hit Enter and be done with it. It usually takes a few seconds, at most. When I use anything with a GUI, I usually have to navigate through menus, and frequently memorize more or less bizarre symbols that are somehow thought to be self-explanatory. Whenever I try to use Microsoft's abominable Office 2007, I usually end up with a boiling rage inside me within minutes, frantically clicking. To me, neither the menus nor the icons make any sense. I'm not particularly interested in learning them, any more than I am interested in learning anything else I perceive as being stupid and counter-intuitive. The worst part of it all is having to submit to interacting with something so inelegant and hideous.

The command-line, on the other hand, is like speaking a familiar language with an old friend over a glass of wine. I know exactly how to pronounce the sentences, and the computer responds immediately. If I do make a mistake, I usually get a response that politely informs me of my mistake and provides me with a few clues to correct it. Learning the commands is like learning words, you don't even have to know the grammar. A few hundred words down the road, you'll be able to do the most amazing things, and it always feel so simple. Most importantly, it has an air of elegance to it.

Perhaps you're thinking that this is still a moot point, because you're not a UNIX or Linux freak and consequently you don't think you have a command-line anyway. But that's where you're mistaken. I won't go into details about the Windows command-line, simply because I detest it, but Mac OS X has lovely UNIX-based interface called "Terminal", look in Utilities in the Applications folder. It's installed by default on every Mac OS X machine.

I'll provide you with my favorite command of all time to get you started: "shutdown". Note that you need to have admin privileges to execute this command. If you don't know what that is, you can just try it. If it works, you have those privileges, otherwise it will protest. If you type the following in your Terminal:

sudo shutdown -h now

You'll be prompted for your password, then the computer will shut down promptly. Now, the beauty of this command is in its parameters. If you replace "-h" with "-r", it will reboot ("h" is for "halt", "r" is for "reboot"). And instead of "now", you can delay the shutdown/reboot by a specified number of minutes. So for example:

sudo shutdown -h +15

Will shut down your computers after fifteen minutes. Just replace "15" with the number of your choosing. Or you could type the exact time, if you like:

sudo shutdown -h 21:00

The shutdown command help can be accessed by typing "--help" after the command instead of using the parameters described by me, good to know in case you forget the exact syntax. The same applies to most other commands, by the way.


So when is this useful? Perhaps you're using a program that takes a long time to execute and you want to go to sleep (for example downloading, encoding or the like). Perhaps you want the computer to stay on while you're getting ready to leave for a social function, in case you need to check something you've forgotten, but you don't want the hassle of having to shut down the machine later when you might be in a rush. Whatever your reason, this is a very useful command and quite easy to remember, I'd say.

That's really all there is to it. Learning words. The parameters are often identical or at least similar. The words are usually self-explanatory (as is evident in the above example). Start to talk to your computer instead of haphazardly prodding it with a mouse. You'll be surprised at how easy it actually is. And most importantly, how elegant.