Hacker News new | past | comments | ask | show | jobs | submit login
Control-R, my favorite Unix shell command (lerner.co.il)
122 points by reuven on May 18, 2014 | hide | past | favorite | 61 comments



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


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!


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


I have history-search-backward and its reverse mapped to page up and page down. A rare few Linux distributions enable them by default, but the rest frequently have a commented example in /etc/inputrc that can be copied to your ~/.inputrc (note that programs using editline instead of readline will need additional configuration).


Why not arrow up? That's what I use and it is very natural.


Me too. And not entering anything gives normal arrow up, so there's no loss.

BTW: ^r is a little more powerful: it is /string/ while this one is /^string/

BTW ^r annoyance: you can't type and then hit ^r. You have to hit ^r first. Seems easy to fix...

BTW tip: if you like readline features, consider rlwrap: it wraps its argument (which is your command) in readline. e.g. rlwrap python. Especially nice to wrap your own interactive cmdline tool.


Thanks for the tip on rlwrap, that looks really useful!


Because you have to move your hand away from the home row. And that's not what some people, like me, want.


with the ssh example, you should set up aliases in .ssh/config for that.

or more generally, just set up aliases for all the commands you use regularly.


^R is probably my second favorite command, I prefer FiSH style (and use 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


Cache: https://webcache.googleusercontent.com/search?q=cache:aJ9SEY...

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


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.


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

Also, you should setup some aliases :)



I definitely should.

Control-R is convenient enough that there's little incentive to do things right. This says more about me than about Control-R though. ;)


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


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.


I didn't know this works in bash. I've been using this in Korn shell for many years and cannot imagine life without it...


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


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.


People get used to this and start hitting up >20 times to get back to a command they ran a while back. Often they overshoot too and have to scroll back.

It's the weaker little brother to ctrl-r, only useful for commands ran 1,2 invokes ago. ctrl-r is more functional I'd say


To enable that in bash, put this into your .inputrc file

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


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!


Tip for rbenv users on OSX... install readline with homebrew and install your ruby versions with:

    SKIP_PRECOMPILED_RUBIES=1 CONFIGURE_OPTS="--with-readline-dir=$(brew --prefix readline)" rbenv install (version)
to get readline working for irb/pry (for ctrl+r search, etc.)


You can also use "rlwrap" with any program to enable readline features.


Python REPL?

Is there something I need to do to enable that?


The Python REPL that I'm using has readline enabled. If you don't have it you may need to rebulid it and enable readline when you run configure.


Type

    python
in your shell?


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


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


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.


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


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


But please, for the sake of the rest of us, don't set vi keys as the default on a server or a shared user account.


I've had this in place for a very long time, since I use vim for most of my editing/coding, but recently I've took a look at emacs. I'm far from being anywhere near proficient in emacs and my main editor is still vim. Also I don't like the fact emacs uses meta so much since on a macbook pro laptop keyboard it isn't so obvious to reach, while I did set my caps-lock key to ESC/Control, and even my tab acts as hyper when combined with another keypress.

I did however remove that binding and use the emacs commands in my terminal, because.. why not stay a fit flexible..

But that's my opinion of course.


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


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


For the determined - the old O'Reilly korn shell book (turtle) has a solid primer for vi mode.


Even if you join an existing session (eg: pairing) you can type "set -o vi", then type " set -o emacs" when you're done to return the shell to "normal" mode.


It's really strange, I use the default (emacs?) bindings in Bash, but I edit with vim (if I don't have X to run Sublime...)


If you do

    stty stop ""
you can use Ctrl-S to search forward too.


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



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.


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



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.


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.


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


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


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


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


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


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


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


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


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.


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.


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


What a strange fit of pique. I make extensive use emacs chords to navigate in the shell, and I show them to people all the time if they seem curious. It never occurred to me to be outraged if somebody didn't already know. I suppose I occasionally lose patience with people as I wait for them to find "Copy" and "Paste" in the Edit menu, although I try to bear it silently rather than berating them.

Nevertheless, I in fact did not know about M-. so thank you for your contribution. For those interested, I found it described here: http://www.aboutlinux.info/2005/08/bash-shell-shortcuts.html

It's not a comprehensive list, it omits one of my favorites, C-/, which gives you undo on the command line. Well, until you hit return, anyway.


The bash manpage runs 103 pages (bash | pr | less). That's quite a few features to learn. In my case, ^R was something I didn't discover for 3-4 years, until I saw a co-worker navigating history far faster than I'd seen before (I was aware of Bourne and csh history commands). And that was after over a decade of Unix use.

zsh has 17 manual pages, or zshall which combines them into one, concise, easily absorbed 483 page manual.

Ignorance is curable. Cluelessness (and spite) aren't. Many Linux tools have copious feature sets of which many users, even advanced ones, know only a small fraction of.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: