Undoing bash command text deletion [duplicate]

If I press the Up or Down arrows on my keyboard and then modify something from my history, it's changed forever. Even if I press Ctrl-C and then try to bring it up again, it's still changed -- I've lost that entry in my history.

How can I prevent this from happening?


You want the readline setting:

set revert-all-at-newline on

You can either put it in ~/.inputrc (see note below), or put bind 'revert-all-at-newline on' in your ~/.bashrc.

Demo:

$ man bash
$ bind 'set revert-all-at-newline on'
$ man bsh # up arrow and edit
No manual entry for bsh
$ man bash # three up arrows

Further details are in the Bash manpage:

revert-all-at-newline

If set to ‘on’, Readline will undo all changes to history lines before returning when accept-line is executed. By default, history lines may be modified and retain individual undo lists across calls to readline. The default is ‘off’.


Note:

If a new ~/.inputrc file is created for the purpose of setting revert-all-at-newline, be aware that bash will use the readline settings in this file instead of any settings which may be in the file /etc/inputrc. That is, any settings specified in /etc/inputrc will no longer be in effect. Therefore, if the /etc/inputrc file exists, it's a good idea to start ~/.inputrc with the line:

$include /etc/inputrc


I enter:

ls /tmp

- wonderful. Now I wan't to enter

ls /temp 

and can prevent it to enter the history, therefore prevent it to overwrite ls /tmp, if I start the command with a blank:

 ls /temp

It's hard to see, but if you know it ...

It is controlled by

export HISTCONTROL=ignoredups
export HISTCONTROL=ignoreboth

ignoredups only ignores duplicated commands, ignoreboth ignores spaces at the beginning of line, which is useful, to hide otherwise unhidden passwords.

But maybe you're out for a solution, where you end with both commands, the unmodified old one, and the new one. My version of bash or settings behave like this, but I don't know, what's different to yours.