50

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)?

Nevir
  • 603
  • 3
    Did not know about the bash shortcut. Definitely beats running history -d after running a command –  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 Answers2

61

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).

  • 1
    Aha, the empty space on its own is exactly what I was looking for, thanks! – Nevir Sep 16 '12 at 06: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. – Ray Andrews Oct 10 '15 at 22:02
4

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

depquid
  • 3,891
cas
  • 78,579