Saturday, December 29, 2007

Why vi: The programmers' true editor

--- Update Thu 01/21/2016 ---
I have been getting quite a few (actually a lot of) hits on this 9-yr old post. The post that linked it didn't do it in the right spirit, the author, she clearly didn't read the post even a little bit and blindly linked it to her blog going only by the URL! So I want to clear the air for my readers here. Just so you don't continue reading with the wrong mindset of:
"This Shaolin a__hole thinks he's a great programmer because he uses ancient vim; and he thinks whoever doesn't, is a loser!"
Rather, really the one and only intent of this post is to put you in the mind-frame of
"Let me read why vi is a really great editor & why it's worth learning vi!"
If you continue with the intended motive of this post, you will have learned something about an editor you don't often hear much about anymore.
--- End Update ---
This is one of the topics I promised to post on... why *every* true programmer must use vim!
Note: hereafter, I'll use vi and vim interchangeably, and mention of one should implicitly mean the other as well. Additionally, when I say vi, I usually exclude the more exotic variants and derivatives of vi. Also, this post is more about conceptual consistency rather than technical accuracy.
All this is out of my personal experience and taste for things that are simple, elegant, and yet powerful and usable. Besides, I would say that you have to be completely insane and out of your senses should you ever use any editor on Unix systems other than vi and emacs (and sam).

This post is _not_ for people who:
  • are not serious programmers
  • are not touch-typists
  • hate the world of pure-n-plain text
  • don't have to type much in their lives
  • last but not least hate length, get off the post if you are not in a mood to read lengthy stuff
In contrast, this post _is_ for the people who:
  • are serious about programming
  • are Unix nerds or are budding unix nerds
  • care about being efficient
  • want to improve their efficiency at programming
  • want to make vi way of their lives!
After using vi only for about a decade I have come to believe that vi makes any other editor (if you will call them editors at all!) like kindergarten toys!
It's not mere editor, vi, for me, is the way of life. It is one helluva piece of program that I look up to whenever I have to design something for usability, reliability and efficiency.

It's just like the old AK-47, simple, effective and even after decades of invention and superseded by million other advanced rifles, it remains the indisputable favorite weapon of assault!

For the morbidly interested, I guess it's fun to see how and why I myself started using vim.
I started using vim in late 90's, 1999, maybe. Back then, I used to idealize on an extravagantly hyped and sought after personality. Used emacs back then, because so did my morbid ideal. Then, I started reading books by the revered Richard Stevens (for the benefit of others, even after passing away from this sickened planet on 1st Sept 1999, he remains the undisputed BEST author of number of unix books, for beginners and advanced users alike. UNP, APUE, TCP/IP...).
Or maybe it was some other of my favorite books, I don't recall. Somewhere in the forewords or intros, I read that the book was completely written in vi and typeset using troff/TeX. That was the turning point in my life... for I haven't looked back ever since! Screw emacs (I wasn't loving it anyways, was kinda forced to use it for my desire to mimic some morbid people back then!). It was a little cumbersome (undoubtedly, far less cumbersome than emacs) but I knew I would do it! And I indeed did it! And so can you.

For those who don't have a clue what is vi, simply put, vi is a program that lets us the mortal programmers (not coders!) edit files on a computer, not only to write exotic and fancy obscure programs, but also lovely, lucid and romantic love letters ;-)

The first time you type vi on the shell and wait for the magic to begin... you are greatly disgusted! Holy sh_t! Where did all the 'File, Edit, ...' menus go??? All you get is a simple welcome banner and black background. What is this, some kind of charity soliciting freeware or something.
No toolbar, nothing. And people rave about this little beast! You have to hit 'i' to enter any damn text. Remember what mode are you in (normal, edit, ex), lest you end up deleting every line around. Apparently brain-dead hjkl cursor movement keys. What kind of contrived mind on earth would devise such a thing :( But use it a couple of times, and I bet you won't want to go back, EVER!

Here I've tried to put up some of the reasons why I chose to stick to vim, and would do so for the rest of my life.

0. Ubiquity
Some people, if not for anything else, use vi, only because it is ubiquitously available on any of the damn stock Unix installations! It has got a very small foot print and hence making it a part of standard installation package is perfectly natural. (And it's available for non-Unix OS'es as well).
Thus, learning just one editor ensures that you can edit files on any system in the world. Versatility, is one quality that won't hurt programmers! And a must for system administrators! I'm yet to see a sys admin who doesn't use vi!

Apparently dumb hjkl keys for cursor movements is simply *the* best invention of vi. Here's another background story...

Back when I worked at IBM, I had to work with all the exotic varieties of Unix: IBM's AIX, HP-UX, Sun Solaris, and tens of Linux distros. On many of these systems, it was impossible to use the regular arrow cursor keys to navigate, in file, as well as on shells (which apparently has to do with incorrect terminal settings, which no one ever got around fixing). Re-typing the whole 101 char command, just because you put a comma (,) instead of a dot (.) was simply maddening. It was painful, until my then Team Lead, Magesh, told me that there's really a nice way around it, 'vi editing' mode in shell. He said it's not very comfortable, but works. I said: what in God's name are you talking! Not very comfortable? !@#$ That's the next best thing to have happened to mankind since the invention of wheel and vi!
$ set -o vi
in your bash and now you have
most of vi's cursor movement and text manipulation commands at your disposal.
So much so, even GDB supports
vi-readline mode! See, I wasn't kidding about ubiquity, vi is everywhere.1. Simplicity
Really! There are only a very few basic commands and concepts that you need to master and you can navigate and edit files at lightning speeds. I hardly know a fraction of vi commands (but know them pretty well) and people grasp in awe when I edit files (I'm not posting this in self-appraisal mood, but just wanna illustrate that it indeed is impressive to see people using keyboard efficiently). Just for you to know, my love and passion for Unix and vi (second not even to martial arts) are notoriously legendary amongst my buddies and colleagues. They just don't ever get me started on either, but here you have no choice ;-)

Cut/Copy/Paste, edit, replace, search, sort, increment/decrement octal/decimal/hex numbers, number the lines, compare files, navigate directory tree, work with multiple files, encrypt/decrypt, transpose, anything, literally anything... you can do all this with stunning speeds, most of them at the touch of 2-3 keystrokes! If you can think of it, you can do it in vi.
Note: Though, some of the features are only available if you have a usable shell.

2. Speed
What some people consider as arcane hjkl cursor movement keys, I say they are just awesome. Once you get used to them, you never have to take your hands off the keyboard to move up and down the lines. And it makes a hell lot of difference in how effective you are at editing. Besides, it makes you look really cool and geeky ;-) you know... just in case you need to impress that lesser known girl sitting across your cubicle, when she just sneaks upon to checkout what are you up to! Some people argue that one spends more time thinking than at keyboard writing programs. This is not entirely true, you spend a hell lot MORE time going through, typing, editing and re-editing and modifying your code than you think you do or would do. Which means, increased efficiency and speed of doing all that would directly improve your effective throughput. Plus, it would help you keep your mind on task at hand: which most certainly will be programming or chasing that unintelligible lines of code.

3. Extensible
There are a million extensions out there to make you feel at home with vi. You most certainly don't have to use'em all. Try and stick to a few that you find good and easy to use. I personally use only a handful few, notably: buffer explorer, winmanager, CVS, and draw it.

4. Powerful
Now, if you happen to ask me why on earth would I need to sed ever, then please, vi is not for you. You are better off with medit, notepad kind of brain-dead editors. There are times, when you want to do magical text replacement, deletion and addition, modification on file of hundreds and thousands of lines, not necessarily source code, but maybe logs.

You want to number all the lines in your file, or remove them, sort numbers and rearrange them, sort text, filter them, swap the arguments in all the function calls, or even record keystrokes so that you can replay them a million times to repeat your last set of actions... you can do it in vi, with just a command or two! (If you are gaping in surprise as to who on earth would want to do all this sh_t anyway! That is, if you are one of those lesser beings who never had to do anything of this sort, you are not a true programmer. And vi is not for you). Another feature is selecting columns of texts, rather than rows the more traditional (dumb ass) editors restrict you to. Which means, you can select vertically, rather than horizontally! Of course, the degree and frequency with which you have to do these might vary, I have to do it all the time, and there's not a vi command that I have learnt and haven't used! Remember, I still don't know more than a handful of them.

5. Learning Curve
Learning vi can be a daunting task, but if you are going to be editing (viz. programming) 10+ hrs a day, for most part of your living life... then I see it as the second best investment you can make in yourself, first being learning touch typing (which I have been assuming all the while you know!). If you don't know that, then don't even bother learning vi, for you would degrade your experience to nothing but hitting mindless keystrokes, and frustrating the hell out of yourself.

vi commands are incredibly powerful for text editing, and you can even combine them to obtain the best results. While typing some text, it is a regular keyboard, but when you're back to normal mode you have the best-designed text-editing contraption there is.

Here's the nice little jpeg, that I got hold of by someone, which compares the learning curves:
If you guys think this is way too much, or all of this is BS... I haven't even talked and touched on exotic features, custom syntax-highlighting, searching, tags, file operations, color schemes, extending vi, etc.

This post is just tip of the iceberg of what vi has to offer.
You take the vi pill and you stay in Wonderland and I'll show you how deep the rabbit-hole goes.
There's lot I can go rattling about, but in the interest of brevity (???) of the post, I think it's time to let you guys off by yourselves.

But beware, if you at all get proficient at using vi, you will reach point of frustration... not with vi, but with people NOT using vi! For you won't be able to tolerate your colleagues taking for ever to point their pesky little mice to the 'Tools' menu, hit the 'Go to line' option, put in a number, hit enter and then grasp at keyboard to reach the desired word! Just to learn they entered the wrong line, starting the ordeal all over again! You would just feel like grabbing that little mouse and shoving.... well, no offense intended buddies.

I personally believe in relying on a few trusted and tried tools to get the work done, rather than to use anything that's available.
Mine are: vi, C, bash, sed, awk, perl, binutils, coreutils.
If I'm to work on a system for more than few hours, I request the owner to let me set up my arsenal, personal settings of which (viz. .vimrc) I always keep handy.

For people, who got even slightly motivated to learn this new way of life, I'm going to post an small tutorial soon, with tips on how to effectively use them. But you don't have to wait, there a zillion out there already, just google'em out and begin TODAY!

Try it out for a week or two, editing everything you want to in vi, even for smallest of things. And if you don't like it, I recommend stop chasing the elusive dream of learning vi. Since most of you use graphical interfaces of any OS, start with gvim, the Graphical VIM. Plus, editor in your favorite IDE might already support vi-emulation, RTFM to dig it out.

Interesting links:

  1. vi was, supposedly, originally written by Bill Joy
  2. vi[m] homepage for anything and everything
  3. There's more about why we !@#$ nutheads use vi[m]!
  4. Graphical tutorial, Not the best out there, and probably a little too overwhelming for beginners, yet I share it.
  5. Why emacs is not that great an editor
And BTW, here's heart felt wishes to all my readers

Wish you all a Very Happy And Prosperous New Year 2008.


Anonymous said...

The learning curve is toooo funny! lol

Sagar Bhanagay said...

Yeah, yeah... I know your passion for VI. No doubts on the abilities of VI & how handy it is. Just wondering on the title - Should it have been "A programmer's true editor" than "A true programmer's editor" ... I mean, was considering a case if we find out that K&R's use some other editor ;)
Kernighan: When I have a choice I still do all my programming in Unix. I use Rob Pike's sam editor, I don't use Emacs. When I can't use sam I use vi for historical reasons, and I am still quite comfortable with ed [laughing]; I know, that's even before you guys where born. And it's partly a question of history: I knew Bill Joy when he was working on vi.

The Shaolin said...

Hmm.... thanks a lot man. Just for K&R's sake, I should change title to what you suggested: Why vi: The programmers' true editor.
Thanks once again, you saved my a$$ hope they haven't read my post yet ;-) Also, I added the SAM to list of editors that sane people use :D

Exodus said...

enlightening post..but still no concrete examples to prove VI's superiority ! I still use editplus but i am willing to go the VI way..send some nice links on the handy features..

Ram said...

That learning curve is pretty accurate lol. And pico is the best editor ever! :)

sufferupagus said...

vim? Pointless 'improvements' and unpleasant incompatibilities with original vi. Rather stick with original vi or nvi.