Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

5 Bash shortcuts for editing and reusing terminal commands

Move through command lines, search history, reuse arguments, edit a command in an editor, and create aliases—with the execution behavior of each method explained.

Table of Contents

Bash’s interactive editing shortcuts can reduce retyping and make long commands easier to review. The examples below assume Bash with the usual Emacs-style Readline bindings. Other shells, vi editing mode, and terminal key mappings can behave differently.

The terminal window and the shell are separate components; see what Bash does if you are unsure which shell you use. Practice with harmless commands before applying shortcuts to commands that change files or system settings.

1. Move by words or jump to the ends of the line

Use Alt + B to move backward one word and Alt + F to move forward one word. Ctrl + A moves to the beginning of the command line; Ctrl + E moves to the end.

Many terminals also map Ctrl + Left/Right Arrow to word movement. On macOS, Option-key behavior depends on the terminal’s configuration; it is not a universal Bash key binding. If Alt does not reach the shell, try pressing Esc, releasing it, and then pressing B or F.

Editing a Bash command line, example 1

Editing a Bash command line, example 2

Editing a Bash command line, example 3

Editing a Bash command line, example 4

Editing a Bash command line, example 5

These movements help you reach an argument or path without repeatedly pressing an arrow key. They do not run the command.

2. Search backward through command history

Press Ctrl + R and type a distinctive part of an earlier command. Press Ctrl + R again to move to an older matching entry.

Reverse command-history search in Bash

Read the entire match before using it. In the usual bindings, Enter executes the selected command; Ctrl + J accepts the match into the editable command line; Ctrl + G cancels the search. The Up arrow remains useful for recalling a recent command.

History reflects what the shell recorded, not every command ever run on the computer. See how the Bash history command works for listing and reviewing entries.

3. Reuse the previous command’s last argument

Press Alt + . to insert the last argument from the preceding history entry into the current line. You can inspect or edit it before executing. Repeated presses can retrieve arguments from earlier entries.

Reusing a previous Bash command argument

For example, after creating a new directory with:

mkdir projects

Type cd and press Alt + . to insert projects. Bash also supports history expansion:

cd !$

When history expansion is enabled, !$ expands to the previous command’s last argument. Do not use it blindly with destructive commands: the preceding history entry may not be the one you remember.

4. Edit the current command in an external editor

Press Ctrl + X, release it, then press Ctrl + E. Bash opens the command in an editor selected through VISUAL, then EDITOR, with a fallback if neither is set.

Editing a Bash command through an external editor

The edited text is executed as shell commands when the editor finishes. This is not simply a preview that always returns to the prompt. Review the complete text before closing the editor. For a command you are still studying, use an ordinary text editor outside this shortcut instead. The behavior is documented in the Bash manual.

5. Give a frequent command a short alias

An alias replaces a command name with text you define. A simple listing example is:

alias ll='ls -al'

Defining a Bash alias

Enter ll on a subsequent command line to use it. To remove this alias from the current shell, run unalias ll. The definition lasts only for the session unless you add it to the Bash startup file used by your environment, commonly ~/.bashrc for interactive Linux shells.

Keep aliases easy to understand. Avoid hiding automatic upgrades, force flags, or destructive actions behind an innocent-looking name. Use a shell function when you need custom argument handling rather than simple text substitution.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.