Kahibaro
Discord Login Register

2.1.6 Command history and autocomplete

Understanding Command History and Autocomplete

Command history and autocomplete help you work faster in the terminal. Instead of retyping long commands, you can reuse previous ones or let the shell complete them for you. In this chapter, you will see how to recall past commands, search through them, and use autocomplete efficiently. The examples assume you are using Bash, which is the default shell on many Linux distributions, but most ideas are similar in other shells.

Basic Command History Navigation

Your shell keeps a list of commands you have typed. This list is called the command history. You can move through it using the keyboard.

Press the Up arrow key to go backwards in time through previous commands. Each press shows an older command. Press the Down arrow key to move forward again toward more recent commands, including the empty prompt.

Once you find a command you want to run again, you can press Enter to execute it, or you can edit it first. For example, you might scroll to a long cp command, change one filename, and then run it.

The history is stored in memory for the current shell session. When you close the terminal, Bash normally writes this history to a file in your home directory called ~/.bash_history. Next time you open a terminal, your old commands are loaded again.

Viewing and Reusing History with `history` and `!`

You can display your command history as a numbered list with the history command.

For example, type:

history

You will see lines like:

  23  ls
  24  cd /etc
  25  cat hosts
  26  sudo apt update

Each command has a history number. You can reuse commands by these numbers with the ! syntax, often called history expansion.

To rerun command number 25, you can type:

!25

This will execute the exact command that was on line 25 of your history list.

You can also use shortcuts based on recent commands:

!! runs the previous command again.

!string runs the most recent command that started with string.

For example, if you recently ran sudo apt update, then later you can type:

!sudo

This will expand to sudo apt update and run it again.

History expansion with ! can be powerful but also dangerous. Always check what will run before pressing Enter, especially when using sudo or commands that modify files.

If you want to see what the shell will expand before executing, you can first type the ! form, then press Ctrl + X, then Ctrl + E in some configurations to edit in an external editor, or in more advanced use, you can enable options that print expansions. As a beginner, use history to check the exact command if you are unsure.

Searching History with Reverse Search

If you do not remember the exact history number but remember part of a command, you can search backward through your history.

Press Ctrl + R to start a reverse search. Your prompt will change to something like:

(reverse-i-search)`':

Now start typing a few characters. For example, if you type apt, you might see:

(reverse-i-search)`apt': sudo apt update

The shell shows the most recent command that matches what you typed. You can:

Press Enter to run that command immediately.

Press the Right arrow key (in many terminals) to accept the command and edit it before running.

Press Ctrl + R again to go further back to older matching commands.

Press Ctrl + C to cancel the search entirely and return to an empty prompt.

This feature is very helpful when you need to reuse a complex command that would be tedious to type again.

Editing the Current Line with History Movement

History navigation works together with command line editing. If you press Up, find a command, and then want to adjust only a part of it, you can use cursor keys to move around and change characters before pressing Enter.

You can also recall only parts of previous commands. Bash supports advanced history expansion to extract arguments, such as !$ for the last argument of the previous command. For example, if you run:

mkdir my_new_folder
cd !$

The !$ expands to my_new_folder, so the second command becomes cd my_new_folder. This can save typing when you frequently reuse recent filenames or directory names.

Using history expansions like !$ and !! changes your command line before it is run. Make sure you understand what is being substituted, especially when you use commands that delete or overwrite files.

Autocomplete with Tab

Autocomplete helps you avoid typing full command names or file paths. Bash provides completion when you press the Tab key.

When you start typing a command at the beginning of a line, then press Tab, Bash will try to complete it to a full command name, if it can find a unique match in your $PATH. For example, type:

mkdi then press Tab.

If mkdir is the only command that starts with mkdi, the shell fills in mkdir for you.

If there are multiple possible matches, pressing Tab once may do nothing visible. Press Tab a second time to show a list of possible completions.

Autocomplete also works for filenames and directories. For example, if you want to run:

cd Documents/Projects/LinuxCourse

You can type:

cd Doc then Tab. If Documents is the only match, it completes to cd Documents/. Then type Pro and press Tab again, and so on.

If a filename contains spaces, the shell will handle quoting or escaping for you when you use Tab completion, which helps avoid errors.

Autocomplete for Options and Arguments

On many systems, Bash completion is configured to also help with command options and sometimes arguments. For example, with certain packages installed, if you type:

git ch then press Tab,

it may expand to git checkout or suggest options related to git.

Similarly, for package managers like apt or dnf, Tab completion may suggest package names. This depends on whether completion scripts are installed and enabled on your system. Different distributions may have different default configurations.

If autocomplete does not seem to work for options or certain commands, it may simply mean that the extra completion scripts are not installed, but basic filename and command name completion should still work in Bash.

Customizing History Behavior

Although details of environment variables are covered elsewhere, command history uses a few variables that affect how it behaves.

Bash uses HISTSIZE to control how many commands are kept in memory for the current session, and HISTFILESIZE to control how many lines are stored in the ~/.bash_history file across sessions. These values are usually set in ~/.bashrc or a similar shell configuration file.

History can also ignore duplicate commands or commands that start with a space, depending on the HISTCONTROL setting. For example, with a certain configuration, running:

 ls
 ls

might store only one ls instead of both.

Since editing shell configuration files is discussed in more detail elsewhere, for now it is enough to know that history behavior can be tuned, and that the default settings are usually a good starting point.

Combining History and Autocomplete Efficiently

When you become familiar with history and autocomplete, you can combine both to work quickly.

You can press Up to recall a recent command, then start editing and use Tab to autocomplete filenames in the new context.

You can also use Ctrl + R to search for an older command, move the cursor to adjust only part of it, then press Tab to complete a new filename or option.

Over time, you will develop a habit of typing only the first few characters of a command or path, pressing Tab to complete, and using history features to avoid retyping long or complex commands. This is an important part of becoming efficient and comfortable in the Linux command line environment.

Views: 8

Comments

Please login to add a comment.

Don't have an account? Register now!