Fundamentals 5 min read

Boost Your Shell Productivity with Little‑Known Command History Tricks

Learn how to speed up repetitive shell tasks using hidden shortcuts like the escape‑dot (\. or !$), history expansion with !n, custom aliases, and essential editing key bindings, enabling you to quickly recall, modify, and execute previous commands without leaving the terminal.

21CTO
21CTO
21CTO
Boost Your Shell Productivity with Little‑Known Command History Tricks
21CTO guide: If you love using a UNIX shell, the shortcuts described here can help you save more time when programming or editing repeated commands.

I use the shell every day and can’t live without it for even a second. To repeat the previous command or slightly modify it, the common method is the up‑arrow, and another frequent technique is Ctrl‑R to search the command history.

There are also two lesser‑known tricks for reusing history commands.

escape‑dot (\. or !$)

Usually you only want to repeat the last argument of the previous command. For example, after running git diff path/to/tests, you might run git add path/to/tests. For the second command you can type git add \. (escape‑dot) and it expands to path/to/tests, the last argument of the previous command.

I often need to run another command on the same argument, and escape‑dot is the most convenient method. You can also use !$ instead of escape‑dot, though it’s a bit harder to type, so I use it less.

history with !

Sometimes I know a command was used before, but Ctrl‑R can’t find a suitable string (or returns too many unrelated hits). In that case I use history to list recent commands. If the desired command appears at position 456 in the list, I type !456 to re‑run it.

If I want to edit the command before execution, I type !456:p, which prints the command; I then press the up‑arrow to bring it onto the command line and edit it as needed.

I like to keep a long history in my shell (several thousand entries). To scroll through more than just the visible entries, I created an alias that shows only the last 100 items:

alias his='history | tail -n 100; echo "Only last 100. For full, type: history" '

Now typing his displays the most recent 100 commands.

Common Editing Commands

When I want to edit the command line before execution (especially after pulling a command with the up‑arrow), I frequently use:

Ctrl‑A – move to the beginning of the line

Ctrl‑E – move to the end of the line

Ctrl‑U – delete from the cursor to the beginning of the line

Ctrl‑W – delete the word before the cursor

Summary

In most of my work, I combine the up‑arrow and Ctrl‑R to repeat commands. Few people are familiar with the escape‑dot and history‑based shortcuts for repeating commands. I often rotate among these four methods, so I wrote this article to share the tricks.

Hope it helps everyone!

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

productivityhistoryShellcommand-lineshortcuts
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.