Put a command in history without executing it
Option four (as an answer to the title of the question, not its explanation ;-):
print -S "this line goes to the history"
Excerpt from zshbuiltins(1)
: "print -S
Place the results in the history list instead of on the standard output. In this case only a single argument is
allowed; it will be split into words as if it were a full shell command line. The effect is similar to reading
the line from a history file with the HIST_LEX_WORDS option active."
Three options:
zsh has the keyboard shortcut Alt + Q by default that pushes the current line.
Use it like this:
Type command.
Press Alt + Q.
Execute othercommand.
command will reappear.
Technically, this doesn't put it in the history. Also, if you need to execute another command before command, you have to press Alt + Q again.
Also by default, the keyboard shortcuts Ctrl + U and Ctrl + Y cut and yank (paste) the current line.
Use them like this:
Type command.
Press Ctrl + U.
Execute othercommand.
Press Ctrl + Y.
command will reappear.
This would work in bash as well and it uses its own clipboard, i.e., they contents of the clipboard managed by Ctrl + (Shift +) C / V and select / middle-click will remain unaltered.
Last but not least, you can permanently enable comments on the command-line by executing
echo setopt interactivecomments >> ~/.zshrc
Alt + # doesn't work here, but Ctrl + A / Home, #, Enter achieves the same result.
To see all available keyboard shortcuts, execute
bindkey
A widgetification of @mpy's answer, add this to your .zshrc
to-history() { print -S $BUFFER ; BUFFER= }
zle -N to-history
bindkey 'œ' to-history
Note: œ is what I get on a Mac for alt/option-q, adjust for your terminal