10

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.

3
  • Almost a duplicate of superuser.com/questions/302230/… , but I'm interested in reverting commands at will, rather than never saving them at all.
    – Sparhawk
    May 10, 2013 at 4:24
  • I'm always forgetting this one... damn those two-stroke emacs keyboard mappings.
    – Lambart
    Oct 21, 2014 at 0:05
  • @Lambart I agree. I feel that this is a very useful command for me… but I've never succeeded in memorising it!
    – Sparhawk
    Oct 21, 2014 at 0:08

3 Answers 3

8

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

1
  • 1
    This 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
    May 8, 2013 at 10:29
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.

6
  • 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
    May 8, 2013 at 6:56
  • @Sparhawk: well, it's still there, it's just the 3rd most recent command now.
    – ckhan
    May 8, 2013 at 8:20
  • @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
    May 8, 2013 at 8:31
  • 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
    May 8, 2013 at 10:22
  • 1
    It'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
    May 8, 2013 at 12:02
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.

2
  • 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
    May 10, 2013 at 4:22
  • 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
    Sep 19, 2014 at 12:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .