3

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)
4
  • 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, 2019 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, 2019 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, 2019 at 9:15
  • Yes. That’s right. To have it bound to up arrow.
    – Steven Lu
    Jun 18, 2019 at 17:06

1 Answer 1

1

Bash 4.4 has history-substring-search-backward bindable command that does this. From the NEWS file:

2.  New Features in Readline
<...>
b.  There are new bindable commands to search the history for the string of
    characters between the beginning of the line and the point
    (history-substring-search-forward, history-substring-search-backward)
1
  • That’s nice despite it needing to be some time until this new of a bash becomes available on most systems.
    – Steven Lu
    Jul 17, 2019 at 12:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .