Friday, December 09, 2005

Ancient History? RTFM

Just as our bodies have little, atavistic features that recapitulate our phylogeny, so does the shell. Back when dinosaurs roamed the earth, there was a shell called the "C shell" (cute name, eh?), that had a clumsy-but-complex history mechanism built around the ! key. Bash retains that mechanism, alongside its more modern one.

About the only useful thing left from it is !!, which means "redo the last command."

If you want to read about it, though, see the man page.
man bash
Impressive, huh?

I don't just mean the HISTORY EXPANSION section, which is 120 lines, starting around line 3172 on my machine. I mean the fact that the man page is over 3000 lines long. (Mine's close to 5000). There's a lot there.

It's usually easier to read a man page this big in a text editor, so stash a copy somewhere. Send it through col first, to remove funny characters.
man bash | col -b > /tmp/bash.man
vi /tmp/bash.man +/"^HISTORY EXPANSION"
On tomorrow, the seventh day, you should rest. The day after, I'll start to grade insensibly into more advanced topics: I'll talk a bit about processes, and how to handle them in the shell.

Thursday, December 08, 2005

Command-line Comments

Shell comments start with #. Important for scripts, but useless on the command-line, right? Wrong.

Suppose you've typed most of a line, but you need to stop and check on something -- how some file or command is spelled, for example.

Just go to the beginning of the line (Home), type a #, and press Enter. What happens? Nothing. It's a comment.

But it goes into the history. When you've finished checking on whatever it was, recall the command, make any needed modifications, delete the #, and press Enter again. Voila!

If you're using vi-style editing, use Esc to switch to command mode, then type the command #. Bash will insert a # at the beginning of the line and give you a new prompt for the next command.

In otherwords,
emacs: Home # Enter
vi: Escape #

Wednesday, December 07, 2005

History at Full Speed

If the command-line editor is really a full-featured history editor, then history should be searchable.

Is this possible? Yes sirree.

In emacs mode, type ^R, then start typing the string you're looking for. You'll get the normal, emacs incremental search. If you find the string you want, but it's not the command you want, press ^R again to get the next instance.

In vi mode, use vi's search command, /. Find the next command containing that string with n.

If there's a command you issued a few minutes ago and you want to bring it back, you don't have to type up-arrow after up-arrow to find it. You can recall it by just typing in a few characters from the command and searching.

vi doesn't have incremental search, but it turns out that some of the emacs-mode commands, like ^R, still work in vi-mode, so it's often worth trying them.

No, this isn't logical. Oh, well.

Happy Pearl Harbor Day!

Tuesday, December 06, 2005

Editor Choice

If you prefer vi to emacs, do this: set -o vi (If you're just experimenting, set -o emacs gets you back to emacs mode.)

Me, I use vi-mode in the shell. I've been using vi for so long, it's in my firmware. I don't have to think about how to edit files with vi, it just comes out of my fingers. Editing command lines comes just as naturally.

Do remember that vi is a modal editor. When you begin to type, you're in insert mode. To get to command mode, type Esc. Once you're in command mode, the familiar vi commands just work. End of line? $ Beginning of line? ^. Back two words? 2b. Change the word under the cursor? cw.

As soon as you type Enter, the current line, as you see it, is handed to the shell and executed. You're put back in insert-mode with a new, empty line.

If you prefer TECO or WordStar or some other editor, you're out of luck. emacs and vi come standard, but nothing else. Sorry.

On the other hand, if you want to learn vi or emacs (because, say, you're coming from another operating system), then the shell is an excellent teaching tool. Each history-editing command you learn will work in the editor, and each text-editing command you learn will work on the command-line.

As one last trivium, the old line editor, ex, feels just like vi-mode history editing. Some day, you'll be stuck using a line editor to recover a system. Instead of being nervous, you'll find yourself completely comfortable. It'll feel just like editing your history.

After you're comfortable with vi-mode history editing, go try ex; you'll see what I mean.

Monday, December 05, 2005

Editing History


The Moving Finger writes; and, having writ,
Moves on: - Omar Khayyam.


You've used arrow keys and the backspace/delete keys to edit command lines. They're just the tip of the iceberg.

To give you access to the most powerful command-editing facilities, I have to give you a new viewpoint. Let me see if I can take you there.

Imagine all the commands you've issued as a file. history shows you this file, with line numbers.

Next, imagine that when you type, you're actually editing that file. There's an empty line at the bottom of the file; as you type, you're typing into that empty line. When you press the carriage return, you move on to the next empty line.

You're in a text editor with a screen so narrow it only displays a single line.

As you press the arrow keys, you're navigating through a file.

But since you are in an editor, you can edit the lines you see. Like most editors you've used, it understands the arrow keys, backspace, and delete, but it understands a lot more -- the Home and End keys for example.

How powerful an editor is it? I'll cut to the chase: it's emacs. Want to go to the beginning of the line? ^A (control-A). End of line? ^E. Delete to end of line? ^K.

Most familiar emacs commands will work.

One-line emacs.

Sunday, December 04, 2005

History


God alone knows the future, but only an historian can alter the past.
- Ambrose Bierce

This week, we're going to give an introduction to history. History is, in turn, the introduction to everything else.