Unix to Know this Command Series: Reverse-i-Search with CTRL-R
If you want to look cool, save time, and live a better life, then spend 5 minutes learning the shell command
CTRL-R — sometimes called reverse-i-search. There is rare shell command that I use more often as a software engineer; I would like to pass my love for this wonderful utility on to the rest of the world. Here we go <3
Note: this command can be used in most Linux, GNU, and Unix scripting shells. Some Windows command-line tools like Cygwin and PowerShell support this command, as well. The title is more catchy my way, though.
Definition of Reverse-i-Search
Reverse-i-search is a simple, backward-moving, incremental search starting at the current line and moving up through the command history (source)
Imagine that the command history is a stack — each time that you execute a command, the command is pushed onto the command history stack.
Each time that you use reverse-i-search, you are peeking at the top of the stack, and then if satisfied, popping the latest command off of the stack to exectute — except, you can imagine you are peeking at the top of a stack that has been filtered to only contain matches to the search pattern requested.
How to Use Reverse-i-Search
Hit CTRL-R then start typing previously used command
For example, if I had previously typed a command to run a specific test file that started with the word
mocha, I could start typing
moch... (mocha is the test runner utility) after initiating reverse-i-search with CTRL-R.
As the above screenshot shows, the command-line is populated with the most recent fuzzy match from the command history — the matched pattern is underlined.
Hit Enter/Return to select a command and execute it
For example, assume that we have started a back-search with
We want to execute the command that matched —
g fetch origin master. We hit enter, and watch the command execute.
Cycle through History
What if we want to search through the command history list, one-by-one?
Cycle through command history by hitting CTRL-R, again
Simple. Just repeat the command as many times as needed until you find your command or exhaust the list of history.
Edit Command while Searching
What if we want to edit the historical command before executing it?
Use the left or right arrow to place cursor within selection for editing
Also simple. While searching, hit either the left or right arrow to place your cursor in the currently matched command. You can then edit the command as you normally would, then hit enter to execute the new command.
Good for you for making your life easier by learning these shortcuts and tools — you now have a valuable way to manipulate your shell command history with
Remember, it isn’t just me that should know these scripting tools — unix to know these commands, too™