Table of Contents
Why History and Autocomplete Matter
Working efficiently in the shell is mostly about not re‑typing things:
- History lets you reuse and search previous commands.
- Autocomplete (also called tab completion) lets you finish commands, paths, and more with the
Tabkey.
These features save time, reduce typos, and make the shell feel much more “interactive”.
Most examples here assume Bash, the default shell on many Linux systems. Other shells have similar features but different details.
Basic Command History Usage
Bash keeps a list of the commands you’ve typed. You can move through this list and re‑run or edit commands.
Navigating History
↑(Up arrow): Go to the previous command.↓(Down arrow): Go to the next command.Ctrl + P: Previous command (same as↑).Ctrl + N: Next command (same as↓).
You can:
- Press
↑multiple times to find an old command. - Edit it if needed.
- Press
Enterto run.
Viewing the History List
Run:
historyYou’ll see a numbered list:
120 ls
121 cd /etc
122 cat hosts
123 historyYou can re-run a command by its number (see below).
Re-Running Commands from History
Using `!!` (last command)
!!expands to the previous command.
Examples:
sudo !! # Run the last command again with sudo
If your last command was apt update, sudo !! becomes sudo apt update.
Using `!number`
Each history entry has a number (shown by history). You can run it again:
!122 # re-runs history entry number 122Using `!string`
Bash will search the history for the most recent command that starts with string:
!ls # re-runs the last command that began with 'ls'
!sudo # re-runs the last command that began with 'sudo'
Be careful: history expansion runs immediately when you press Enter. Always check before using it in critical situations.
Searching History
Reverse/Forward Incremental Search
You don’t have to scroll one command at a time; you can search as you type.
Ctrl + R: Start reverse search (backwards in time).Ctrl + S: Start forward search (forwards in time; sometimes disabled in terminals).
Procedure:
- Press
Ctrl + R. - Start typing part of a previous command (e.g.
apt). - Bash shows something like:
(reverse-i-search)`apt': sudo apt update- Keep pressing
Ctrl + Rto cycle through older matches. - Press:
Enterto run the shown command.Right arroworCtrl + Eto accept but edit before running.Ctrl + GorCtrl + Cto cancel search.
This is one of the most powerful ways to reuse past commands.
Searching with `history | grep`
You can search the history output using standard tools:
history | grep ssh
history | grep "apt install"
When you see the command and its number, use !number to re-run.
Clearing or Editing History
Temporarily clearing the current session’s history
history -cThis clears the in-memory history of the current shell session (already-saved lines in the history file may remain; see below).
Deleting specific entries
You can delete entries by editing the history file (explained later) or using history -d:
history # find the number
history -d 211 # delete entry 211Avoiding saving a specific command
Common tricks:
- Start the command with a space (works when
HISTCONTROLincludesignorespace):
secret-command hereNote the leading space.
- Or remove it for the current session after you run it:
history -d $(history 1 | awk '{print $1}')(You don’t need to memorize this now; just know that you can discard sensitive commands.)
Where History Is Stored (Basics Only)
Bash typically stores command history in the file:
~/.bash_history~is your home directory.- The file is plain text; you can open it with
cat,less, or a text editor.
History behavior is influenced by environment variables like HISTFILE, HISTSIZE, and HISTCONTROL, which are configured in shell configuration files. Those are covered in more depth in their own chapter; for now, just remember:
- Your commands are usually saved between sessions.
- If you don’t want something saved, don’t type it carelessly (especially secrets and passwords).
Basic Autocomplete (Tab Completion)
Autocomplete in Bash is mainly triggered with the Tab key.
Completing File and Directory Names
Type part of a name and press Tab:
cd Doc<Tab>
If there is only one match (e.g. Documents), Bash completes it:
cd Documents/If there are multiple matches:
- Press
Tabonce: nothing (or a partial common prefix) may add. - Press
Tabagain: Bash will show all possible matches.
Example:
ls Do<Tab><Tab>
Documents Downloads
Now type more letters until it's unique, then press Tab again.
Completing Command Names
If you type the start of a command and press Tab, Bash will try to complete it with executable names found in your PATH:
gre<Tab> # might become 'grep'
If there are several possible commands starting with gre, press Tab twice to list them.
Autocomplete for Commands with Arguments
Bash can complete not only filenames but also:
- Command options
- Usernames
- Hostnames
- Git branches
- Package names
- And more
Much of this is enabled by the “bash-completion” system (often installed by default on modern distros).
Example: Options and Files
For many commands, pressing Tab after a dash (-) will show possible options:
ls -<Tab><Tab>
You might see a list such as -a -l -h -R ... depending on your system’s completion scripts.
Examples
sshcan complete hostnames from~/.ssh/configorknown_hosts:
ssh ser<Tab> # might complete to 'server1' or similarsudocan complete commands:
sudo sy<Tab> # might become 'sudo systemctl'Available completions depend on what completion scripts are installed and enabled.
Useful Keyboard Shortcuts Related to History & Completion
These shortcuts help combine editing with history and completion:
Ctrl + A: Move cursor to the start of the line.Ctrl + E: Move cursor to the end of the line.Alt + F: Move forward one word.Alt + B: Move backward one word.Ctrl + U: Delete from cursor to start of line.Ctrl + K: Delete from cursor to end of line.Ctrl + Y: Paste the last thing deleted withCtrl + U/K.
These are not specific to history/completion, but they make editing past commands much faster once you recall them from history.
Putting It Together: Practical Examples
Example 1: Fixing a Mistyped Command
- You run:
apt isntall htop
(You typed isntall instead of install.)
- Press
↑to recall the previous command. - Use arrow keys (or
Alt + B/Alt + F) to move toisntall. - Fix the spelling.
- Press
Enter.
No need to retype the whole command.
Example 2: Reusing a Long Command
You once ran:
tar -czvf backup-home-2025-01-01.tar.gz /home/userLater, you want to run something similar.
- Press
Ctrl + R. - Type
tar -czvf. - When the command appears, press:
Right arrowto edit the filename/date.- Then
Enterto run.
Example 3: Navigating Deep Paths with Autocomplete
Instead of typing a long path manually:
cd /var/log/apache2You can type:
cd /v<Tab>/lo<Tab>/apa<Tab>
Each Tab completion saves keystrokes and reduces the chance of typos.
Summary
- Use arrow keys or
Ctrl + Rto access and search past commands. - Use
!!,!number, and!stringfor quick history expansion (carefully). - Use
historyto see previous commands andhistory -c/history -dto clear or edit. - Press
Tabto autocomplete commands, paths, and often options. - Press
Tabtwice to show all possible completions. - Combine history, autocomplete, and editing shortcuts (
Ctrl + A/E,Ctrl + U/K, etc.) to work much faster in the shell.