1

How can I tell a BASH reverse search (ctrl+r) that the string I give it should be the start of the command line?

E.g. in my system I have 2 common programs: "activate module" and "deactivate module", but when searching for "activate" I don't want to get any results about "deactivate". When I start the search string with an "^" the reverse search seems to stop working, I can no longer input anything and have to abort it via Ctrl+C.

(The solution of How do I change bash history completion to complete what's already on the line? is not working, since a search via that for "acti" gives as first result "deactivate", and so a result I do not want to get.

The solution of Bash: select a previous command that matches a pattern is not addressing my question of only including hits that include the beginning of the line...

Community
  • 1
  • 1
Rava
  • 53
  • 7
  • This Q is not about programming as defined for StackOverflow. It **may** be more appropriate on the S.E. related sites http://unix.stackexchange.com (Unix & Linux) OR http://superuser.com . Consider using the `flag` link at the bottom of your Q and ask the moderator to move it. Please don't post the same Q on 2 different sites. Thanks and Good Luck. – shellter Sep 25 '16 at 15:14
  • @Sundeep: Not really, when I type "acti" and "up key", the first result I get is also a "deactivate" line, so it does just the same as reverse search. shellter, I flagged the Q to be moved to unix.stackexchange.com... – Rava Sep 25 '16 at 15:27
  • you probably need to apply the changes made.. simple way to do that is open a new terminal – Sundeep Sep 25 '16 at 15:47
  • This is not a duplicate of the selected question. The answer @Rava is looking for is to use `history-search-backward`, which searches for commands starting with what you've typed so far. Run this: `printf "%s\n" '"\ee": history-search-backward' '"\er": history-search-forward' >>~/.inputrc` to bind the reverse and forward search of this type to `ESC-E` and `ESC-R` respectively. The changes will take effect when you relaunch your shell. To make them effective in your current shell, run `bind -f ~/.inputrc` – Eric Sep 25 '16 at 20:08
  • @Eric I tried your solution but cannot get it to work... When binding these to ESC-E or ESC-R... how do I make it work? When typing ESC-E, it does the same as typing the "up" key to browse through the previous commands, how is that helping with my question of reverse-searching including the begin-of-line character? – Rava Sep 26 '16 at 04:05
  • First type the start of the command. e.g. to find a previous `curl` command, enter `curl ` at the prompt. Then press and release `ESC`. Then press and release `e`. You can repeat the `ESC` then `e` sequence to step through commands that start with `curl `. If you go back too far, you can use `ESC` then `r` to move through the same list in the other direction. – Eric Sep 26 '16 at 10:56
  • @Eric that works for the first find, if I press esc, e again it acts like pressing the "up" key, not giving me the next result, but showing me the previous command... Also, after I executed the bind -f ~/.inputrc after most commands the result is either not shown (I have to enter [return] to see it), or the prompt is not shown, I also have to enter [return] to get the prompt. I previously not used a ~/.inputrc. My bash version: 4.2.53(2)-release (x86_64-slackware-linux-gnu) – Rava Sep 26 '16 at 17:44

0 Answers0