Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Both bash and zsh support a shorthand of not placing a command in history if you prepend it with a space. This works great across sessions (if you've setopt histignorespace). However, the command is still in the current session's history.

How can I avoid placing a command in the current session's history (or remove if after executing it)?

share|improve this question

migrated from stackoverflow.com Sep 16 '12 at 1:09

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

2  
Did not know about the bash shortcut. Definitely beats running history -d after running a command –  Dave Snigier Sep 15 '12 at 23:09
    
If I use your shortcut in bash I don't see the command even in the current session's history (GNU bash 4.2.24). –  Matteo Italia Sep 15 '12 at 23:30

2 Answers 2

up vote 15 down vote accepted

With setopt histignorespace, the command is removed from the current session history. If you tested by pressing Up and seeing that the command line is still there, it's a feature.

Note that the command lingers in the internal history until the next command is entered before it vanishes, allowing you to briefly reuse or edit the line. If you want to make it vanish right away without entering another command, type a space and press return.

If you typed a command that didn't start with a space or didn't have the histignorespace option turned on, then there's no way to remove the command from the current session's history (you can edit the history file externally).

share|improve this answer
    
Aha, the empty space on its own is exactly what I was looking for, thanks! –  Nevir Sep 16 '12 at 6:48

If you were using bash, instead of zsh, you could use these techniques to remove a command from history:

Press up-arrow to get to the line, then press Ctrl-U to erase it. Don't press ENTER, instead press up or down arrow to go to another history line.

The line you want to erase will be replaced by an empty line. I don't know if this is documented anywhere, I noticed it by accident several years ago...I guess the erase is just a fairly thorough :) edit of the history line.

e.g instead of pressing ^U to clear the line, change something instead. DON'T HIT ENTER, as that would create a new history entry. Press up or down arrow instead. The line is now changed permanently in the history, as you can see by by using up/down arrow to bring it back into view.

By permanent, I mean that after pressing up/down arrow keys, the line will remain changed even if you hit enter on a new/edited command. Only the edited line will remain in history replacing the original, and will get saved to your $HISTFILE as usual when you exit that shell.

You can press ^C to abort either an edit or ^U erase, to stop it affecting the history.

Another method is to use history -d if you know the history line number. For example:

$ history | grep history | tail
[...irrelevant, deleted...]
10113  help history
10114  history | grep history | tail

$ history -d 10113

$ history | grep history | tail
[...irrelevant, deleted...]
10113  history -d 10113
10114  history | grep history | tail

The help history line was deleted by the history -d 10114

share|improve this answer

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.