Linux in a NutshellLinux in a NutshellSearch this book

8.6. Command History

Previously executed commands are stored in a history list. You can access this list to verify commands, repeat them, or execute modified versions of them. The history built-in command displays the history list; the predefined variables histchars and history also affect the history mechanism. There are a number of ways to use the history list:

The easiest way to take advantage of the command history is to use the arrow keys to move around in the history, select the command you want, and then rerun it or use the editing features described in Section 8.7.5, later in this chapter, to modify the command. The arrow keys are:

Key

Description

Up arrow

Previous command.

Down arrow

Next command.

Left arrow

Move left in command line.

Right arrow

Move right in command line.

The next sections describe some tools for editing and rerunning commands. With the C shell, which does not have the command-line editing features of tcsh, these tools are important for rerunning commands. With tcsh, they are less often used, but they still work.

8.6.1. Command Substitution

Command

Description

!

Begin a history substitution.

!!

Previous command.

!N

Command number N in history list.

!-N

Nth 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 string

Command 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, and execute modified command.

8.6.2. Command Substitution Examples

The following command is assumed:

%3 vi cprogs/01.c ch002 ch03

Event number

Command typed

Command executed

4

^00^0

vi cprogs/01.c ch02 ch03

5

nroff !*

nroff cprogs/01.c ch02 ch03

6

nroff !$

nroff ch03

7

!vi

vi cprogs/01.c ch02 ch03

8

!6

nroff ch03

9

!?01

vi cprogs/01.c ch02 ch03

10

!{nr}.new

nroff ch03.new

11

!!|lp

nroff ch03.new | lp

12

more !?pr?%

more cprogs/01.c

8.6.3. Word Substitution

Colons may precede any word specifier.

Specifier

Description

:0

Command name

:n

Argument number n

^

First argument

$

Last argument

:n-m

Arguments 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

8.6.4. Word Substitution Examples

The following command is assumed:

%13 cat ch01 ch02 ch03 biblio back

Event number

Command typed

Command executed

14

ls !13^

ls ch01

15

sort !13:*

sort ch01 ch02 ch03 biblio back

16

lp !cat:3*

more ch03 biblio back

17

!cat:0-3

cat ch01 ch02 ch03

18

vi !-5:4

vi biblio

8.6.5. History Modifiers

Command and word substitutions can be modified by one or more of the following modifiers:

8.6.5.1. Printing, substitution, and quoting

Modifier

Description

:p

Display command, but don't execute.

:s/old/new

Substitute string new for old, first instance only.

:gs/old/new

Substitute string new for old, all instances.

:&

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

:g&

Repeat previous substitution, all instances.

:q

Quote a wordlist.

:x

Quote separate words.

8.6.5.2. Truncation

Modifier

Description

:r

Extract the first available pathname root (the portion before the last period).

:gr

Extract all pathname roots.

:e

Extract the first available pathname extension (the portion after the last period).

:ge

Extract all pathname extensions.

:h

Extract the first available pathname header (the portion before the last slash).

:gh

Extract all pathname headers.

:t

Extract the first available pathname tail (the portion after the last slash).

:gt

Extract all pathname tails.

:u

Make first lowercase letter uppercase.

:l

Make first uppercase letter lowercase.

:a

Apply modifier(s) following a as many times as possible to a word. If used with g, a is applied to all words.

8.6.6. History Modifier Examples

From the preceding, command number 17 is:

%17 cat ch01 ch02 ch03

Event number

Command typed

Command executed

19

!17:s/ch/CH/

cat CH01 ch02 ch03

20

!17g&

cat CH01 CH02 CH03

21

!more:p

more cprogs/01.c (displayed only)

22

cd !$:h

cd cprogs

23

vi !mo:$:t

vi 01.c

24

grep stdio !$

grep stdio 01.c

25

^stdio^include stdio^:q

grep "include stdio" 01.c

26

nroff !21:t:p

nroff 01.c (is that what I wanted?)

27

!!

nroff 01.c (execute it)

8.6.7. Special Aliases

Certain special aliases can be set in tcsh. The aliases are initially undefined. Once set, the commands they specify are executed when specific events occur. The following is a list of the special aliases and when they are executed:

beepcmd
At beep.

cwdcmd
When the current working directory changes.

jobcmd
Before running a command or before its state changes. Like postcmd, but does not print built-ins.

helpcommand
Invoked by the run-help editor command.

periodic
Every few minutes. The exact amount of time is set by the tperiod shell variable.

precmd
Before printing a new prompt.

postcmd
Before running a command.

shell shell
If a script does not specify a shell, interpret it with shell, which should be a full pathname.



Library Navigation Links

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