Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

This question already has an answer here:

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.

marked as duplicate by Gilles zsh Oct 14 '15 at 23:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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
up vote 23 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
Excellent. By adding the 'space, newline' to this: bindkey -s "^Z" '\C-a history 1 | grep --color=auto "\C-e"\C-m \C-m' ... I prevent the history command from showing up even of the first UP. – rayandrews Oct 10 '15 at 22:02

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

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