Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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




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

Search: