Satish
Satish

Reputation: 1933

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.

Upvotes: 26

Views: 6432

Answers (3)

questionto42
questionto42

Reputation: 9590

Understanding the question

Not sure whether I understand it right. I do not see a problem, and the answer is already in the first comment, which I put in the middle of this answer at "Cancel without losing console text".

Editor: tmux

What is more, if you want to keep a copy of the word "find" and still keep cat fire.txt, that is also possible when taking tmux as the editor. (not tested, but I guess that Alacritty can do that as well, and perhaps many more)

Copy

Go into Ctrl+B+[ mode to be free to roam around in any field of the console. Press Space as the starting marker of the "recursive search text". Press Enter at the end marker. If you set mouse on in your config, you can also use the mouse to Shift+mark and Copy with Ctrl+Shift+C, or you can just mark with the mouse and by this, copy to the tmux clipboard.

Cancel without losing console text

Then go back to any console text that you had at the start with Ctrl+g as the first comment says it: anything you had written in the console is still there.

Paste

And you now can paste the text of the recursive search on top of that, check middle mouse click, Ctrl+Shift+V, or look up other ways.

Meta noise

This is a cross-site copy of the same answer at How can I cancel reverse search without losing text typed?.

Upvotes: 0

c0der512
c0der512

Reputation: 655

You press "Escape" Key to terminate reverse-i-search. I'm speculating here. Since you have enabled find in your command line intentionally, the reverse search is triggered.

The reverse search searches index from newest to oldest. Since the cat fire.txt is always the recent search, it always shows up first.

Linux Bash now allows forward search using cntrl + S and backward search using cntrl + R . Please check if your terminal traps cntrl + S & cntrl + Q for flow control. If that's the case you can disable this by using stty -ixon

Hope this helps.

Upvotes: 2

Etan Reisner
Etan Reisner

Reputation: 81012

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.

Upvotes: 2

Related Questions