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 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.
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.
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
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.
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.
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.
^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:
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 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).
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.
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'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'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..
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.
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.
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
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.)
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.
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.
62 comments
[ 2.9 ms ] story [ 134 ms ] threadI 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'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.
Also, you should setup some aliases :)
Control-R is convenient enough that there's little incentive to do things right. This says more about me than about Control-R though. ;)
It's a very useful trick to keep up your sleeve!
Is there something I need to do to enable that?
Update: Server seems to be doing OK...
Anyway just wait until this person figures out ctrl-a and ctrl-e and alt-. Then minds will truly be blown.
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.
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.
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:
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.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.
(Why not the cursor keys? Because then you need to take your fingers off the home row.)
or more generally, just set up aliases for all the commands you use regularly.
• for programs using readline, put in $HOME/.inputrc
• for programs using libedit / editline, put in $HOME/.editrcI 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.
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.
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
"\e[A": history-search-backward "\e[B": history-search-forward
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
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.)
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.)
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".
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.
In this case use:
or better Now every command that starts with a space wont be logged into history.http://stackoverflow.com/questions/549810/control-r-reverse-...