Kahibaro
Discord Login Register

Command history and autocomplete

Why History and Autocomplete Matter

Working efficiently in the shell is mostly about not re‑typing things:

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

You can:

  1. Press multiple times to find an old command.
  2. Edit it if needed.
  3. Press Enter to run.

Viewing the History List

Run:

bash
history

You’ll see a numbered list:

  120  ls
  121  cd /etc
  122  cat hosts
  123  history

You can re-run a command by its number (see below).


Re-Running Commands from History

Using `!!` (last command)

Examples:

bash
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:

bash
!122         # re-runs history entry number 122

Using `!string`

Bash will search the history for the most recent command that starts with string:

bash
!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.

Procedure:

  1. Press Ctrl + R.
  2. Start typing part of a previous command (e.g. apt).
  3. Bash shows something like:
   (reverse-i-search)`apt': sudo apt update
  1. Keep pressing Ctrl + R to cycle through older matches.
  2. Press:
    • Enter to run the shown command.
    • Right arrow or Ctrl + E to accept but edit before running.
    • Ctrl + G or Ctrl + C to 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:

bash
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

bash
history -c

This 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:

bash
history           # find the number
history -d 211    # delete entry 211

Avoiding saving a specific command

Common tricks:

bash
   secret-command here

Note the leading space.

bash
  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
~/.bash_history

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:

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:

bash
cd Doc<Tab>

If there is only one match (e.g. Documents), Bash completes it:

bash
cd Documents/

If there are multiple matches:

  1. Press Tab once: nothing (or a partial common prefix) may add.
  2. Press Tab again: Bash will show all possible matches.

Example:

bash
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:

bash
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:

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:

bash
ls -<Tab><Tab>

You might see a list such as -a -l -h -R ... depending on your system’s completion scripts.

Examples

bash
  ssh ser<Tab>     # might complete to 'server1' or similar
bash
  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:

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

  1. You run:
bash
   apt isntall htop

(You typed isntall instead of install.)

  1. Press to recall the previous command.
  2. Use arrow keys (or Alt + B / Alt + F) to move to isntall.
  3. Fix the spelling.
  4. Press Enter.

No need to retype the whole command.

Example 2: Reusing a Long Command

You once ran:

bash
tar -czvf backup-home-2025-01-01.tar.gz /home/user

Later, you want to run something similar.

  1. Press Ctrl + R.
  2. Type tar -czvf.
  3. When the command appears, press:
    • Right arrow to edit the filename/date.
    • Then Enter to run.

Example 3: Navigating Deep Paths with Autocomplete

Instead of typing a long path manually:

bash
cd /var/log/apache2

You can type:

bash
cd /v<Tab>/lo<Tab>/apa<Tab>

Each Tab completion saves keystrokes and reduces the chance of typos.


Summary

Views: 24

Comments

Please login to add a comment.

Don't have an account? Register now!