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.

Wednesday, December 26, 2007

Screenshot

Sporting on my desktop right now is this stunning Spanish beauty, Paz Vega!

Tuesday, December 25, 2007

Visit to Pittsburgh Zoo

This was my and Vish's *very first* visit to any decent place outside CMUniverse and our den. Ever since we have been wanting to visit the Pittsburgh Zoo and PPG Aquarium.

When our grand plans to visit New Jersey went down the drains, we became even MORE desperate to tread outside. Zoo was the perfect choice, since we all love animals (for the time being, lets keep aside the argument: If you love animals so much, how come you like to see them in captivity!). So the usual trio, myself, Raju ki maa (aka Moti... err Ritu) and Raju (aka Vishal) all set out early morning for the zoo!

The Trio: Ready for some kicka$$ adventure in the rains!

First step outside the apartment and my reaction was: #@$! WTF!!! It's raining! Why the !@$# we didn't see the weather forecast (which, btw, unlike India, is pretty accurate here in States!) and plan the day :( Damn! The only plan to go to a decent place is gonna be screwed. Heavy hearted, opening our umbrellas we all tread along, just in the hopes that rain will soon stop by the time we reached the bus stop. Alas! We were wrong, and I recommended going back home, since it would be waste of time and money to visit zoo in this weather. As most of the enclosures are open to sky, I argued no animal would want to drench in this nasty rain. Vish & Ritu insisted we stick to the plan.

Still raining, we reached the Zoo, paid the entry fee ($8) and I asked the guy if there was even slightest chance we could glimpse even half of the animals advertised on their website. He gleefully said 'Most Certainly, Sir'! Still skeptical, we moved on, silently cursing ourselves for just throwing $$$s and time down the drain. We certainly should be crazy to do this... Looking at the map, we strode inside the zoo...


A way into the wilderness...

Lo and behold! Good Lord! Was my first reaction... and all the gloominess sublimed in thick moist air! It was raining, unwieldily maneuvering our umbrellas and cameras, we tried to capture the a really really huge tiger, into our virtual world of digital photography. But wait, he refused to go away... Blissfully playing and rolling around (videos to come soon, once I edit out noise and put a real cool NatGeo-like music in!). Wow! We all smiled at each other, this is gonna be freaking awesome! Thanks to Vish and Moti, rain, after all, wasn't a deterrent for these mighty beasts! The zoo, I should take a moment to say, is REALLY amazing!
I believe the $8 fee is way too little for the quality of zoo this is! It's so well maintained, and all the animals, seem, to have been taken care of pretty well.

3 wild-n-mighty beasts in one single frame!

3 Cheers for the tiger!

Kings of the kings: rather listless today :(
(probably the 2 had a fight last night over what channel to watch!)

Then came a VERY huge artificially created Tropical Rain Forest enclosure, where there were a variety of monkeys. The place is meticulously controlled for it's resemblance to actual rain forest: the light, humidity, ambiance, temperature, etc. Pretty impressive. Outside, while having hot (but absolutely tasteless) burger, we could watch this huge bear:

Kodiak bear (from Grizzly bear family) trying the strength of his mighty teeth on the log!

The indoor aquarium was just breath taking! You name a fish, tiny, small, big, huge, really really huge, anything, and it was here!
Someday, I'll have a this waterfall and aquarium kind of arrangement in my house too!
Waterfall and the fish aquarium

Beautiful Emperor Angel Fish

This fish is REALLY BIG!

Straight out of a fairy-tale book: Sea Dragon

Another beautiful indoor aquarium.


Touching the sting ray and small shark!!!

Family reunion :-P

Suse Linux!!!

But *the* best part was seeing Polar Bear playing with a rubber ball! It is one of the most spectacular wildlife moments I've seen! Now, as you can see, the zoo is REALLY very well architect'ed: you could see these polar bears from top, kinda bird's eye view, or through a glass enclosure, where water level reaches your knee, or from an underground glass tunnel, which is MOST exciting of the views! You can so closely see the Bear's graceful movements that you will be mesmerized, just like us. We spent good part of 45 minutes just observing the bear!
Here's a video:




Here are more videos for you to watch: Pitt Zoo Videos. Plus, Vish has uploaded the whole album on Picasa, a good 325 photos from his and my cameras. I'll do it soon too. Meanwhile have a look at the pictures and videos Vish has uploaded.

Maa, we are tired, ready to go back home!

Oh, and yes, Wish you all a Very Merry Christmas and New Year!

Links:

0. Complete Album
1. Few of our videos
2. Pittsburgh Zoo Videos

Wednesday, December 19, 2007

Done with exams!

Back after a long hiatus. The grueling final exams of the Fall '07 had kept me off blogging. The semester wasn't bad, and I'm satisfied with it, though, there are things I would be improving on the upcoming semester, which is what I'll use the whole of winter vacation for. This vacation we (as in me and my roomie, Vish, aka Jay) intend to slog harder than we did through the semesters to achieve the mammoth goals we have set for 3-4 weeks!

It has started snowing here, a couple of weeks back, actually. And I find it very exciting because this is the first time I'm seeing snowfall!!! It looks beautiful around, everything is just white, PURE WHITE! No adulteration! But it's not easy to survive in snow, once outdoors, you gotta be careful to be well equipped: thermals, weather-proof jacket, good shoes (if not snow shoes), nice head-cap, muffler, etc. The wind chills are the worst part, sub-zero winds blowing at tens of kms/hr are not easy to bear. And just 2 days back I had been to Robinson Mall's Walmart, and the wind actually strayed us away from our paths while we were walking on the highway!!!

Here are some photos, see end of the post for complete albums.


This is CMU's legendary ECE department, aka Hammerschlag Hall.



Lying down for a short nap...
Naah... it's pretty cool down and you bet don't you stay any longer than a few moments, lest it freezes your nice little butt into a hard rock.

Once lush green lawns of CMU are now all snow-clad, no trace of green colour anywhere!!!
Cute snowman that some beautiful girls made (wish they too were in the photo :-P )


Thanks to thermals, Vish's attempts to freeze his a$$ is foiled!

Snow everywhere!
This is how parkway looks like after snowfall through whole night. Taken from Namrata's house.

Brave boy!
Only the braves dare tread in such heavy snow and lethal wind chills...
On our way back home, after we went grocery shopping at Walmart the previous day, that's what's in that HUGE ruck-sack.

Raju ki maa (scolding): Badmash Raju, drinking wine?? Aaa???

Apart from these, writing my own shell, exploiting buffers to open an interactive shell on remote system, developing small yet completely functional proxy server, and a zillion other projects kept me busy through the post-midterm part of the semester.

What I promise my readers this winter break are:
0. vim: a beginer's guide
Only because, I believe true programmers use ONLY vi[m]
1. Why vim?
Why I believe 0 is so true
2. New and revamped homepage
3. Why simplicity rocks: in light of Unix design

As I promised, and as usual if you have read so far, then you deserve to get bored a bit more... here are some albums that Vish has put up:
0. Snow in Pittsburgh
1. More Snow in Pittsburgh

Photo credits: Vish