History: Review previous commands

General Form:

history [-r] [number]

Print out the history, or the last (first if -r is specified) number commands typed at the keyboard.

A history substitution enables you to reuse a portion of a previous command as you type the current command. History substitutions save typing. A history substitution normally starts with a '!'. A history substitution has three parts: an event that specifies a previous command, a selector that selects one or more words of the event, and some modifiers that modify the selected words. The selector and modifiers are optional. A history substitution has the form ![event][:]selector[:modifier] …] The event is required unless it is followed by a selector that does not start with a digit. The ':' can be omitted before the selector if this selector does not begin with a digit. History substitutions are interpreted before anything else — even before quotations and command substitutions. The only way to quote the '!' of a history substitution is to escape it with a preceding backslash. A '!' need not be escaped if it is followed by whitespace, '=', or '('.

Ngspice saves each command that you type on a history list, provided that the command contains at least one word. The commands on the history list are called events. The events are numbered, with the first command that you issue when you start Ngspice being number one. The history variable specifies how many events are retained on the history list.

These are the forms of an event in a history substitution:

!!
The preceding event. Typing '!!' is an easy way to reissue the previous command.
!n
Event number n.
!-n
The nth previous event. For example, !-1 refers to the immediately preceding event and is equivalent to !!.
!str
The unique previous event whose name starts with str.
!?str[?]
The unique previous event containing the string str. The closing '?' can be omitted if it is followed by a newline.

You can modify the words of an event by attaching one or more modifiers. Each modifier must be preceded by a colon. The following modifiers assume that the first selected word is a file name:

:r
Removes the trailing .str extension from the first selected word.
:h
Removes a trailing path name component from the first selected word.
:t
Removes all leading path name components from the first selected word.
:e
Remove all but the trailing suffix.
:p
Print the new command but do not execute it.
s/old/new
Substitute new for the first occurrence of old in the event line. Any delimiter may be used in place of `/'. The delimiter may be quoted in old and new with a single backslash. If `&' appears in new, it is replaced by old. A single backslash will quote the `&'. The final delimiter is optional if it is the last character on the input line.
&
Repeat the previous substitution.
g a
Cause changes to be applied over the entire event line. Used in conjunction with `s', as in gs/old/new/, or with `&'.
G
Apply the following `s' modifier once to each word in the event.

For example, if the command ls /usr/elsa/toys.txt has just been executed, then the command echo !!^:r !!^:h !!^:t !!^:t:r produces the output /usr/elsa/toys /usr/elsa toys.txt toys . The '^' command is explained in the table below.

You can select a subset of the words of an event by attaching a selector to the event. A history substitution without a selector includes all of the words of the event. These are the possible selectors for selecting words of the event:

:0
The command name
[:]^
The first argument
[:]$
The last argument
:n
The nth argument (n 1)
:n1-n2
Words n1 through n2
[:]*
Words 1 through $
:x*
Words x through $
:x-
Words x through ($ - 1)
[:]-x
Words 0 through x
[:]%
The word matched by the last ?str? search used

The colon preceding a selector can be omitted if the selector does not start with a digit.

The following additional special conventions provide abbreviations for commonly used forms of history substitution:

  • An event specification can be omitted from a history substitution if it is followed by a selector that does not start with a digit. In this case the event is taken to be the event used in the most recent history reference on the same line if there is one, or the preceding event otherwise. For example, the command echo !?qucs?^ !$ echoes the first and last arguments of the most recent command containing the string qucs .
  • If the first non-blank character of an input line is '^', the '^' is taken as an abbreviation for !:s^ . This form provides a convenient way to correct a simple spelling error in the previous line. For example, if by mistake you typed the command cat /etc/lasswd you could re-execute the command with lasswd changed to passwd by typing ^l^p .
  • You can enclose a history substitution in braces to prevent it from absorbing the following characters. In this case the entire substitution except for the starting '!' must be within the braces. For example, suppose that you previously issued the command cp accounts ../money . Then the command !cps looks for a previous command starting with cps while the command !{cp}s turns into a command cp accounts ../moneys .

Some characters are handled specially as follows:

~
Expands to the home directory
*
Matches any string of characters in a filename
?
Matches any single character in a filename
[]
Matches any of the characters enclosed in a filename
-
Used within [] to specify a range of characters. For example, [b-k] matches on any character between and including ‘b’ through to ‘k’.
^
If the ^ is included within [] as the first character, then it negates the following characters matching on anything but those. For example, [^agm] would match on anything other than ‘a’, ‘g’ and ‘m’. [^a-zA-Z] would match on anything other than an alphabetic character.

The wildcard characters *, ?, [, and ] can be used, but only if you unset noglob first. This makes them rather useless for typing algebraic expressions, so you should set noglob again after you are done with wildcard expansion.

When the environment variable HOME exists (on Unix, Linux, or CYGWIN), history permanently stores previous command lines in the file $HOME/._ngspice_history. When this variable does not exist (typically on Windows when the readline library is not officially installed), the history file is called .history and put in the current working directory.

The history command is part of the readline or editline package. The readline program provides a command line editor that is configurable through the file .inputrc. The path to this configuration file is either found in the shell variable INPUTRC, or it is (on Unix/Linux/CYGWIN) the file ~/.inputrc in the user's home directory. On Windows systems the configuration file is /Users/<username>/.inputrc, unless the readline library was officially installed. In that case the filename is taken from the Windows registry and points to a location that the user specified during installation. See https://cnswww.cns.cwru.edu/php/chet/readline/rltop.html for detailed documentation. Some useful commands are:

Left/Right arrow
Move one character to the left or right
Home/End
Move to beginning or end of line
Up/Down arrow
Cycle through the history buffer
C-_-
Undo last editing command
C-r
Incremental search backward
TAB
completion of a file name
C-ak
Erase the command line (kill)
C-y
Retrieve last kill (yank)
C-u
Erase from cursor to start of line