0

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
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
setopt HIST_IGNORE_DUPS

setopt HIST_IGNORE_ALL_DUPS

Delete empty lines from history file

setopt HIST_REDUCE_BLANKS

Ignore a record starting with a space

setopt HIST_IGNORE_SPACE

Do not add history and fc commands to the history

setopt HIST_NO_STORE

Add timestamp for each entry

setopt EXTENDED_HISTORY

Kusalananda
  • 333,661
alper
  • 469

2 Answers2

1

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

HIST_IGNORE_SPACE (-g)

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.

Kusalananda
  • 333,661
  • Can single space and pressing Enter be applied as post-execution after each command? or should I do it manually – alper Apr 05 '21 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 Apr 05 '21 at 20:18
-2

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

ChadD
  • 97
  • 1
    set: no such option: history – alper Jul 16 '20 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 Jul 16 '20 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 Jul 16 '20 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 Jul 16 '20 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 Jul 17 '20 at 12:09