by loevborg · 12y ago · 0 comments
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. 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.)
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.
or more generally, just set up aliases for all the commands you use regularly.