Introduction to Unix

../_images/ksus2.jpg ../_images/UNIX_blocks1.jpeg

5.8. history

Linux for Programmers and Users, Sections 6.4.1

history

Bash tracks the commands that you type. You can use this to re-enter or edit a previous command. The history of past commands is normally kept in the ~/.bash_history file.

SYNOPSIS

history [-c] [n]

With options, the whole history file is displayed, the size of which is capped at 1000 lines. The last n lines may also be displayed.

OPTIONS:

-c

Clear the history

5.8.1. fc

fc

Display or edit and re-execute command from the history list that matches a command searched for. See help fc for more details.

SYNOPSIS

fc [-e ename] [command]

fc -s [pat=rep] [command]

Execute the previous command if no command is specified. A useful alias is r='fc -s'. The user can type r cmd to re-execute the last command beginning with cmd and typing r re-executes the last command while r foo=bar cmd re-executes cmd replacing foo for bar in the command.

OPTIONS:

-e ename

Select which editor to use. Default is FCEDIT, then EDITOR, then vi.

-s [pat=rep]

Replace pat with rep before executing.

5.8.2. Command Line Editing

Previous entered commands may also be recalled and edited using text editing commands before re-executing. The default editor for the BASH shell is emacs, but it can be changed to vi by setting set -o vi in the ~/.bashrc file.

In both emacs and vi modes, up and down arrows on the keyboard may be used to find previous commands. Left and right arrows may also be used to move around on the command line.

In vi mode, press the ESCAPE key to enter vi’s command mode. Then j and k can move between commands and the within the command h, l, b, w, and e are used to move left, right, back word, next word, and end of word. To modify the command, use standard vi editing commands, such as cw - change word, 3cw - change 3 words, dw - delete word, x - delete character, i - insert, a - append, etc.