Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

When dealing with bash history, I find that too many people are still unaware of C-r (Ctrl+R or Command+R): press it and start typing unique bits of your past commands. If multiple commands match, cycle through them with repeated C-r presses.

The usual reaction is :mindblown: :)



sort by: page size:

TIL I can use ctrl+R to search bash history! This is going to save me a lot of time going forward!

Instead of aliasing common commands, I make heavy use of Ctrl-r at the command prompt to search the history.

So if I have a complex command I want to run several times, I'll type it out in full the first time. After that, I'll hit Ctrl-r and start typing part of the command until it shows the one I mean, then hit enter to run it.


For those who haven't used it, ctrl-r in the command line is the best thing in the world. It does a reverse-search over previous commands. Set your max history size to unlimited, and your CLI will remember /all/ commands you ever type, making it very easy to find long, strange incantations that you figured out once, so long as you can remember some small part of the command.

For example, How did I do that magical ffmpeg thing last time? Just hit ctrl-r, type ffmpeg, and keep hitting ctrl-r to find previous examples until I find what I'm after.


Ctrl-r is what I use too. Usually requires fewer keystrokes than other methods discussed here. And it works for all commands in shell history.

Not quite a command, but ctrl+r in bash does a real useful history search

And the next lesson: C-r will search the command history (at least in most *nix shells). So when needing to do something but you aren't sure how far back it was (not on the screen or not recallable) but you know it is in the history and some text in it, just C-r <something> [C-r repeatedly until you get to the correct version].

"In bash, use Ctrl-R to search through command history."

Or, add these lines to your .bashrc to use Ctrl-p and Ctrl-n:

bind "\C-p":history-search-backward

bind "\C-n":history-search-forward


Discovering Ctrl+r (for searching through past commands) was a big jump for me in CLI usability.

Thanks, I discovered C-o thanks to you (even though I have gone through the bash manual several times).

No, more having to press C-r multiple times just to get sequential group of commands from history.


An important addition to that is to repeatedly hit ctrl-r to cycle through the matching commands in the history.

I was today years old when I learned about CTRL+R. I've always just done history | grep

Also, I use zsh which filters history when using up/down arrows to whatever you typed. So you can do "git" [up] [up] [up] to cycle through all recent git commands which is super useful.


I use control-r a LOT. It lets you look at and edit what you'll re-type. I use it for complicated commands I ran yesterday and the day before.

Unfortunately my hands are wired for control-r (caps lock = ctrl) and not meta-r.

Hard to figure out the ROI of re-learning.

(still emacs recall history doesn't seem to reflect bash history, which I routinely set to 10k commands)


Interactive history search (^R) in Bash (and IPython) so awesome, it can't be overstated. I've become so addicted to it that I start almost any command with ^R now. For most of them, I've got them somewhere in history - and only if it doesn't find anything, I ^C and type manually.

For those unaware: CTRL+R in terminal will also change your prompt to search your command history. After typing, CTRL+R again to cycle through matches.

To be fair, I also have that alias and use CTRL+R. They are useful for different scenarios. CTRL+R when I know what command I'm looking for, but can't remember the full one. `history | grep -i $TERM` when I remember some part of the command, but not exactly sure. With that, I get a list of possible candidates to choose from, instead of having to do CTRL+R repeatedly to find the command I was looking for.

Great Atuin, that's amazing. Thank you, this is indeed a game changer for having a permanent history. I use ctrl+r for everything including commands I vaguely remember but know at least some letters

Agreed, but even nicer than control-r are the readline functions `history-search-backward` and `history-search-forward`.

Personally I bind them to up/down which on OS X at least involves:

  # Put this in some file like ~/.readline-bindings
  "\e[A": history-search-backward
  "\e[B": history-search-forward

  # And this in your ~/.bashrc or ~/.zshrc
  bind -f ~/.readline-bindings
That way if you haven't typed any input it behaves like normal up (previous command), but if you've typed some characters it only retrieves matching commands.

http://codeinthehole.com/writing/the-most-important-command-...


My favorite bash trick is still Ctrl-O: submit command and advance history by one.

If you ever find yourself counting up-arrows to repeat a sequence of commands (that aren't worth scripting for whatever reason), next time, go to the top of the list and hit Ctrl-O. The result is the the command runs, and the one after it in the history is loaded into you prompt.


C-r will search what you type afterwards, (e.g., "C-r ssh" will then show me the previous command that starts with "ssh") whereas this tip seems to be searching to match what you've already typed. Pretty cool, IMO.
next

Legal | privacy