cookem cookem - 3 months ago 15
Bash Question

Prevent accidental history editing in bash

I recently switched from tcsh to bash, and one thing that keeps tripping me up is how bash handles history. I regularly bring up a previous history command and begin to edit it, but realize I need to run a different command first. In bash, this edits the original command, and I cannot see what I originally typed. In tcsh, the edited command is at the bottom of the stack when I use the up and down arrows, and is ready to continue editing or run when I am ready for it.

I found this question, which had several answers helpful in understanding how bash works in regards to rerunning and editing history, but I still don't know of a way to change this behavior (if such a thing exists). Is it possible to prevent bash from editing the commands that have already been run?

For example, if I run

abcd
efgh
ijkl


And then use arrow up and backspace efgh to efg, then hit down and type a different command "mnop", I want my history to look like:

abcd
efgh
ijkl
mnop


Currently it looks like:

abcd
* efg
ijkl
mnop


This may seem like a trivial issue but when running long commands with multiple pipes that require trial and error, it is some substantial loss of work.

Answer

Each line maintains its own undo list which you can access with C-x C-u or C-_ to undo the most recent change, or M-r to revert all changes since it was last saved.