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

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.


sort by: page size:

Ctrl+R lets you search old commands in a similar way. I would say it's more intuitive, too.

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: :)


Not a hard choice. So far I stick to the vanilla C-r, same as you.

But I also remember that I can C-r /fzf/ and it will search back to the command:

   source /usr/share/doc/fzf/examples/key-bindings.bash
which will instantly upgrade my search capabilities, if I'm stuck. Which doesn't really happen that much.

I think the main part of my vanilla C-r experience is that I trained myself to remember commands differently. I somehow remember exact char-to-char tokens, like the "/fzf/" substring above. Or for a more extreme example "ose -p d" when I try to find:

    docker compose -p devenv exec myservice /bin/sh
Weirdly, I kinda know that if used "se -p d" instead (shorter) it would land me on a wrong command (so, not shorter).

Last year a friend taught me about the bash trick "CTRL-R" <start typing 'ssh' or some other previously run command> on the command line for reverse history searching, and it is an amazing time saver. It acts as a great alternative to #8, "Find the last command that begins with “whatever,” but avoid running it"

$ !whatever:p


C-r is reverse-search-history. You press C-r and start typing your search pattern to trigger an incremental search. Most recent command matching the pattern anywhere in the command is selected.

In contrast, history-search-backword requires you to start typing your pattern followed by the keystroke it was bound to. The most recent command that starts with your search pattern is selected.


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].

control^r in bash - literally 10x faster to type things which (ideally) makes 10x more productive :)

control^r is reverse search in the command history (for UI people out there).


^R is useful. But for me, there's something even more useful than searching for a string in your history using ^R: completing the line you're currently typing from your history using Alt-p. This comes in handy when you realize, after typing a few words, that you don't remember the arguments you need.

Say you start typing "ssh ex". If you hit Alt-p, you get "ssh example.com"; hit the key another time, and you get "ssh example2.com" (or whatever you've `ssh`ed into in the past). A matching line has to begin with what you've typed so far, so it will match "ssh ex.com" but not "echo ssh ex.com".

To get this behavior in zsh, you can use the following:

    bindkey '^[p' history-beginning-search-backward
    bindkey '^[n' history-beginning-search-forward
By the way, if using the alt key is cumbersome (as it is on OSX), just press once Esc (and release) and then press p, which in the terminal is always equivalent to simultaneously pressing Alt-p. It's also easier for my fingers to reach, given the different positions of the Alt key, so I tend to use Esc instead of Alt.

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-...


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.

I think the Control-R that readline provides just does string matching on the history. I can do Control-R ssh to find the last ssh command, or I can do Control-R blah to find the last time I used that word anywhere in a command line.

I've got a long ssh command that I use to connect to a server and set up some tunnels and I always enter it by doing Control-R and looking for part of the server's name. If it ever falls of the end of my history I'm in trouble.


In the opposite vein, it can be helpful to stick something like ` #useful` or ` # description ` on the end of a command you expect to need again, so you can easily grab from history with C-r

I always add

  "\e[A": history-search-backward
  "\e[B": history-search-forward
to my .inputrc, I find it easier to use than C-r as it's some sort of autocomplete from history. But looking up commands with !number can still be very useful when you don't remember the command you need but remember when you used it (e.g. what commands you ran before and after).

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

"Ctrl + r" for searching previously used commands in shell.

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.

That's useful, but C-r has its merits too, as you can search for any string in the history. So, even if you run, say, python programs all day, here's a way to find that one time you used a different command line parameter... etc.

Note that I think that's a Bash thing, and the default is Emacs-style bindings (C-a, C-e, C-p, C-n) which include C-r for reverse interactive search of the history. I use that every day, a dozen or more times.

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

Legal | privacy