Dr. StrangeVim, or How I Learned to Stop Worrying and Love Vi


It all started with a ssh session on the University web server. I was thinking of implementing some clever thing as a cron job, and wanted to see what the crontab for our username currently looked like. As it turned out, there wasn't a user crontab, so I was greeted with the dreaded empty screen, and a sinister column of tildes down the left hand side. This could only mean one thing--the default editor was set as vi! Much wailing, gnashing of teeth and cursing followed, as I had only very briefly used vi once before. After dredging up what I could remember of how it worked from the depths of my brain, I spent a great deal of time stabbing the ESC key, typing 'wq' and getting beeped at before I gave in and opened a 'man' page to find out what I should really be doing. All of this activity was just to quit the editor.

For those of you who haven't yet experienced the Joy of Vi, let me give you a little background. Vi differs from most other editors as it is a modal editor. This means that you can't just start typing in the window. If you do, vi beeps at you but doesn't do what you actually wanted it to, which was to transfer your typed text to the screen. You have to enter 'insert' mode by typing i in order to type, and then hit ESC to leave the mode and enter commands (like quit). If that all sounds confusing--believe me, it is.

I have to explain something about myself in order to continue with my story; I have an odd compulsion to prove myself wrong. If I have a strong opinion about something, I have an irresistable urge to actually test it. It's as if I have an internal peer review process, and my internal referees start making irritating comments about me not actually having enough data on which to base my view. If you put this together with the catnip-like allure of anything geeky, you have a recipe for -wasting hours- spending hours gainfully employed in trying something out.

So this was where it all began. One moment I was muttering something about "stupid vi", and the next I was starting to think that perhaps I should actually try to get to know vi a bit better. After all, it has been the subject of a holy war, and many Perl users swear by (or at) its features, so there must be something in it--right? I decided that if I was going to do this properly, I should get hold of the most feature-packed, state-of-the-art version of vi going, so I plumped for Vim. This has some features not found in vi (multiple undo, visual selection, syntax colouring etc.), and even has a GUI, albeit an extremely minimal one.

I've spent several hours over the weekend exploring Vim, reading the manuals and following the tips posted on Vim.org, and I can honestly say that I'm hooked. Vi and Vim have a very steep learning curve[1], but--once you've struggled up the first nursery slope--the power and flexibility are phenomenal. The really magical thing about Vim is the ability to combine the 'movement' commands with editing commands. So, for example, I could type 4dw in normal mode, and it would delete the next four words from my cursor position. Or I could use c/replacement and it would delete everything up to the word 'replacement'. You can write macro-like sequences of commands by just recording what you are doing into one of a number of 'registers' and then playing the register back, you can make abbreviations, so that 'mt' gets expanded automagically to 'Moveabletype', and you can copy and paste into a number of different registers, so you're not just limited to one copy and paste buffer. External programmes can be run from inside Vim to filter or modify the current file (or a range of lines of the current file), so that you can sort lines with the Unix program sorta, or count words withwca. This has the odd side effect that you can do Ouroboros-type things like filtering your Perl script with another Perl script. It has a regex engine, so you can do complex search and replace operations, and even combine these with editing commands, so that the command is only executed on the result of the search.

I could ramble on for hours in this fashion and still only be scratching the surface, but you've either got the idea by now, or you're about to die of boredom. The first time you use 3gUw to turn the next three words to uppercase, you feel like a Jedi knight. Then you start wishing that all your text editing applications worked like that. Perl is indeed very easy to edit in Vim, with great syntax highlighting, auto-completion of variable names and smart indentation. I've installed the perl-support plugin, which provides a number of very useful shortcuts for inserting various comments and functions in your code. I've also mapped keys to perform functions like jumping between split windows and maximizing them, so that I've got something approaching an editor with tabbed windows--extremely useful for copying and pasting between two files, or for consulting documentation.

My only worry is that Vim commands might eventually occupy so much of my memory that I won't remember my own name, though I will of course still be able to toggle the case of my name in a text file (2g~w). So that's alright then.

[1] I'm reminded here of a quote from one of the 'Jeeves and Wooster' stories by P. G. Wodehouse; ??Wooster: "I say, Jeeves, that's a bit steep!" Jeeves: "Practically perpendicular, Sir."??