0

I have noticed that when I go up through my commands using the arrow keys, UP and DOWN, that if I then edit a command, it edits it in history, and I can even remove a command like that, however I do not want it to have this affect on the commands in history, I don't want it to actually delete them in the history, only allow me to edit what I am typing, but not actually modify the history through this. So how can I make it so that this does not actually modify the history, but just the command that I execute?

3

I asked something quite similar in Unix & Linux: How to undo changes in the commands-history?.

The great answer I got from Gilles says:

As long as you've edited a history entry but not pressed Enter yet, to go back to the original entry, repeatedly press Ctrl+_ — the undo command — until it doesn't make any further change. You're back to the original entry.

  • 1
    So is there no way to disable the feature which means you are allowed to edit and delete previous entries? – user364819 Aug 6 '15 at 12:43
  • If you check the information with history, you will see it stores the initial info. So the data that is being changed is not that one, but the one you access through the arrows (which I don't know where it is stored). – fedorqui Aug 6 '15 at 12:51
  • 1
    @fedorqui it's stored in your home directory in a file called .bash_history (if you're using bash) – wb9688 Sep 29 '15 at 19:37
2

I encountered the same problem and while I was playing around and struggling to understand what was happening, I just figured out the following:

If you call from the history a command line that you have actually typed earlier in that same current terminal session and then edit it (maybe deleting it) but then interrupt your edit by leaving that (history-)command line using for instance the Down arrow to come back to the prompt of the actual current command line, then later on when you exit the terminal the edits get recorded in the .bash_history file.

This behavior does not happen if you edit and interrupt your edit on a command line that was already recorded in the .bash_history file before you started the terminal session.

In other words, while a terminal session is open, the newly typed command lines are only kept in memory, not in the .bash_history. You can recall them using the up arrow or invoking the history commands, and you can edit them while the session is open (when invoking the historycommand, the lines of modified commands will then be indicated by an asterisk *). It's only when you exit the session that the commands of the session get recorded 'as is' in the .bash_history file, thereby keeping the last edits you have made on them before exiting the session.

See also the answer from falconer there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy