How to Use CTRL R in Linux

If you’re using the modern bash shell, then you can use Ctrl+R as a keyboard shortcut to search through your command history. You can bring up commands that you used previously and issue them over again. This might work in other shells as well, such as if you’re using ksh in emacs mode. You can actually speed up your command line workload a great deal using this little trick.

You’ll have to be working from a terminal for this to work. Hold down Ctrl, Alt and T to open one or Super+T if your desktop environment supports that. You can also click on the Applications menu, head to System Tools and then click on Terminal or search for it on the Ubuntu Dash.

Method 1: Use Ctrl+R to Search for a Command

At the bash prompt, hold down the Ctrl key and push R. You’ll receive a message that reads (reverse-i-search)`’: followed by the cursor. Type the first letter of a command that you issued before to find it. For instance, if you’ve used the cat command to look at a text file’s contents, then push c. If you’ve used cal to view a month’s calendar, then this might come up as well.

Push Ctrl+R again to cycle through similarly named commands. If you have a long bash history, then you might find a number of commands with similar names. Once you find the command that you want to run, push the enter key and your prompt will look like you’ve just typed it in and ran it. You won’t have to type the full command but instead only a letter or two.

You can type a few letters to find a longer command that you might have issued in the past and it too will come up right away on the command line, and you can then push enter to run it like normal. It makes the process of finding something in your history much easier. Once a command is on the line you can also use the arrow keys and edit it before running it.

If you think better about running a command, then you can always hold down the Ctrl key again and push C to cancel out of the Ctrl+R search just like you could cancel out of anything else at the terminal’s command line. You can use this technique after running any other type of command at all, and despite what some Websites might read you actually don’t have to worry about what kind of output is already inside your terminal when you give this a try.

Method 2: Add a Ctrl R Tag

If you want to return to a commonly used command time and time again, you can actually add a tag to it by commenting out a name for the command at the end of the line it sits on. Type in a command followed by a space and then type #findme after it. Push enter and run the command. The shell will simply ignore the #findme tag since it counts as a programming language comment.

Now, anytime you want to find that command hold down Ctrl+R and start typing #fin so that it pops right up. If you want to tag more than one command in this way, then simply come up with other things to tag them with. You might want to try tagging #command1, #command2 and so on if you’d like. That way you can type #com and then push Ctrl+R a few times to find the command that you’re looking for. This works best if you have a massive command history. Each tag will remain in the history until it gets cleared out.

Method 3: Enabling emacs Mode in ksh

If you’re using ksh as your default shell, then type set -o emacs and push enter. This will enable emacs mode, which will let you use the Ctrl+R trick the same way that bash allows it. It works ever so slightly differently, and users of the bash shell will never have to do this so most Linux coders can simply ignore this step.

Users who prefer ksh can push Ctrl+R now and the shell will print ^R to the screen. Type your search and then push enter. You’ll see the closest matching entry, which you can edit or run by pushing enter again. By the way, emacs enables a few other options such as normal arrow key functionality.

ABOUT THE AUTHOR

Kevin Arrows


Kevin Arrows is a highly experienced and knowledgeable technology specialist with over a decade of industry experience. He holds a Microsoft Certified Technology Specialist (MCTS) certification and has a deep passion for staying up-to-date on the latest tech developments. Kevin has written extensively on a wide range of tech-related topics, showcasing his expertise and knowledge in areas such as software development, cybersecurity, and cloud computing. His contributions to the tech field have been widely recognized and respected by his peers, and he is highly regarded for his ability to explain complex technical concepts in a clear and concise manner.