Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

migrated from stackoverflow.com Jun 25 '11 at 22:19

This question came from our site for professional and enthusiast programmers.

3  
I love the title of this question. You can actually alter history and you're complaining? What is wrong with you?! – Daniel Beck Jun 25 '11 at 22:24
    
@Daniel: Lol, yeah... I mean, it's as if I'm changing my past. Clearly nonsensical, and it gets ridiculously annoying after a while. :\ – Mehrdad Jun 25 '11 at 22:25
    
Btw, if you use history-search-xxx instead, the behavior's different. Maybe that's something for you? – Daniel Beck Jun 25 '11 at 22:29
    
@Daniel: That's indeed useful -- thanks a lot. But still, I'd like to know the answer to this question, because sometimes the commands don't look so much like each other, and that doesn't work. – Mehrdad Jun 25 '11 at 22:34
    
history-search with an empty prompt works just like regular history prev/next, i.e. displaying all entries. – Daniel Beck Jun 25 '11 at 22:35
up vote 11 down vote accepted

You want the readline setting:

set revert-all-at-newline on

You can either put it in ~/.inputrc, 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 manpage:

   revert-all-at-newline (Off)
          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.
share|improve this answer
    
You're awesome, that's exactly what I need. No longer do I want to punch my monitor. :D +1000000 (I wish) – Mehrdad Jun 26 '11 at 0:08
    
This has been bugging me for years. Thanks! – BillyBBone 21 hours ago

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.

share|improve this answer
    
Ha; I always assumed not saving commands with a leading space was a bug. Thanks. :) – sarnold Jun 25 '11 at 23:58

You must log in to answer this question.

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