Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
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 '13 at 4:24
add comment

3 Answers

up vote 4 down vote accepted

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

share|improve this answer
    
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 '13 at 10:29
add comment

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.

share|improve this answer
    
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 '13 at 6:56
    
@Sparhawk: well, it's still there, it's just the 3rd most recent command now. –  ckhan May 8 '13 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 '13 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 '13 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 '13 at 12:02
show 1 more comment

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.

share|improve this answer
    
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 '13 at 4:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.