Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel failed reverse-i-search in bash but keep what I typed in

I hit Ctrl+R pretty much every time I run a command, but after typing in a word (e.g. find) I realize that the command is not in my history so I get this as expected:

(failed reverse-i-search)`find`: cat fire.txt

Now how do I cancel the search but keep 'find' in my command line? No matter what key I press 'cat fire.txt' ends up on my command line and I have to erase and re-type my command again.

I am using Ubuntu. Thank you.

like image 595
Satish Avatar asked Oct 27 '15 00:10

Satish


1 Answers

I don't know that you can do what you want exactly but you can get something vaguely close (though it sacrifices other features of reverse-i-search).

Bind a key sequence to history-search-backward and do your searches by typing the starting word and then hitting your bound sequence.

history-search-backward ()

Search backward through the history for the string of characters between the start of the current line and the point. The search string must match at the beginning of a history line. This is a non-incremental search. By default, this command is unbound.

But that only works for commands that start with the word you are searching for (as well as not being incremental).

Update: Just found, while looking for something else, that in the bash NEWS for 4.3 there is an entry that says

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)

So that means with a new enough version of readline (that's in the readline section of the NEWS) you can still have the substring history search with this version.

like image 116
Etan Reisner Avatar answered Nov 09 '22 06:11

Etan Reisner