Control-R, my favorite Unix shell command
blog.lerner.co.il
blog.lerner.co.il
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.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.
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! 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.)
or more generally, just set up aliases for all the commands you use regularly.
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.comI 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. ;)
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.
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
1) Replace CapsLock with Ctrl
2) Use Ctrl-P, Ctrl-N to navigate the commands
Remember: never leave the home row.It's a very useful trick to keep up your sleeve!
Is there something I need to do to enable that?
python
in your shell? 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.) "\e[A": history-search-backward
"\e[B": history-search-forward
to search backwards and forwards with the up and down arrow keys.• for programs using readline, put in $HOME/.inputrc
set editing-mode vi
• for programs using libedit / editline, put in $HOME/.editrc bind -vI 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.
stty stop ""
you can use Ctrl-S to search forward too. bindkey -k up history-search-backward
bindkey -k down history-search-forward
Very useful.http://stackoverflow.com/questions/549810/control-r-reverse-...
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.Update: Server seems to be doing OK...
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.)
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.