It's not uncommon that I make a typo when entering commands in my bash interactive shell. I'd like to be able to correct the typo in my bash history so that the incorrect command does not pollute it and lead me to accidentally re-execute it later. In particular, I'd like to edit the last command.
There are quite a number of questions asking about how to prevent accidentally editing bash history. I want the opposite: I want to explicitly edit history.
Based on some of the explanations from the referenced questions, I tried doing:
$ echo foo
pressing Up, changing it to:
$ echo foobar
pressing Down, but that does nothing, and if I then press Enter, it will execute the modified command and leave both
echo foo
echo foobar
in my history.
I am aware that I can manually delete history entries with history -d
, but I haven't devised a good way to use that conveniently. I don't want to make a shell function to unconditionally delete the last history entry because I still want to be able to use Up to load the last entry so that I can correct it. I could make the correction and then delete the second-to-last history entry, but that feels clumsy and it's particularly annoying for a long-running command since I'd either need to remember to perform extra steps later or would need to temporarily suspend it, do those steps, and resume.
What I want:
Ideally what I'd like to be able to do is to press Up, make a correction to my previous command, and press some special keybinding or add some magic token to the command-line to cause it to replace the history entry when executed.
Also acceptable would be to press some other key sequence to retrieve and edit a command from history (similar to Ctrl+R) that overwrites the history entry when executed.
A robust shell function that removes the second-to-last history entry would be tolerable but non-ideal.
I imagine that surely other people make typos too and are similarly annoyed when such commands pollute their history. What do other people do?
history -d
or editing.bash_history
in an editor) are too inconvenient to use. I'm specifically asking for more convenient approaches, particularly in the context of correcting an existing line (which might not entail complete removal of the original). – jamesdlin Jul 17 '18 at 02:59man bash
); all features are documented. There are no hidden secrets. – Ipor Sircer Jul 17 '18 at 03:08