Every now and then it would be useful to add a command directly to the history in Bash without actually executing it. So far the closed thing I have found is adding a
# in front of it and hitting return. Are there better ways?
history -s command
You can even bind a keystroke to do this for you. You can enter this at a Bash prompt:
bind '"\C-q": "\C-a history -s \C-j"'
or add this to your
"\C-q": "\C-a history -s \C-j"
then you can type something and press Ctrl-q and it will be added to the history without being executed. The space before "history" causes the
history command itself to not be added to the history if your
HISTCONTROL variable contains
ignoreboth. Another keystroke could be chosen instead of "\C-q".
It is hard to come up with anything shorter than a one character "command", so we really need to go to the keystroke level. With my bash setup and an US keyboard what you do now takes:
- Go to beginning of line: CTRLA: +1 instruction, +2 keypresses
#(needs SHIFT): +1 instruction, +2 keypress
- Appending it to the history with ENTER: +1 instruction, +1 keypress
so in total 3 instructions, 5 keypresses.
Using ALT# does the same in 1 instruction, 2 keypresses.
YMMV depending on your keyboard layout and bash configuration.