UNIX in a Nutshell: System V Edition

UNIX in a Nutshell: System V EditionSearch this book
Previous: 4.4 Arithmetic ExpressionsChapter 4
The Bourne Shell and Korn Shell
Next: 4.6 Built-in Commands (Bourne and Korn Shell)
 

4.5 Command History

The Korn shell lets you display or modify previous commands. This is similar to the C shell's history mechanism. Commands in the history list can be modified using:

4.5.1 Line-edit Mode

Line-edit mode lets you emulate many features of the vi or emacs editor. The history list is treated like a file. When the editor is invoked, you type editing keystrokes to move to the command line you want to execute. You can also change the line before executing it. When you're ready to issue the command, press RETURN.

Line-edit mode can be started in several ways. For example, these are equivalent:

$ VISUAL=vi
$ EDITOR=vi
$ set -o vi	Overrides value of VISUAL or EDITOR

Note that vi starts in input mode; to type a vi command, press ESCAPE first.

4.5.1.1 Common Editing Keystrokes

vi

emacs

Result

k

CTRL-p

Get previous command.

j

CTRL-n

Get next command.

/string

CTRL-r string

Get previous command containing string.

h

CTRL-b

Move back one character.

l

CTRL-f

Move forward one character.

b

ESC-b

Move back one word.

w

ESC-f

Move forward one word.

X

DEL

Delete previous character.

x

CTRL-d

Delete one character.

dw

ESC-d

Delete word forward.

db

ESC-h

Delete word back.

xp

CTRL-t

Transpose two characters.

4.5.2 The fc Command

Use fc -l to list history commands and fc -e to edit them. See the entry under built-in commands for more information.

4.5.2.1 Examples

$ history	List the last 16 commands.	
$ fc -l 20 30	List commands 20 through 30.
$ fc -l -5	List the last five commands.
$ fc -l cat	List the last command beginning with cat.
$ fc -ln 5 > doit	Save command 5 to file doit.	
$ fc -e vi 5 20	Edit commands 5 through 20 using vi.
$ fc -e emacs	Edit previous command using emacs.
$ r	Re-execute previous command.
$ r cat	Re-execute last cat command.
$ r doc=Doc	Substitute, then re-execute last command.
$ r chap=doc c	Re-execute last command that begins with c,
	but change string chap to doc.


Previous: 4.4 Arithmetic ExpressionsUNIX in a Nutshell: System V EditionNext: 4.6 Built-in Commands (Bourne and Korn Shell)
4.4 Arithmetic ExpressionsBook Index4.6 Built-in Commands (Bourne and Korn Shell)

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System