I have two suggestions how you can approach what you want (referring to bash
only):
add it to the history
Before typing the first history expansion command line you can disable history expansion (set +H
) and "execute" the history expansion command (and then reenable with set -H
). It then is part of the shell history and you can easily get back to it and modify it.
A more direct approach for getting the history expansion command line in the shell history would be history -s
. The earlier suggestion may be easier to remember (and may be easier in case of complicated quoting), though (depending on how familiar someone is with shell options).
readline yank
This is most useful when you do not need yanking during the whole operation.
Type the history expansion command line but do not press Enter. Instead go to the beginning / end of the line and delete the whole line with Ctrl-K / Ctrl-U. This puts the whole line on the kill ring. You can restore the line with Ctrl-Y. Even after executing the command you can get it back this way as long as you do not put anything else in the kill ring. And even if: You can go back to older kill ring entries with Ctrl-Y Alt-Y.
!!
invocation is expanded before it gets added to history. I don't think there's anything you can do, but I might be wrong.print -P
is zsh-specific but you're tagging with bothbash
andzsh
, and becausebash
is a more popular tag thanzsh
, the title of the page ends up beingbash - View History...
. Do you want a solution forzsh
,bash
or both?