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
13
  • Why do you run head -n1? The last command should be the last line of history not the first line. Jul 16 '20 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
    Jul 16 '20 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
    Jul 16 '20 at 13:37
  • its ignoredups:erasedups:ignorespace I have also added ignoredups:erasedups
    – alper
    Jul 16 '20 at 13:41
  • 1
    I checked and it doesn't seem to be an available option.
    – KGIII
    Jul 16 '20 at 18:20
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.

2
  • Can single space and pressing Enter be applied as post-execution after each command? or should I do it manually
    – alper
    Apr 5 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 5 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

5
  • set: no such option: history
    – alper
    Jul 16 '20 at 12:16
  • this would be for bash shell, what shell are you using? If zsh I think you can do "setopt histignorespace" 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 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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