Friday, July 13, 2007

Supercomputing Course: The vi Editor

Back in the good-ol' days (again, before I was born), the user would communicate with the computer through the use of punch cards. Each card would represent a single line of FORTRAN code. There would be a sort of typewriter where you would type and it would punch out dots or rectangles from the cards. (They probably had problems with hanging chads in those cards, but I can't be sure of it.) You would stack up all your cards in the right order, and then put them into a card reader for the computer to read. Your output would be on a line printer, printed on that very wide, green-and-white striped paper. Then the terminal was invented in the 1960's, and after that your program would be keyed into the machine. A very crude line editor known as ed was invented. It had a 24-line terminal and no ability to scroll back. It did the job but it was very primitive and unsatisfactory.

In the mid-1970's, Bill Joy was a graduate student at Berkeley. He and his friends were unhappy with ed, so they hacked around on it and made some improvements. Joy and his counterparts built their new editor vi on top of ed, and they gave it away to anyone who wanted a copy, which is how vi became a part of basic UNIX.

Every *nix machine has some flavor of vi on it. Most modern unices (that's the plural of unix, like index → indices) have vim, which is an improved and slightly prettier version of vi. But if you ever find yourself using an ancient dinosaur machine, it will have vi on it, guaranteed, whereas other editors, such as emacs, are not guaranteed to be there. (That being said, I've never used a machine that didn't have emacs, but you never know.) So that's why I always teach vi instead of emacs.

You can start a whole flame war over which editor, vi or emacs, is better, something I'm not interested in getting into. Really! Self-righteous jerks on both sides condemn users of the other editor, people's feelings get hurt, and it gets really ugly. I think there is room for both editors in this world. I like vi because it's ubiquitous, it doesn't require as many keystrokes, and it's a much better text editor than emacs. But emacs is definitely more versatile than vi; as I've heard some people say, emacs is a great operating system with a pretty good text editor in it. If you want an editor that you can use to play tetris or check your e-mail in addition to editing your programs, then by all means learn emacs. However, you'll have to find a different resource if you want to learn emacs, because I'm teaching vi!

The most important concept to understand about vi is that there are two modes: command mode and text entry mode. When you're in command mode, you (surprise!) issue commands, such as moving the cursor, deleting words or whole lines, or substituting one piece of text for another. When you're in text entry mode, you (surprise!) enter text through typing. If you can't remember which mode you're in, pressing the escape key will assure you of being in command mode. There are several different ways to get into text entry mode, but the most common ways are to press i, a, o, or O while in command mode. Each of those commands starts you in a slightly different location. Keep in mind that vi is case sensitive, meaning that j and J do two very different things. (See one of the resources below to find out what they do.)

To start up the vi editor, from a command prompt type vi filename (or just vi if you don't have a file in mind). When you want to save, type :w to write the file, and if you want to quit, you can type :q, or :wq to save and quit, or :q! to quit without saving.

You can pattern search with vi, to find a particular string of characters. So if you have a file and you're looking for the word 'box' within the text, in command mode type /box and it will take you to the next instance of 'box' in the file, wrapping around to the top if necessary. Using a question mark instead of a slash (?box) takes you to the previous instance, wrapping around to the bottom if necessary. You can search for any regular expression too, meaning that you can use wildcard and other special characters in your search. For example, /[Bb]ox searches for 'box' and 'Box'. I could search for those with /ox, but /ox will also turn up lox, toxic, and oxen, for example.

Let's say that you've written a C code with a variable named goto, but then you realize that 'goto' is actually a keyword in C so you can't use it as a variable name. This totally stinks, because you have like 80 instances of 'goto' in your code. How in the world are you going to find them all and change them to an acceptable variable name, such as go_to? Well, you could search for goto everywhere and manually replace it with go_to, or you could use this very compact global search and replace expression:
:g/goto/s//go_to/g
which replaces every instance of goto with go_to. (Don't forget the colon at the beginning; it's part of the command.)

I will leave you with some useful links.

Good vi Resources:
vi lovers' homepage
Learning vi – the "cheatsheet" technique
vi Cheat sheet
K Computing vi editor cheat sheet (leading to a pdf file with the simplest commands)
EMACS vs. vi: The endless geek 'holy war' (a good summary of the emacs/vi feud)

Up next: Makefiles

5 comments:

Anonymous said...

There is no God, and emacs is His editor.

Doctor Pion said...

Programs were read in on cards well into the 70s. Terminals came with a "connect time" charge unless you had your own personal computer (like the PDP in your earlier story, a machine that probably made the Apple I look fast). You literally could not afford to type in a 10,000 line program with an editor. That might use up your entire computing budget. You used the editor to revise the program or, more commonly, to revise the UPDATE file that maintained the changes in the program.

I only vaguely remember when card readers were thrown out. Early 80s on a college campus, a bit before 1980 at national labs where you had a fully networked system.

Doctor Pion said...

The observation goes back to TECO (which might be what God used to create emacs), but applies equally well to vi:

See what it does when you type in your name when in command mode.

Do not do this on a valuable file.

Katie said...

I'm actually not going to the reunion, I wish I was! My mom is taking a full two weeks off, and I just can't take that much vacation...

Rebecca said...

Doctor Pion, thanks for straightening my timeline out.