Navigating Bash History with Ctrl+R
.bash_history(there’s a limit on how many it stores but for me it goes back a few days). I find it really useful to look back at the history to use the same commands again or edit them slightly. You can press the up arrow to go through your history but it can take a really long time to find what you’re looking for. So instead, try Ctrl + r.
To do this: first press Ctrl + r, then start typing the command or any part of the command that you are looking for. You’ll see an autocomplete of a past command at your prompt. If you keep typing, you’ll get more specific options appear. You can also press Ctrl + r again as many times as you want to, this goes back in your history to the previous matching command each time.
Once you see a command you like, you can either run it by pressing return, or start editing it by pressing arrows or other movement keys. I find this a really useful trick for going back to a command I know I used recently, but which I can’t remember or don’t want to look up again. I hope this is a useful trick for you too!
Thanks Lorna, i tried it and it works great. I normally use the command history | grep “
” but this is faster i think.
Lineke: I like this approach because it all happens at the prompt and when you get the right command, it’s ready to be run. Glad you liked it!
very useful ,Thanks.~
Thanks, this is very useful! I realise that I’ve wasted a lot of time using “history | grep” :(
I can never get my head around CTRL-R history enough to use it usefully … I guess I was used to the 4NT style history with cursor keys in the old days + find it hard to navigate in CTRL-R
Thanks for sharing this. After reading your post I realized that I can edit and run really long command. This definitely saves few keyboard hits and comes handy.
Regarding running last executed commands there are few more ticks. You can using bang command “!”. This doesn’t provide you luxury to edit and run. it simply run the command.
If you type “!find” it will try to run last “find” command. You can write the very first character of command it will find the last executed one and run it again.
If you want to run just last command executed, run “!!”
Thanks, I was missing that multiple [ctrl]-r could bring you farther back in history. To contribute back some tidbits, a handy command is alias:
[code]alias l=’ls -al'[/code]
So when you type “l” in console, it automatically does an “ls-al”.
nice CTRL-R CTRL-R trick. It keeps searching backwords but once you use movement keys and pick the command how to further do search replace on it ?
I’ve always used history command with grep to filter results. Its kinda useful too, cuz u can look for all matching registers.
This is a good tip, thanks for sharing
I use a custom .inputrc file so that hitting up- and down- arrow keys only goes through the parts of my history that match what I’ve typed so far.
Pretty sure I got the technique from here: https://coderwall.com/p/oqtj8w/the-single-most-useful-thing-in-bash
I use this all the time, you don’t even have to type the first part of the command. E.g whenever I want to restart apache I do ctrl+r then start typing “restart”.
Also if you are cycling with ctrl+r and accidentally pass the command you wanted, just use ctrl+shift+r to reverse the reverse search :)
This is VERY useful, thanks!
Wow, years later I also came to the realisation that it was multiple ctrl-r I needed !
thanks buddy, its really helps in identifying the previous commands also.
Pingback: Mark Llobrera · Professional Amateurs: Memory Management « site design
Pingback: Mark Llobrera · Professional Amateurs: Memory Management | WEB DESIGN
Pingback: A Unix Guy looks at Powershell | Object Partners
Pingback: Chimo ([email protected])'s status on Thursday, 13-Jul-2017 09:50:08 EDT - Micromic
This is great. It’s changed my life! Ok, not my whole life, but I have lots of long, hard-to-remember commands that can’t be tab-completed, and this is exactly what I needed.
I use Ctlr-R every day since i learnt it over the shoulder of a colleague freelance developer.
After the 1st Ctrl-R, you can also
1. Return to execute it
2. left, right arrow and backspace to edit the commands before execution
3. Ctrl-R again to see previous matches
4. Arrows up and down to see previous or next commands
5. Alt-Arrow left and right to navigate by word instead of characters
6. ?? and ?? to move to the beginning and end of the command line == THIS IS THE MISSING ONE I COULD NOT FIND, IF ANYONE KNOWS IT PLEASE REPLY THANK YOU !!
Hope this helped.
Beginning of the line: Ctrl+a
End of the line: Ctrl+e
Still useful after almost ten years.
If I search for a command and don’t find it (failed reverse-i-search), I want to carry on typing my new command and execute it. How can I do that?
Pingback: Memory Management – Scrappy