0

When I am entering some command, often I need to run a different command. I once saw in a video where they hit ^C then later retrieved that command from history.

The answer here (2nd paragraph) also talks about this exact behaviour I am looking for. But doesn't say how to make it work.

Quoting the answer->

Alternatively, and this is particularly useful if you are in a nested command, such as a while or for loop, hit CTRL+C, which adds the command to history without executing it and clears the line, so you can then recall it using the shell's history mechanism when you are ready to use it.

I know we can use Ctrl+U or Ctrl+K then Ctrl+Y, but I am looking for this specific solution.

I went over all these sources:

https://superuser.com/questions/563939/save-command-to-history-without-executing-in-bash
How to save a command you entered without executing it?
Remember a half-typed command while I check something
https://stackoverflow.com/questions/35807653/how-do-i-add-a-command-to-bash-history-without-executing-it
https://superuser.com/questions/555310/bash-save-history-without-exit
Cancel Current Command but Save to History
Running a subshell from the middle of the current command
Coming back to a command I have partially written

  • The answer you quoted works only for multi-line commands. bash saves a string to history only after Enter. So I don't think bash can save commands directly to history like that. – davidt930 Jan 07 '24 at 05:38

1 Answers1

2

Shells do not typically enter a Ctrl-C-cancelled command into the history.

I can't find anything in the GNU Bash or GNU Readline documentation about enabling a behavior whereby cancelled commands would be put into the history anyway.

If you're typing a command and realize you need to run another one first, then jump to the start of the line and comment out your command with #. Then hit Enter to dispatch it. Commented lines are put into the history, so you can later recall it, delete the comment character and keep working on it.

Kaz
  • 8,273