Previous Section  < Day Day Up >  Next Section

13.5. Command-Line Shortcuts

13.5.1 Command and Filename Completion

To save typing, bash implements command and filename completion, a mechanism that allows you to type part of a command or filename, press the Tab key, and the rest of the word will be completed for you.

If you type the first letters in a command and press the Tab key, bash will attempt to complete the command and execute it. If bash cannot complete the filename or command, because neither exists, the terminal may beep and the cursor will stay at the end of the command. If there is more than one command starting with those characters and you press the Tab key a second time, all commands that start with those characters will be listed.

If there are several files starting with the same letters, bash will complete the shortest name that matches, expand out the filename until the characters differ, and then flash the cursor for you to complete the rest.

Example 13.28.

1   $ ls

    file1 file2 foo foobarckle fumble



2   $ ls fu[tab]       # Expands filename to fumble



3   $ ls fx[tab]       # Terminal beeps, nothing happens



4   $ ls fi[tab]       # Expands to file_  (_ is a cursor)



5   $ ls fi[tab][tab]  # Lists all possibilities

    file1 file2



6   $ ls foob[tab]     # Expands to foobarckle



7   $ da[tab]          # Completes the date command

    date

    Tue Feb 24 18:53:40 PST 2004



8   $ ca[tab][tab]      # Lists all commands starting with ca

    cal    captoinfo  case   cat


EXPLANATION

  1. All files are listed for the current working directory.

  2. After fu is typed, the Tab key is pressed, causing the spelling of the filename to be completed to fumble, and listed.

  3. Because none of the files start with fx, the terminal beeps and the cursor remains, but does nothing. (The terminal may not beep if that feature has been disabled.)

  4. There are a number of files starting with fi; the filenames are completed until the letters are no longer the same. If another Tab is pressed, all files with that spelling are listed.

  5. By pressing two Tabs, a list of all files beginning with file is printed.

  6. When the Tab key is pressed, the filename is expanded to foobarckle.

  7. When the Tab key is pressed after da, the only command that begins with da is the date command. The command name is expanded and executed.

  8. When the Tab key is pressed after ca, nothing happens because more than one command starts with ca. Pressing the Tab key twice lists all commands starting with ca.

13.5.2 History

The history mechanism keeps a history list, a numbered record of the commands that you have typed at the command line. During a login session, the commands you type are stored in the the shell's memory in a history list and then appended to the history file when you exit. You can recall a command from the history list and re-execute it without retyping the command. The history built-in command displays the history list. The default name for the history file is .bash_history , and it is located in your home directory.

When bash starts accessing the history file, the HISTSIZE variable specifies how many commands can be copied from the history file into the history list. The default size is 500. The HISTFILE variable specifies the name of the command history file (~/.bash_history is the default) where commands are stored. If unset, the command history is not saved when an interactive shell exits.

The history file grows from one login session to the next. The HISTFILESIZE variable controls the maximum number of lines contained in the history file. When this variable is assigned a value, the history file is truncated when it surpasses that number of lines. The default size is 500.

The fc –l command can be used to display or edit commands in the history list.

Table 13.4. History Variables

Variable

Description

FCEDIT

The pathname of the UNIX/Linux editor that uses the fc command.

HISTCMD

The history number, or index in the history list, of the current command. If HISTCMD is unset, it loses its special properties, even if it is subsequently reset.

HISTCONTROL

If set to a value of ignorespace, lines that begin with a space character are not entered on the history list. If set to a value of ignoredups, lines matching the last history line are not entered. A value of ignoreboth combines the two options. If unset, or if set to any other value than those above, all lines read by the parser are saved on the history list.

HISTFILE

Specifies file in which to store command history. The default value is ~/.bash_history. If unset, the command history is not saved when an interactive shell exits.

HISTFILESIZE

The maximum number of lines contained in the history file. When this variable is assigned a value, the history file is truncated, if necessary, to contain no more than that number of lines. The default value is 500.

HISTIGNORE

A colon-separated list of patterns used to decide which command lines should be saved on the history list. Each pattern is anchored to the beginning of the line and consists of normal shell pattern-matching characters. An & can be used in the pattern causing the history command to ignore duplicates; for example, ty??:& would match for any command line starting with ty followed by two characters, and any duplicates of that command. Those commands would not be placed in the history list.

HISTSIZE

The number of commands to remember in the command history. The default value is 500.


13.5.3 Accessing Commands from the History File

The Arrow Keys

To access commands from the history file, you can use the arrow keys on the keyboard to move up and down through the history file, and from left to right (see Table 13.5). You can edit any of the lines from the history file by using the standard keys for deleting, updating, backspacing, and so forth. As soon as you have edited the line, pressing the Enter key will cause the command line to be re-executed.

Table 13.5. The Arrow Keys

Up arrow moves up the history list.

Down arrow moves down the history list.

Right arrow moves cursor to right on history command.

Left arrow moves cursor to left on history command.


The history Built-In Command

The history built-in command displays the history of commands typed preceded by an event number.

Example 13.29.

1   $ history

    982  ls

    983  for i in 1 2 3

    984  do

    985  echo $i

    986  done

    987  echo $i

    988  man xterm

    989  adfasdfasdfadfasdfasdfadfasdfasdf

    990  id -gn

    991  id -un

    992  id -u

    993  man id

    994  more /etc/passwd

    995  man ulimit

    996  man bash

    997  man baswh

    998  man bash

    999  history

    1000  history


EXPLANATION

  1. The built-in history command displays a list of numbered commands from the history list. Any lines listed with an * have been modified.

The fc Command

The fc command, also called the fix command, can be used in two ways: (1) to select commands from the history list, and (2) to edit the commands in either the vi or emacs editor, or for that matter, any editor on your system.

In the first form, fc with the –l option can select specific lines or ranges of lines from the history list. When the –l switch is on, the output goes to the screen. For example, fc –l, the default, prints the last 16 lines from the history list, fc –l 10 selects lines numbered 10 through the end of the list, and fc –l –3 selects the last three lines. The –n switch turns off the numbering of commands in the history list. With this option on, you could select a range of commands and redirect them to a file, which in turn could be executed as a shell script. The –r switch reverses the order of the commands.

The second form of fc is described in "Command-Line Editing" on page 793.

Example 13.30.

1  $ fc -l

   4       ls

   5       history

   6       exit

   7       history

   8       ls

   9       pwd

  10       clear

  11       cal 2000

  12       history

  13       vi file

  14       history

  15       ls -l

  16       date

  17       more file

  18       echo a b c d

  19       cd

  20       history

2 $ fc -l -3

  19       cd

  20       history

  21       fc -l

3 $  fc -ln

         exit

         history

         ls

         pwd

         clear

         cal 2000

         history

         vi file

         history

         ls -l

         date

         more file

         echo a b c d

         cd

         history

         fc -l

         fc -l -3

4 $ fc -ln -3 > saved

5 $ more saved

         fc -l

         fc -l -3

         fc -ln

6 $ fc -l 15

  15       ls -l

  16       date

  17       more file

  18       echo a b c d

  19       cd

  20       history

  21       fc -l

  22       fc -l -3

  23       fc -ln

  24       fc -ln -3 > saved

  25       more saved

  26       history

7 $ fc -l 15 20

  15       ls -l

  16       date

  17       more file

  18       echo a b c d

  19       cd

  20       history


Table 13.6. The fc Command

Argument

Meaning

–e editor

Puts history list into editor

–l n–m

Lists commands in range from n to m

–n

Turns off numbering of history list

–r

Reverses the order of the history list

–s string

Accesses command starting with string


EXPLANATION

  1. fc –l selects the last 16 commands from the history list.

  2. fc –l –3 selects the last three commands from the history list.

  3. fc with the –ln options prints the history list without line numbers.

  4. The last three commands, without line numbers, from the history list are redirected to a file called saved.

  5. The contents of the file saved are displayed.

  6. Commands from the history list, starting at number 15, are listed.

  7. Commands numbered 15 through 20 are displayed.

If fc is given the –s option, a string pattern can be used to re-execute a previous command; for example, fc –s rm will cause the most previous line containing the pattern rm to be re-executed. To emulate the Korn shell's redo command, you can create a bash alias called r (e.g., alias r='fc –s') so that if you type r vi at the command line, the last history item containing that pattern will be re-executed; in this case, the vi editor will be started just as it was the last time it started, including any arguments passed.

Example 13.31.

1   $ history

    1    ls

    2    pwd

    3    clear

    4    cal 2000

    5    history

    6    ls -l

    7    date

    8    more file

    9    echo a b c d

2   $ fc -s da

    date

    Thu Jul  15 12:33:25 PST 2004

3   $ alias r="fc -s"

4   $ date +%T

    18:12:32

5   $ r d

    date +%T

    18:13:19


EXPLANATION

  1. The built-in history command displays the history list.

  2. fc with the –s option searches for the last command that began with string da. The date command is found in the history list and is re-executed.

  3. An alias (a user-defined nickname) called r is assigned the command fc –s. This means that any time r is typed at the command line, it will be substituted with fc –s.

  4. The date command is executed. It will print the current time.

  5. The alias is used as a shortcut to the fs –s command. The last command beginning with a d is re-executed.

Re-executing History Commands (Bang! Bang!)

To re-execute a command from the history list, the exclamation point (called bang) is used. If you type two exclamation points, (!!) bang, bang, the last command in the history list is re-executed. If you type an exclamation point, followed by a number, the command listed by that number is re-executed. If you type an exclamation point and a letter or string, the last command that started with that letter or string is re-executed. The caret (^) is also used as a shortcut method for editing the previous command. See Table 13.7 for a complete list of history substitution characters.

Example 13.32.

1   $ date

    Mon Jul  12 12:27:35 PST 2004



2   $ !!

    date

    Mon Jul  12 12:28:25 PST 2004



3   $ !106

    date

    Mon Jul  12 12:29:26 PST 2004



4   $ !d

    date

    Mon Jul  12 12:30:09 PST 2004



5   $ dare

    dare: Command not found.



6   $ ^r^t

    date

    Mon Jul  12 12:33:25 PST 2004


Table 13.7. Substitution and History

Event Designator

Meaning

!

Indicates the start of history substitution.

!!

Re-executes the previous command.

!N

Re-executes the Nth command from the history list.

!–N

Re-executes the Nth command back from present command.

!string

Re-executes the last command starting with string.

!?string?

Re-executes the last command containing string.

!?string?%

Re-executes the most recent command-line argument from the history list containing string.

!$

Uses the last argument from the last history command in the current command line.

!! string

Appends string to the previous command and executes.

!N string

Appends string to Nth command in history list and executes.

!N:s/old/new/

In previous Nth command, substitutes the first occurrence of old string with new string.

!N:gs/old/new/

In previous Nth command, globally substitutes old string with new string.

^old^new^

In last history command, substitutes old string with new string.

command !N:wn

Executes current command appending an argument (wn) from the Nth previous command. Wn is a number starting at 0, 1, 2, ... designating the number of the word from the previous command; word 0 is the command itself, and 1 is its first argument, etc. (See Example 11.32.)


EXPLANATION

  1. The UNIX/Linux date command is executed at the command line. The history list is updated. This is the last command on the list.

  2. The !! (bang bang) gets the last command from the history list; the command is re-executed.

  3. Command number 106 from the history list is re-executed.

  4. The last command on the history list that started with the letter d is re-executed.

  5. The command is mistyped. It should be date, not dare.

  6. The carets are used to substitute letters from the last command on the history list. The first occurrence of an r is replaced with a t; that is, dare becomes date.

Example 13.33.

1   $ ls  file1 file2 file3

    file1 file2 file3

    $ vi !:1

    vi file1



2   $ ls file1 file2 file

    file1 file2 file3

    $ ls !:2

    ls file2

    file2



3   $ ls file1 file2 file3

    $ ls  !:3

    ls file3

    file3



4   $ echo a b c

    a b c

    $ echo !$

    echo c

    c



5   $ echo a b c

    a b c

    $ echo !^

    echo a

    a



6   % echo a b c

    a b c

    % echo !*

    echo a b c

    a b c



7   % !!:p

    echo a b c


EXPLANATION

  1. The ls command lists file1, file2, and file3. The history list is updated. The command line is broken into words, starting with word number 0. If the word number is preceded by a colon, that word can be extracted from the history list. The !:1 notation means: get the first argument from the last command on the history list and replace it in the command string. The first argument from the last command is file1. (Word 0 is the command itself.)

  2. The !:2 is replaced with the second argument of the last command, file2, and given as an argument to ls. file2 is printed. (file2 is the third word.)

  3. ls !:3 reads: go to the last command on the history list and get the fourth word (words start at 0) and pass it to the ls command as an argument. (file3 is the fourth word.)

  4. The bang (!) with the dollar sign ($) refers to the last argument of the last command on the history list. The last argument is c.

  5. The caret (^) represents the first argument after the command. The bang (!) with the ^ refers to the first argument of the last command on the history list. The first argument of the last command is a.

  6. The asterisk (*) represents all arguments after the command. The ! with the * refers to all of the arguments of the last command on the history list.

  7. The last command from the history list is printed but not executed. The history list is updated. You could now perform caret substitutions on that line.

13.5.4 Command-Line Editing

The bash shell provides two built-in editors, emacs and vi, that allow you to interactively edit your history list. When you use the editing features at the command line, whether in vi or emacs mode, the readline functions determine which keys will perform certain functions. For example, if using emacs, Ctrl-P allows you to scroll upward in the command-line history, whereas if using vi, the K key moves upward through the history list. Readline also controls the arrow keys, cursor movement, changing, deleting, inserting text, and redoing or undoing corrections. Another feature of readline is the completion feature previously discussed in "Command and Filename Completion" on page 783. This allows you to type part of a command, filename, or variable, and then, by pressing the Tab key, the rest of the word is completed. There are many more features provided by the Readline library designed to help manipulate text at the command line.

The emacs built-in editor is the default built-in editor and is modeless, whereas the vi built-in editor works in two modes, one to execute commands on lines and the other to enter text. If you use UNIX, you are probably familiar with at least one of these editors. To enable the vi editor, add the set command listed below[4] and put this line in your ~/.bashrc file. To set vi, type what's shown in the following example, at either the prompt or in the ~/.bashrc file.

[4] If the set –o (editor) has not been set, but the EDITOR variable has been set to either emacs or vi, then bash will use that definition.

Example 13.34.

set –o vi


EXPLANATION

Sets the built-in vi editor for command-line editing of the history list.

To switch to the emacs editor, type:

Example 13.35.

set –o emacs


EXPLANATION

Sets the built-in emacs editor for command-line editing of the history list.

The vi Built-In Editor

To edit the history list, go to the command line and press the Esc key. Then press the K key if you want to scroll upward in the history list, and the J key[5] to move downward, just like standard vi motion keys. When you find the command that you want to edit, use the standard keys that you would use in vi for moving left and right, deleting, inserting, and changing text. (See Table 13.8.) After making the edit, press the Enter key. The command will be executed and added to the bottom of the history list.

[5] vi is case-sensitive; an uppercase J and a lowercase j are different commands.

Table 13.8. vi Commands

Command

Function

Moving Through the History File

Esc k or +

Move up the history list

Esc j or

Move down the history list

G

Move to first line in history file

5G

Move to fifth command in history file for string

/string

Search upward through history file

?

String search downward through history file

Moving Around on a Line

h

Move left on a line

l

Move right on a line

b

Move backward a word

e or w

Move forward a word

^ or 0

Move to beginning of first character on the line

$

Move to end of line

Editing with vi

a A

Append text

i I

Insert text

dd dw x

Delete text into a buffer (line, word, or character)

cc C

Change text

u U

Undo

yy Y

Yank (copy a line into buffer)

p P

Put yanked or deleted line down below or above the line

r R

Replace a letter or any amount of text on a line


The emacs Built-In Editor

If using the emacs built-in editor, like vi, start at the command line. To start moving upward through the history file, press ^P. To move down, press ^N. Use emacs editing commands to change or correct text, then press Enter and the command will be re-executed. See Table 13.9.

Table 13.9. emacs Commands

Command

Function

Ctrl-P

Move up history file

Ctrl-N

Move down history file

Ctrl-B

Move backward one character

Ctrl-R

Search backward for string

Esc B

Move backward one word

Ctrl-F

Move forward one character

Esc F

Move forward one word

Ctrl-A

Move to the beginning of the line

Ctrl-E

Move to the end of the line

Esc <

Move to the first line of the history file

Esc >

Move to the last line of the history file

Editing with emacs

Ctrl-U

Delete the line

Ctrl-Y

Put the line back

Ctrl-K

Delete from cursor to the end line

Ctrl-D

Delete a letter

Esc D

Delete one word forward

Esc H

Delete one word backward

Esc space

Set a mark at cursor position

Ctrl-X Ctrl-X

Exchange cursor and mark

Ctrl-P Ctrl-Y

Push region from cursor to mark into a buffer (Ctrl-P) and put it down (Ctrl-Y)


FCEDIT and Editing Commands

If the fc command is given the –e option followed by the name of a UNIX/Linux editor, that editor is invoked containing history commands selected from the history list; for example, fc –e vi –1 –3 will invoke the vi editor, create a temporary file in /tmp, with the last three commands from the history list in the vi buffer. The commands can be edited or commented out. (Preceding the command with a # will comment it.) If the user quits the editor, the commands will all be echoed and executed.[6]

[6] Whether the user saves and quits the editor, or simply quits the editor, the commands will all be executed, unless they are commented or deleted.

If the editor name is not given, the value of the FCEDIT variable is used (typically set in the initialization files, either bash_profile or .profile), and the value of the EDITOR variable is used if FCEDIT is not set. When editing is complete, and you exit the editor, all of the edited commands are echoed and executed.

Example 13.36.

1   $ FCEDIT=/bin/vi

2   $ pwd

3   $ fc

< Starts up the full screen vi editor with the pwd command on line 1>







            



4   $ history



    1 date

    2 ls -l

    3 echo "hello"

    4 pwd



5   $ fc -3 -1      # Start vi, edit, write/quit, and execute

                     # last 3 commands.


EXPLANATION

  1. The FCEDIT variable can be assigned the pathname for any of the UNIX/Linux text editors you have on your system, such as vi, emacs, and so on. If not set, the vi editor is the default.

  2. The pwd command is typed at the command line. It will be placed in the history file.

  3. The fc command caused the editor (set in FCEDIT) to be invoked with the last command typed in the editor window. After the user writes and quits the editor, any commands typed there will be executed.

  4. The history command lists recently typed commands.

  5. The fc command is used to start up the editor with the last three commands from the history file in the editor's buffer.

    Previous Section  < Day Day Up >  Next Section