How can I reset an edited history line in Bash?

7

2

In Bash, you can press the up (and down) arrow to traverse through history. You can then modify the command before executing it. Sometimes I've incorrectly modified the command, and wish to reset it to what was in history originally. Is there a way to do this?

I've tried executing a blank line, and then traversing through history again, but the pertinent line remains edited.

Sparhawk

Posted 2013-05-08T05:25:04.253

Reputation: 1 201

I'm always forgetting this one... damn those two-stroke emacs keyboard mappings. – Lambart – 2014-10-21T00:05:16.733

@Lambart I agree. I feel that this is a very useful command for me… but I've never succeeded in memorising it! – Sparhawk – 2014-10-21T00:08:30.850

Almost a duplicate of http://superuser.com/questions/302230/how-to-prevent-bash-from-altering-history , but I'm interested in reverting commands at will, rather than never saving them at all.

– Sparhawk – 2013-05-10T04:24:51.693

Answers

7

Undo is C-x C-u (or C-_, for which you may have to type C-/)

Uwe

Posted 2013-05-08T05:25:04.253

Reputation: 1 043

1This works, more or less. Ctrl+x then Ctrl+u; or Ctrl+Shift+-; or Ctrl+/ all work immediately after I've edited the command. Alternatively, I can go back to a previously-edited line (after executing other commands) and "undo" then. However, if I edit, execute a subsequent command, "undo" the edited command, then traverse the command history again, the edit command reverts to the pre-undo state, and undo now does nothing. – Sparhawk – 2013-05-08T10:29:06.753

1

The file that contains the bash history is "~/.bash_history", fittingly enough(at least in ubuntu, might be different in other flavors). just open it with your favorite text editor, note that each user should have their own .bash_history file. Also note that the preceding "." means that it is a hidden file, so it will not show up in a file browser or with the ls command unless you use it with -a.

ghost

Posted 2013-05-08T05:25:04.253

Reputation: 59

Perhaps I was being vague. I know that this is the location of the history file, but I was wondering if there were a quick and easy way of determining the original command directly. e.g. if I modified the second-last command, and wanted to run it again un-edited. – Sparhawk – 2013-05-08T06:56:17.067

@Sparhawk: well, it's still there, it's just the 3rd most recent command now. – ckhan – 2013-05-08T08:20:52.133

@ckhan Yes, but you have to execute some other command first, before it becomes visible again. A null command : is sufficient, but a blank line (as tried by Sparhawk) is not. – Uwe – 2013-05-08T08:31:23.487

For me, : did not reset the edited command. To be explicitly clear, I tried typing in echo foo, enter. up, backspace, down, :, enter. up, up, and I still saw echo fo, not echo foo. – Sparhawk – 2013-05-08T10:22:57.467

1It's even more complicated. Bash seems to restore the originally executed command, if you go back to its modified version using up, erase that one completely (C-u), press enter, and then again go back in the history. Don't ask me why. – Uwe – 2013-05-08T12:02:43.543

@Uwe Nice find. You should submit this as another answer (can you do that?), so I can upvote you twice. – Sparhawk – 2013-05-09T03:11:30.763

1

This binding will prevent un-executed history alterations from over-writing the originals:

bind 'set revert-all-at-newline on'

Example:

  1. Execute ls
  2. Press up arrow
  3. Add -Al to the line
  4. Press down arrow
  5. Press Return (to try to cancel the change)

Without this binding:
ls will be replaced in history by ls -Al, even though it was never executed.

With this binding:
The un-executed ls -Al will disappear, whilst ls remains.

Obviously this means that un-executed history alterations will never be saved (like zsh's default behaviour). If you only want the originals restored sometimes, you'll have to go with one of the other answers.

kine

Posted 2013-05-08T05:25:04.253

Reputation: 1 669

I just wanted to add a note that this answer only refers to the currently-active terminal. That is, either setting will not affect the contents of ~/.bash_history or other terminals. – Sparhawk – 2014-09-19T12:20:51.677

Good to know. I just realised there is an almost-dupe question, so I'll accept one of the other answers, that more specifically suits my question (reverting as opposed to not saving). – Sparhawk – 2013-05-10T04:22:51.623