Score:1

Handy command for editing previously executed mistyped command and overwriting history entry with the fixed one

za flag

until recently I was pretty sure that fc command opens previously executed command in the default editor, executes submitted changes and overwrites last entry from the bash history. However, seems like newly-executed command is actually just appended to the end of the bash history, so mistyped one (which wanted to be corrected) still remains.

As I find improper commands in the bash history very irritating, since from time to time I stumble upon them, I was wondering if bash shell could be somehow set-up to overwrite editing entry - primarily I'm interested in overwriting the previous entry, but solution for overwriting any entry from history would be even better! I'm quite sure that there is already something like that (probably even available as a property in some config file or as adding some argument on command invocation), but I haven't got any useful results while searching for it on the web.

I've come up on one answer which I am providing as an answer to this question, but if anyone else has even better solution, do not hesitate to share.

zeko868 avatar
za flag
Sorry, that's not really what I've been looking for, although there are some helpful segments inside of some answers. I've posted an answer as I originally intended and announced. Here is the complete history being deleted and I just wanted to remove the last entry from the history - preferably by editing it in a text editor like it's the case with the *fc* command.
Score:1
za flag

This solution seems quite promising to me after I've been using it for a while.

Add the following to your ~/.bashrc file:

shopt -s histappend
PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
fc_override_prev_command_if_no_args() {
        if [ $# -eq 0 ]; then
                fc
                history -d $(wc -l < ~/.bash_history)
                history -w
                history -c
                history -r
        else
                fc $@
        fi
}
alias fc=fc_override_prev_command_if_no_args

What this does is changing behaviour of the fc command when it is called with no arguments. When it is called with arguments, I made it to behave as it behaves usually, since otherwise its behaviour would be misleading without if/else/fi block, and without else part it wouldn't do anything at all.

On the other hand, when no arguments are provided, then fc command is executed, the last entry from the bash history is dropped and the cached history is reloaded from the file.

The part with reassignment of PROMPT_COMMAND variable I've borrowed from here and I found it useful as submitted commands are instantly available in bash histories of other currently-opened terminals with bash (OK, not really instantly, but even an action like sending SIGINT signal with Ctrl+C is enough to reload history cache).

Feel free to edit this answer or to comment if you find some parts of solution unnecessary.

Link to gist with the above snippet is available here

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.