11

When using reverse-search (Ctrl-R) in the bash terminal , I can cancel the search using either Ctrl-C or Ctrl-G. Both these options will clear the text typed while searching.

However, the most common reason I need to cancel my search is when I realize the command I wanted to type is not in my history, and this often happens only after I've typed a very long command.

Is there some way to cleanly abort the search without losing everything I typed?

1

3 Answers 3

1

You might want to look into a slightly different behaviour when using reverse search. If you start typing a command, and then press Ctrl+R but don't find the previously typed command, then pressing Ctrl+G does not erase the current text.

So my suggestion would be to first type some of the command, then use reverse search and if the required command is not found, exit using Ctrl+G to preserve your input.

Bash uses the readline library for this behaviour, so you can also check the readline section in the bash manual for more details.

0

Editor: bash

Just mark the command with the mouse, then press Ctrl+Shift+C, then Ctrl+G and you have it in memory.

Editor: tmux

Not sure whether I understand it right. I do not see a problem, and you give the answer yourself. That is how it seems, at least, when taking tmux as the editor. (not tested, but I guess that Alacritty can do that as well)

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 you write it as well: 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.

-3

Try using the cursor movement keys, e.g. Ctrl+B or Ctrl+F.

3
  • Is this experience based? or just a guess? This doesn't work for me. Commented Sep 25, 2016 at 16:44
  • This works for me, using bash version 4.1.2
    – J Earls
    Commented Sep 25, 2016 at 20:06
  • 4
    My bash reports its version as 4.3.11(1)-release. Your solution just inserts the command found into the command line and allows me to edit it. OP wants to keep what has been typed, not what has been (approximately) found. Are you sure this is what you get? Commented Sep 25, 2016 at 20:34

You must log in to answer this question.

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