$_ CLI In Depth
Learning progress 0%
lesson performance tmux vim git

History & Readline Shortcuts

5 min read Module 18: Performance, Debugging & Power Tools Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:14

History is a list of past lines

Bash keeps a list of the lines you enter in an interactive session. The list lives in memory while the shell runs, and it is saved to a file, usually .bash_history in your home directory, when the session ends. The variables HISTSIZE and HISTFILESIZE set how many lines are kept in memory and in the file. The history builtin prints the list with numbers, which you can reuse with an exclamation mark followed by the number.

History is an interactive feature. A script run with bash does not record its commands, because the history is turned on only for interactive shells. That is why the lab does not show history in a checked exercise: the checks run scripts, and the history of a script is empty.

History expansion

History expansion is a set of shortcuts that rewrite a line before it runs. The double exclamation mark repeats the last command, and the exclamation mark followed by a string repeats the most recent command that starts with that string. The word designator !$ means the last argument of the previous command, which is useful after a mkdir or a cp. Expansion happens before quoting is removed, so single-quoted text is left alone.

BASH2 lines
mkdir -p reports/2026
cd !$

Run these as two separate commands. The !$ in the second line expands to the last argument of the previous history entry, which is reports/2026. History expansion works on earlier lines, not on an earlier command within the same line, so mkdir -p reports/2026 && cd !$ would pick up the last argument of whatever you ran before it. Bash prints the expanded line before running it, so you can see what happened.

Searching history

Ctrl-r starts a reverse incremental search. You type part of a command, and readline finds the most recent line that contains it. Press Ctrl-r again to go further back, and press Enter to run the match or an arrow key to edit it. The search is faster than scrolling, and it works across sessions once the history file is saved.

The history builtin also accepts options. history -a appends the lines of the current session to the file now, which is useful when you run several terminals and want each one to see the others.

Readline names and bindings

Readline is a library, and every editing action has a name, such as beginning-of-line, end-of-line, and history-search-backward. A key is bound to a name, and the bindings are shown by bind -p, which prints them in a form that can be saved. bind -l lists the names of all readline functions, and bind -P shows which keys trigger each one.

BASH1 lines
bind -l | grep history

The control key notation in readline is Ctrl followed by a hyphen. Ctrl-a is beginning-of-line and Ctrl-e is end-of-line, which are the two most useful motions on a command line. Ctrl-u deletes from the cursor to the start of the line, and Ctrl-k deletes to the end.

inputrc: saving your choices

Readline reads its settings from a file named inputrc, usually in your home directory. A line in the file either sets a variable, such as editing-mode, or binds a key sequence to a function or a macro. The bind -f command reads a file on demand, which is handy for testing a binding without restarting the shell.

BASH1 lines
bind -f my-inputrc

A macro binding has a quoted key sequence on the left and a quoted string on the right, and the string is typed as if you had entered it. Escape sequences such as backslash C followed by a hyphen mean Ctrl, and backslash n means Enter.

Tip

Set editing-mode to vi in inputrc if you prefer vim-style editing of the command line. The change applies to readline and not to the shell language, so your scripts are unaffected.

Practice on the lab machine

The lab shell is not interactive during a checked step, so the exercises read the binding tables and load a small inputrc file with bind -f. Use the help builtin to read the bind command, and the online bash manual for the full list of readline names and variables.

Expansion that avoids retyping

Some expansions take the words of an earlier command. The sequence !:0 is the command name, and !:2 is the second word. The caret sign followed by a pattern replaces the first match in the previous command, which helps fix a typo in a long path without retyping the whole line. These expansions are printed before the line runs, so an unexpected substitution shows up on the screen rather than in a file.

Keeping history useful

Commands that start with a space are not saved when the variable HISTCONTROL includes ignorespace, which is a common way to keep a password or a token out of the history file. Duplicate lines can be removed with ignoredups or ignoreboth. These settings belong in your startup file, so they apply to every interactive shell you open.

Searching by prefix

Readline has a prefix search that works on the text before the cursor. Binding a key to history-search-backward makes the up arrow find only the lines that start with what you have typed, which is faster than scrolling through every recent command. The function names are the same as in bind -l, so a binding can be tested with bind -f before it is made permanent in the startup file.

Vi and emacs modes

Readline has two editing modes. Emacs mode is the default, with the control key motions described above. Vi mode gives you normal and insert modes on the command line, similar to vim. Switching modes changes how the keys behave, but it does not change how the shell parses a line, so a switch never changes what a command does.

Terminal lab · 3 steps · real Linux in your browser

Inspect readline bindings

List readline function names and bindings, and write an inputrc file that adds a new key binding.

01
List the readline function names
bind -l prints the names of every readline function, such as beginning-of-line and history-search-backward. Save the list: ``bash bind -l > functions.txt `` Open functions.txt and search for history-search-backward.
Show hint for step 1
Readline functions are what keys are bound to. The names are the same in every shell that uses readline.
02
Save the default key bindings
bind -p prints the bindings in a form you can save into an inputrc file. Save them and look for the line that binds a key to beginning-of-line: ``bash bind -p > bindings.txt `` Open bindings.txt. It should contain a line that names beginning-of-line.
Show hint for step 2
bind -p prints each binding as a function name, and bind -P shows the keys for each function. Either works for this step.
03
Write and load a custom binding
An inputrc file holds readline settings. Write one line that binds Ctrl-x then p to run pwd and press Enter. Then load it with bind -f: ``bash cat > my-inputrc <<'EOF' "\C-xp": "pwd\n" EOF bind -f my-inputrc `` bind -f exits 0 when the file loads. You do not have to press the key in this lab.
Show hint for step 3
The quoted left side is the key sequence, and the right side is what it inserts. \C-x means Ctrl-x and \n means Enter.
Check yourself Question 1 of 4
Concept Check Reverse search key recall

What does Ctrl-r do at the bash prompt?

Recap & Key Takeaways

History keeps a list of interactive commands, and the double exclamation mark and !$ expand to earlier commands or their arguments.
Ctrl-r starts a reverse search through history, and history -a writes the current session to the history file.
bind -l lists readline function names, bind -p prints bindings in an inputrc format, and bind -f loads a file.
Readline settings live in an inputrc file, and bind -f applies a file without restarting the shell.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue