Skip to content

Linux Command Line History Shortcuts: A Quick Guide

1 October 2022
Command Line Shortcuts History

Learn how to use Linux command line History Shortcuts to save time and be more productive. This quick guide covers the most essential shortcuts, including how to navigate through your history, repeat commands, and search for specific commands.

History Shortcuts

  • bash stores a history of commands executed
  • history lists all commands
  • history N lists the last N commands
$ history 5
14  cd  /tmp
15  ls  -l
16  cd
17  cp /etc/passwd
18  vi passwd

History

In addition to the basic command recall with the arrow keys, the bash history mechanism supports a variety of advanced ways of retrieving commands from the history list.

!!      Repeats last command
!char   repeats last command that started with char
!num    repeats a command by its number in history output

To View past commands with the history numbers, use the history command.

Other slightly more advanced history tricks include:

!?abc    repeats last command that contains (as opposed to ?started with?) abc
!-n      repeats a command entered n commands back

Use ^old^new to repeat the last command with old changed to new, for example:

[student@stationX ~]$ cp filter.c /usr/local/src/project
[student@stationX ~]$ ^filter^frontend
cp frontend.c /usr/local/src/project

More History Tricks

  • Use the up and down keys to scroll through previous commands.
  • Type Ctrl-r to search for a command in command history.
    • (reverse-i-search):
  • To recall last argument from previous command:
    • Esc (the escape key followed by a period)
    • Alt (hold down the alt key while pressing the period)
    • Can be pressed multiple times
    • !$ (only valid for the last command)

History

Using your history is a great productivity-enhancing tool. Linux users who develop a habit of using their history can streamline and speed their use of the shell. Try playing with the keystrokes listed above.

You can ignore repeated duplicate commands and repeated lines that only differ in prepended sapces by running the command below, or by adding it to your ~/ .bashrc file.

[student@stationX ~] export HISTCONTROL=ignoreboth