7 ms·
Control-R, my favorite Unix shell command
- aroch 12y agoCache: https://webcache.googleusercontent.com/search?q=cache:aJ9SEYtJjfIJ:blog.lerner.co.il/control-r-favorite-unix-shell-command/ 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
- Mister_Snuggles 12y agoI 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 12y agoControl-R with fuzzy searching never seemed to work as well as I wanted to. Also, you should setup some aliases :)
- nanofortnight 12y agoOr ssh host shortnames! http://davidwinter.me/articles/2010/08/22/setting-up-ssh-host-shortnames/ http://davidwinter.me/articles/2010/08/22/setting-up-ssh-hos...
- Mister_Snuggles 12y agoI 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. ;)
- Mister_Snuggles 12y agoControl-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 12y agoPython REPL? Is there something I need to do to enable that?
- Bootvis 12y agoType python in your shell?
- maxerickson 12y agoNote that a given python may be compiled without readline or with a readline replacement.
- encoderer 12y agoYeah, the one on OSX isn't compiled w/ readline. But I tried it on a ubuntu host and it works.
- Mister_Snuggles 12y agoThe 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.
- ninkendo 12y agoTip 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.)
- 12y ago
- lucb1e 12y agoWordpress strikes again. It's curious how just about every previously-unknown Wordpress blog goes down at the first hint of traffic.
- reuven 12y agoOP 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...
- JetSpiegel 12y agoBetter 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 12y agoAnother 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
- thrownaway2424 12y agoOne 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 12y agoOP 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 12y agoIt was nice to share, thank you! I remember how excited I was the first time I learned about this command.
- xaritas 12y agoWhat 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 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.
- dredmorbius 12y agoThe 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.
- loevborg 12y ago^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.
- deleted 12y ago[deleted]
- nitrogen 12y agoI 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).
- gbog 12y agoWhy not arrow up? That's what I use and it is very natural.
- hyp0 12y agoMe 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.
- kps 12y agoFor 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
- habitue 12y agoBut 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.
- cturner 12y agoFor the determined - the old O'Reilly korn shell book (turtle) has a solid primer for vi mode.
- beaumartinez 12y agoI'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 12y agoI 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-in-zsh-prompt/ http://paulgoscicki.com/archives/2012/09/vi-mode-indicator-i...
- larrybolt 12y agoI'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.
- 12y ago
- Spittie 12y ago^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
- stevedomin 12y agofish got it so right, much simpler and powerful than ^R
- hartator 12y agoIn 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
- pavel_lishin 12y agoTo enable that in bash, put this into your .inputrc file "\e[A": history-search-backward "\e[B": history-search-forward
- ramblerman 12y agoPeople 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
- neric 12y agoUsing 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.
- bla2 12y ago^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.
- psibi 12y agoWould be interested to know, if there is a way of highlighting text in bash while using ^R.
- voltagex_ 12y agoHighlighting? How so? Are you in a graphical term emulator or using something like gpm?
- psibi 12y agoThe 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 12y agoOP 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 12y agoThanks, 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
- ahnick 12y agoI 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".
- piokuc 12y agoI didn't know this works in bash. I've been using this in Korn shell for many years and cannot imagine life without it...
- jeorgun 12y agoSo 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.
- minimax 12y agoIf you do stty stop "" you can use Ctrl-S to search forward too.
- belovedeagle 12y agoBetter yet, switch zsh to use vim keybindings and not have to ever worry about those keybinding clashes.
- a-b 12y agoZSH ZLE! http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html
- blahedo 12y agoI 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.
- kephra 12y agoSometimes 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 12y agoAlso 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.
- aaron695 12y agoIf you can't get Control S to work see http://stackoverflow.com/questions/549810/control-r-reverse-i-search-in-cygwin-bash-how-do-you-reset-the-search http://stackoverflow.com/questions/549810/control-r-reverse-...