I have following answer for How to remove a single line from history?. When I do following, the line ( echo hello_world) is not saved into history. Please note that I am using zsh shell.

prompt$  echo hello_world
> #     ^ extra space

$ history | tail -n1
 1280  history | tail -n
$  echo hello_world
$ history | tail -n1
 1280  history | tail -n

But when I do run a command having a space at the beginning and right after do Ctrl+P, I can see it on the shell history, even though it is not save in history. Is it possible to prevent it? With the bash shell, this works when setting HISTCONTROL= ignorespace.

$  echo hello_world
$ # Press `CTRL-p` => " echo hello_world" shows up again

Setup: I have following configuration for the zsh shell:
## Save only one command if 2 common are same and consistent


## Delete empty lines from history file

## Ignore a record starting with a space

## Do not add history and fc commands to the history

## Add timestamp for each entry
  • Why do you run head -n1? The last command should be the last line of history not the first line. Commented Jul 16, 2020 at 9:14
  • Ah I am sorry, history was aliased to fc -rl - 0in order to reverse the output. For clarity I replaced head -n1 with tail -n1 @RaphaelAhrens
    – alper
    Commented Jul 16, 2020 at 9:47
  • What is the value of your $HISTCONTROL variable? (referred to in the Accepted answer unix.stackexchange.com/a/49216/117549)
    – Jeff Schaller
    Commented Jul 16, 2020 at 13:37
  • its ignoredups:erasedups:ignorespace I have also added ignoredups:erasedups
    – alper
    Commented Jul 16, 2020 at 13:41
  • 1
    I checked and it doesn't seem to be an available option.
    – KGIII
    Commented Jul 16, 2020 at 18:20

2 Answers 2


The behaviour that you're observing, i.e. that pressing Ctrl+P brings back the previous command even if it starts with a space and HIST_IGNORE_SPACE is set, is documented (my emphasis):


Remove command lines from the history list when the first character on the line is a space, or when one of the expanded aliases contains a leading space. Only normal aliases (not global or suffix aliases) have this behaviour. 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.

The workaround, according to the manual, is to type a single space and press Enter to prevent Ctrl+P from accessing that command again.

  • Can single space and pressing Enter be applied as post-execution after each command? or should I do it manually
    – alper
    Commented Apr 5, 2021 at 20:06
  • 1
    @alper I currently don't know if this could be automated. I suspect it could be automated, but my zsh-foo is not strong enough. I might get back to this later in the week.
    – Kusalananda
    Commented Apr 5, 2021 at 20:18

Not sure if this is what you are looking for, but before running commands that you would not want in the history you can disable it for the current shell with

$ set +o history

then ctrl-p will show only that and nothing afterwards

  • 1
    set: no such option: history
    – alper
    Commented Jul 16, 2020 at 12:16
  • 1
    this would be for bash shell, what shell are you using? If zsh I think you can do "setopt histignorespace"
    – ChadD
    Commented Jul 16, 2020 at 16:30
  • Ah sorry I forget to mention, I was using zsh. I have tried setopt histignorespace but I am still facing with the same problem :(
    – alper
    Commented Jul 16, 2020 at 17:31
  • I believe you have to do that and then in whatever command you are using it has to start with a space - so $ ls - not $ls - see if that works
    – ChadD
    Commented Jul 16, 2020 at 21:08
  • Please check the comments under my question; it mentioned that: I checked and it doesn't seem to be an available option.
    – alper
    Commented Jul 17, 2020 at 12:09

You must log in to answer this question.

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