
Control-R, my favorite Unix shell command - reuven
http://blog.lerner.co.il/control-r-favorite-unix-shell-command/
======
loevborg
^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.

~~~
neric
I use and love the same mechanism with a different binding though:

    
    
      bindkey '^P' up-line-or-search
      bindkey '^N' down-line-or-search
    
    

Using Ctrl - P or Ctrl - N on an empty command line will just cycle through
all your past commands. Now if the line begins by vim, then pressing Ctrl - P
will cycle just through all your previous vim commands Greatness!

~~~
loevborg
I like your key bindings even better! However, I still prefer the "beginning-
search" instead of just the "search" function:

    
    
        bindkey '^P' history-beginning-search-backward    
        bindkey '^N' history-beginning-search-forward
    

It works and I don't see a downside to this: ^P and ^N still work as expected.

(Why not the cursor keys? Because then you need to take your fingers off the
home row.)

------
Spittie
^R is probably my second favorite command, I prefer FiSH style (and use
[https://github.com/zsh-users/zsh-history-substring-
search](https://github.com/zsh-users/zsh-history-substring-search) with ZSH),
but since this isn't on every server I have access, it's a nice trick to
remember.

My favorite one (I digress) is that you can use ^[something]^[something else]
to replace [something] with [something else] in the last command. Great for
typos, or for repeated commands with few different arguments.

    
    
        ~ ping google.com
        ~ ^google^news.ycombinator
        ~ ping news.ycombinator.com

------
aroch
Cache:
[https://webcache.googleusercontent.com/search?q=cache:aJ9SEY...](https://webcache.googleusercontent.com/search?q=cache:aJ9SEYtJjfIJ:blog.lerner.co.il/control-
r-favorite-unix-shell-command/)

I prefer how ZSH or FiSH does this, fuzzy matching without the need to
remember the command/binary but can match the servername or a variable and no
need for C-r

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

~~~
aroch
Control-R with fuzzy searching never seemed to work as well as I wanted to.

Also, you should setup some aliases :)

~~~
nanofortnight
Or ssh host shortnames! [http://davidwinter.me/articles/2010/08/22/setting-up-
ssh-hos...](http://davidwinter.me/articles/2010/08/22/setting-up-ssh-host-
shortnames/)

------
ahnick
I prefer using vi bindings (from bash type "set -o vi" to enable) to the ^R
method. If you know vi/vim then the results you get are very intuitive.

Hit Esc to enter vi mode and then hit "/". Type your search and hit enter. If
the first result that comes up isn't what you are looking for then hit "n" for
the next result. To go backwards do Shift + "n".

~~~
jeorgun
So _that 's_ what it is! I was just getting increasingly frustrated, wondering
why "?" wasn't working.

I guess staying intuitive is a difficult game to play, when your premise (the
default / most useful searching order) is inherently different from the norm.

------
hartator
In zsh and some bash, you can access command history in a way simpler way:

1) Start typing a command `ssh`

2) Use `up` arrow to go backward in your history

2) Use `down` arrow to go forward in your history

~~~
neric
Using arrows is an heresy and should never be taught to anybody.

    
    
      1) Replace CapsLock with Ctrl
      2) Use Ctrl-P, Ctrl-N to navigate the commands
    

Remember: never leave the home row.

------
Mister_Snuggles
Control-R will work with anything that uses readline and keeps history. In
addition to working in the Unix shell, it also works in psql (the PostgreSQL
tool) and the Python REPL.

It's a very useful trick to keep up your sleeve!

~~~
encoderer
Python REPL?

Is there something I need to do to enable that?

~~~
Bootvis
Type

    
    
        python
    

in your shell?

~~~
maxerickson
Note that a given python may be compiled without readline or with a readline
replacement.

~~~
encoderer
Yeah, the one on OSX isn't compiled w/ readline. But I tried it on a ubuntu
host and it works.

------
JetSpiegel
Better yet, put this on ~/.inputrc

    
    
       "\e[A": history-search-backward
       "\e[B": history-search-forward
    

to search backwards and forwards with the up and down arrow keys.

~~~
gemlog
Another way to take advantage of ctrl+r is to add a comment at the end of the
line when you have very similar commands. e.g. ssh fred@127.0.0.1 #servername
ffmpeg -some -long -incantation #vid2mp3 It's better than an alias or function
in your rc file as you can easily edit to change some args

------
kps
For those who edit with vi, you can use vi rather than emacs commands for
editing and history:

• for programs using readline, put in $HOME/.inputrc

    
    
      set editing-mode vi
    

• for programs using libedit / editline, put in $HOME/.editrc

    
    
      bind -v

~~~
beaumartinez
I'd do this, but the lack of indication of the editing mode you're in destroys
how useful this'd be to me (I'd have to paranoidly hit Esc all the time).

~~~
drifkin
I have zsh set up to indicate when I'm in command mode. Mostly based on this
post: [http://paulgoscicki.com/archives/2012/09/vi-mode-
indicator-i...](http://paulgoscicki.com/archives/2012/09/vi-mode-indicator-in-
zsh-prompt/)

------
minimax
If you do

    
    
        stty stop ""
    

you can use Ctrl-S to search forward too.

~~~
belovedeagle
Better yet, switch zsh to use vim keybindings and not have to ever worry about
those keybinding clashes.

------
a-b
ZSH ZLE! [http://zsh.sourceforge.net/Doc/Release/Zsh-Line-
Editor.html](http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html)

------
blahedo
I use tcsh, and I've bound up-arrow to do this.

    
    
        bindkey -k up history-search-backward
        bindkey -k down history-search-forward
    

Very useful.

------
bla2
^X^E goes well with ^R: it opens the current line in $EDITOR, so you can find
a recent long command and then easily tweak it.

------
aaron695
If you can't get Control S to work see

[http://stackoverflow.com/questions/549810/control-r-
reverse-...](http://stackoverflow.com/questions/549810/control-r-reverse-i-
search-in-cygwin-bash-how-do-you-reset-the-search)

------
kephra
Sometimes one does not want the shell to log a command.

In this case use:

    
    
      export HISTCONTROL=ignorespace
    

or better

    
    
      export HISTCONTROL=ignoreboth
    

Now every command that starts with a space wont be logged into history.

~~~
dredmorbius
Also HISTIGNORE if you want to ignore specific commands, or export
HISTFILE=/dev/null to disable history storage. HISTSIZE=0 also works, though
it disables all history commands for the present shell, whereas HISTFILE
merely fails to save them to file on exit.

------
stevedomin
fish got it so right, much simpler and powerful than ^R

------
lucb1e
Wordpress strikes again. It's curious how just about every previously-unknown
Wordpress blog goes down at the first hint of traffic.

~~~
reuven
OP here: Yeah, I'm rather surprised and frustrated that my server went down
due to this traffic. It has handled much more than this in the past without an
issue. I'm looking into the problem, and hope to have a solution very soon.

Update: Server seems to be doing OK...

------
psibi
Would be interested to know, if there is a way of highlighting text in bash
while using ^R.

~~~
voltagex_
Highlighting? How so? Are you in a graphical term emulator or using something
like gpm?

~~~
psibi
The OP has mentioned this in his article "Depending on your shell and
configuration, the matching text might even be highlighted."

I use gnome-terminal and I don't see any highlighting there. But it seems to
work in zsh for OP. (Probably oh-my-zsh is providing it.)

~~~
reuven
OP here. My zsh configuration, done through oh-my-zsh, contains the following
line:

zle_highlight=(region:standout special:standout suffix:bold
isearch:bold,underline,fg=yellow,bg=red)

As you can see, one of the items here is "isearch", and it's both bold and
underlined, with a yellow foreground and red background. I set that up as part
of a custom theme that I wrote for oh-my-zsh.

I'm not sure if zle_highlight is part of the standard zsh package. I'm sure
that there is a way to get bash to do something similar, but I don't know what
it would be. (I switched from bash to zsh about 4-5 years ago, and have only
good things to say.)

~~~
psibi
Thanks, but it seems you cannot do it in bash since it uses readline. More
details here:
[http://unix.stackexchange.com/a/131395/29539](http://unix.stackexchange.com/a/131395/29539)

------
thrownaway2424
One wonders how long it takes people to figure these things out. Incremental
reverse search with ctrl-r is not new and does not require a "modern" shell
unless you've been using Unix forever, in which case you surely knew about it
already.

Anyway just wait until this person figures out ctrl-a and ctrl-e and alt-.
Then minds will truly be blown.

~~~
reuven
OP here: I've been using Unix as my primary desktop, and Emacs as my primary
editor, since 1988. I've known about these keybindings for a heckuva long
time. But I keep encountering people who don't, and who are delighted to learn
something new. I thought that it would be nice to share, that's all.

~~~
derwiki
It was nice to share, thank you! I remember how excited I was the first time I
learned about this command.

