Instant Replay

Everyone comes across the situation where they want to rerun a command they have just issued to the computer, but don't want to type it all in again, or they want to reissue a command with changes. Unix has a mechanism to help you with this. The technique differs according to the shell you use, so, since most users on Bama have the Korn shell (ksh), we'll show you how to work with that.

As you work, the shell keeps a history of the commands you issue. These can be recalled and rerun, with or without changes. You can see the list of commands by using:

history

You will see a numbered list of the commands you have run, the last 16 by default. A short example of the output is:

142 ls 
143 cd mydir 
144 pico myfile 
145 date 
146 pico anotherfile 
147 cp anotherfile directory1/directory2/directory3 
148 pwd 

This is system alias for the command " fc -l" (an alias is just another name for the command). There is a short command alias which reruns the previous command. Simply type "r" (just the letter "r) and press return. To rerun a command other than the last one you must use some form of the fc command. For instance, to return an earlier command, use the fc command with the -s option (to indicate no changes) and give it the number. Using the previous listing as an example, then:

fc -s 146

would rerun the "pico anotherfile" command. You can use this form without the number by using the first few letters of the command you wish to rerun. So, the last pico command could be rerun with

fc -s pi

which would pick up the last command that started with "pi" (fc -s p would get the "pwd" command).

That covers the methods to use when you want to rerun the command without changes, but now let's look at editing the command before rerunning it. On Bama, everyone is given the "vi" editor by default for command editing. The other option is the "emacs" editor. To change to "emacs", put the following two lines in your ".profile":

EDITOR=/usr/local/bin/emacs
export EDITOR

By default, EDITOR is set to /bin/vi. Don't confuse FCEDIT (discussed in the "fc" man page and unix books) with EDITOR. FCEDIT only gets used when you use the "fc -e" version of the command.

Using vi

You would work differently with the two editors, and the one you choose is entirely up to your personal preference. We'll look at the "vi" options first. In "vi", to recall the command history so that you can edit it, press "escape". At that point you can press the "k" key to move back through your commands ("j" will move you forward to more recent commands if you go too far). Each press of the key will move you back one more command. If no changes are desired, you can press enter when you get to the command you want to rerun. To edit the command, use some simple "vi" commands. When the command is edited, press "return" to execute it. Here is a small, useful list

To do this:

Type this:

Delete one or more characters

use "space" to get to character, erase with "x" key.

Insert characters

use "space" to get to proper position, press "i", type characters to be inserted. Press "escape" if you want to do more editing.

Replace a single character

use space to get to proper position, press "r", then press new character

Replace many characters (overstrike)

use "space" to get to proper position, pre "R", then type overstrike characters, then "escape"

Move forward or backward along the command

space or "l" goes forward, "h" goes backward

Get to end of line to append new text

press "A" then type new text

Any other "vi" commands will work. For instance, "dw" will delete a word. Typing "x" followed by "p" will take two letters and transpose them. Transpositions are one of the most common command typing errors.

What about Emacs?

To recall commands in Emacs, press Control-p. Repeat this to move back through your command history. If you go too far you can use Control-n to move to more recent commands. When the command line is correct, press "return". Here is a short list of emacs commands

To do this:

Type this:

Delete a character

use your regular delete key

Insert characters

type the new characters

Move forward or backward along the command

control-f or control-b

Get to the end of the line to append new text

control-e

As for the differences between "vi" and "emacs", "emacs" uses a very simple syntax, but "vi" can give you a mountain of options for moving around.

What about other shells?

You can do command line editing with other shells. Go to the man pages for "csh" to find out more about recalling previous commands in that shell. It is quite different from using the ksh.

 

* Footnote: A neat trick is to have a "vi" macro to transpose characters. Put these two lines in your ".profile", then the letter "t" will transpose characters when you edit commands, just space over to the first letter in the transposed pair.

EXINIT='map t xp'
export EXINIT

 

© 1998, The University of Alabama. The information included here is for the University of Alabama central computing facility as it was configured on the document date. It may or may not apply to other Unix systems.