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
bash
saves a string to history only afterEnter
. So I don't think bash can save commands directly to history like that. – davidt930 Jan 07 '24 at 05:38