5.6. Command History

 < Day Day Up > 

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, history, and savehist also affect the history mechanism. There are a number of ways to use the history list:

  • Rerun a previous command

  • Edit a previous command

  • Make command substitutions

  • Make argument substitutions (replace specific words in a command)

  • Extract or replace parts of a command or word

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 the section "Command-Line Editing," 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 features are important for rerunning commands. With tcsh, they are less often used, but they still work.

5.6.1. Command Substitution

Command

Description

!

Begin a history substitution.

!!

Previous command.

!N

Command number N in history list.

!-N

N th 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.


5.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


5.6.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.

Specifier

Description

:0

Command name.

:n

Argument number n.

^

First argument.

$

Last argument.

%

Argument matched by a !?string? search.

: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.


5.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*

lp ch03 biblio back

17

!cat:0 3

cat ch01 ch02 ch03

18

vi !-5:4

vi biblio


5.6.5. History Modifiers

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

5.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 word list.

:x

Quote separate words.


5.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.


5.6.6. History Modifier Examples

From the table in the section "Word Substitution Examples," 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

!17:g&

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 (quotes not shown intcsh output)

26

nroff !21:t:p

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

27

!!

nroff 01.c (execute it)


5.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. See the tcsh manpage for details.


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 program

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

5.6.7.1. Examples

Demonstrate the cwdcmd alias:

     [arnold@mybox ~]$ alias cwdcmd 'echo now in $PWD'          Set alias     [arnold@mybox ~]$ cd /tmp                                  Change directory     now in /tmp                                               Output from alias     [arnold@mybox /tmp]$ cd                                    Change back home     now in /home/arnold                                       Output from alias

Demonstrate the postcmd alias:

     [arnold@mybox ~]$ alias postcmd 'echo now starting\!'      Set alias     [arnold@mybox ~]$ ls -FC *.txt                             Run a command     now starting!                                             Output from alias     adr.gdb.backcover.txt  gdb.backcover.txt  sol-d1-2.txt    Output from command     awkhomepage.txt        sol-d1-1.txt

     < Day Day Up > 


    Unix in a Nutshell
    Unix in a Nutshell, Fourth Edition
    ISBN: 0596100299
    EAN: 2147483647
    Year: 2005
    Pages: 201

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net