There are 2 existing ways to get something like this easily:

  • zsh's history-substring-search plugin
  • bash's ctrl+r mode

The problem with zsh is that it's not on many systems. The problem with bash's implementations is that it requires me to hit ctrl+r first. Also, I know about history-search-backward in bash, but it is anchored to the beginning of the command and not nearly as powerful.

I'm hoping there is a way to bind the up key in bash to make it:

  • copy the currently typed command buffer
  • enter ctrl+r mode
  • paste the currently type command buffer in that mode

and on subsequent presses of the up key

  • hit ctrl+r (search next previous)
  • No, bash's history-search-backward isn't anchored to the beginning of the command. You can search for a substring anywhere in your command lines. The cursor will be placed at the beginning of the match, see for example this search on substring '''mus''' which has found a command called copy_music, and put the cursor at 'm' : imgur.com/a/3lsMXam – ychaouche Jun 17 at 14:02
  • 1
    @ychaouche history-search-backward is a bind you can make. It only works on the initial substring part of a command. What you show is the Ctrl+R that i describe. – Steven Lu Jun 17 at 20:30
  • Thanks for the clarification @Steven Lu. So if I get this correctly, instead of typing C-r first then your substring, you would like to first type your substring then type have C-r search what you already typed ? – ychaouche Jun 18 at 9:15
  • Yes. That’s right. To have it bound to up arrow. – Steven Lu Jun 18 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.