Site icon New Generation Enterprise Linux

Tame Long Commands with `Ctrl+R` and Shell History Expansion

Quick Tip

Tame Long Commands with `Ctrl+R` and Shell History Expansion

Challenge: You’ve run a complex command earlier, and now you need to recall and re-run it, or a slightly modified version, without painstakingly retyping it.

The Solution: Use `Ctrl+R` to interactively search your command history, or use shell history expansion to recall and reuse previous commands.

# Interactive Search Ctrl+R # History Expansion Examples !echo # Re-runs the last command starting with 'echo' !-2 # Re-runs the second to last command !! # Re-runs the very last command !string:p # Prints the command that starts with 'string' without executing it !string:gs/old/new # Re-runs the last command starting with 'string', replacing 'old' with 'new'

Why it works: The shell maintains a history of executed commands. `Ctrl+R` provides a powerful interactive search mechanism. History expansion, using `!`, allows you to directly reference and manipulate previous commands programmatically within your current command line.

Pro-Tip: To see your entire command history, simply type `history` and press Enter.

Linux Tips & Tricks | © ngelinux.com | 4/29/2026

0 0 votes
Article Rating
Exit mobile version