You already knew that the up and down arrows in a Linux terminal window allow you to navigate through previously typed commands, but did you know that you can also search through your command history?
CTRL-r is great for finding those commands that are a bit tedious to type.
When you type CTRL-R in a terminal window your whole prompt turns into this strange looking text:
As you type characters the first command in your history, starting from the most recent and working back towards the top will be displayed.
Let’s say you’ve typed the following list of commands:
sqlplus / as sysdba
rman target "'/ as sysbackup'"
At a command prompt you type CTRL-r and then type s. Your prompt will now look like this:
(reverse-i-search)`s': ls -als
And the s in the als will be highlighted. What you’ve done here is found the first s in your command history.
If you type Enter right now, you’ll rerun that command.
Things get more interesting when you start typing more letters. Instead of typing just s, type sq and you’ll get the following:
(reverse-i-search)`sq': sqlplus / as sysdba
Hitting Enter will rerun that sqlplus command.
What if you want to find something that you typed earlier than the last command that matched? Just type CTRL-r again. Each time you press CTRL-r you’ll find older and older strings that match. If you have a short search string (just s for example), then it’s not unusual to find that string in a single command multiple times. Once you’ve exhausted all the matches in one command, you’ll reach back into your history to find another that matches.
What if you want to find the last thing that you found with CTRL-r?
Start a new search (or just use backspace to remove any characters you’ve started typing) and press CTRL-r again. Let’s say that you used rm as your last successful search and retrieved the rman command from the above list of commands. You then ran a few more commands and decided to run rman again… Just type CTRL-r twice. The first CRTL-r says “I want to search through my history”, the second CTRL-r says “Remember the last successful search I used? I want that one again.”
What if you don’t want to run the command you found, but instead you just put the command at your prompt so you can edit it? Instead of hitting Enter after you find a command, just press Esc. This dumps the text of the command into your terminal at a command prompt.
Here’s where things get really cool… If you use rlwrap for various Oracle commands like sqlplus, rman, etc. (See this post to install rlwrap, and this post to configure the aliases to use with rlwrap) then CTRL-r will also work in those commands too!
Want to find the last alter user command that you ran? In sqlplus just type your good friend CTLR-r and start typing alter. You probably won’t have to actually type all the letters in alter and soon you’ll have the following:
(reverse-i-search)`al': alter user dbsnmp identified by oracle_4U account unlock;
Technically CTRL-r finds the spot in your history where the command you found lives, so once you’ve found a command, you can use the up and down arrows to cycle through the commands that are near that command. What was that command that I typed right after I did that alter user command? Just press the down arrow key and you’ve found it!
There are a few idiosyncrasies with CTRL-r. First it is case sensitive so you’ll have to remember if you used SELECT or select. Second it can be kind of easy to get it into a strange place with the characters you are searching for if you start typing characters that are not actually in your command history CTLR-r will beep at you a bunch and you may or may not be able to use the backspace key to get back to where you want to be… Just use Esc to get out of the CTRL-r search and try again.
Also you may want to give CTRL-r more history to work with. If you’d like to do this you’ll need export two different environment variables, HISTSIZE and HISTFILESIZE.
I added the following to my .bash_profile file in my home directory:
# Let's give the history command and CTRL-r more to work with!
CTRL-r has saved me much typing over the years, I hope it helps you too!