Book HomeBook TitleSearch this book

5.5. Command History

Previously executed commands are stored in a history list. The C shell lets you access this list so you can verify commands, repeat them, or execute modified versions of them. The history built-in command displays the history list; the predefined variables histchars, history, and savehist also affect the history mechanism. Accessing the history list involves three things:

5.5.1. Command Substitution

!Begin a history substitution
!!Previous command
!NCommand number N in history list
!-NNth command back from current command
!string

Most recent command that starts with string

!?string?

Most recent command that contains string

!?string?%

Most recent command argument that contains string

!$Last argument of previous command
!!string

Previous command, then append string

!N stringCommand N, then append string
!{s1}s2

Most recent command starting with string s1, then append string s2

^old^new^

Quick substitution; change string old to new in previous command; execute modified command

5.5.2. Command Substitution Examples

The following command is assumed:

3% vi cprogs/01.c ch002 ch03
Event NumberCommand TypedCommand Executed
4^00^0vi cprogs/01.c ch02 ch03
5nroff !*nroff cprogs/01.c ch02 ch03
6nroff !$nroff ch03
7!vivi cprogs/01.c ch02 ch03
8!6nroff ch03
9!?01vi cprogs/01.c ch02 ch03
10!{nr}.newnroff ch03.new
11!!|lpnroff ch03.new | lp
12more !?pr?%more cprogs/01.c

5.5.3. Word Substitution

Word specifiers allow you to retrieve individual words from previous command lines. Colons may precede any word specifier. After an event number, colons are optional unless shown here.

:0Command name
:nArgument number n
^First argument
$Last argument
:n-mArguments n through m
-m

Words 0 through m; same as :0-m

:n-Arguments n through next-to-last
:n*

Arguments n through last; same as n-$

*All arguments; same as ^-$ or 1-$
#Current command line up to this point; fairly useless

5.5.4. Word Substitution Examples

The following command is assumed:

13% cat ch01 ch02 ch03 biblio back
Event NumberCommand TypedCommand Executed
14ls !13^ls ch01
15sort !13:*sort ch01 ch02 ch03 biblio back
16lp !cat:3*lp ch03 biblio back
17!cat:0-3cat ch01 ch02 ch03
18vi !-5:4vi biblio

5.5.5. History Modifiers

Command and word substitutions can be modified by one or more of these:

5.5.5.1. Printing, Substitution, and Quoting

:pDisplay command but don't execute.
:s/old/new

Substitute string new for old, first instance only.

:gs/old/newSubstitute string new for old, all instances.
:&

Repeat previous substitution (:s or ^ command), first instance only.

:g&Repeat previous substitution, all instances.
:qQuote a word list.
:xQuote separate words.

5.5.5.2. Truncation

:rExtract the first available pathname root.
:grExtract all pathname roots.
:eExtract the first available pathname extension.
:geExtract all pathname extensions.
:hExtract the first available pathname header.
:ghExtract all pathname headers.
:tExtract the first available pathname tail.
:gtExtract all pathname tails.

5.5.6. History Modifier Examples

From the table in Section 5.5.4, command number 17 is:

17% cat ch01 ch02 ch03
Event #Command TypedCommand Executed
19!17:s/ch/CH/cat CH01 ch02 ch03
20!:g&cat CH01 CH02 CH03
21!more:pmore cprogs/01.c (displayed only)
22cd !$:hcd cprogs
23vi !mo:$:tvi 01.c
24grep stdio !$grep stdio 01.c
25^stdio^include stdio^:qgrep "include stdio" 01.c
26nroff !21:t:pnroff 01.c (is that want I wanted?)
27!!nroff 01.c (execute it)



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.