1

I have a command that actually clears the screen instead of simply scrolling, like the default clear, aliased to c in my .zshrc:

alias c='clear && printf "\e[3J";'

My question is: how can I keep this command away from my zsh command history, so that when I scroll up with the up arrow key I don't see it?

noibe
  • 387
  • 5
  • 17
  • 1
    There's something about that in the manual, under HISTORY_IGNORE, but I have some trouble getting it to work (possibly because of the alias). – ilkkachu Mar 14 '20 at 16:28
  • Why not just press Ctrl+L? – Stéphane Chazelas Mar 14 '20 at 17:41
  • @StéphaneChazelas because Ctrl+L is the same as clear and doesn't actually clean the screen. Also, a single c is easier to reach than Ctrl+L – noibe Mar 14 '20 at 17:56
  • @ilkkachu the command passed as the 1st arg to zshaddhistory includes the terminating newline. –  Mar 14 '20 at 21:19
  • @mosvy, could be it, thanks. – ilkkachu Mar 14 '20 at 21:55
  • The command actually does clear the screen. The problem is that your terminal emulator does not. It does not correctly clear the screen in response to control sequences that say to clear the screen. And in response you are bodging around this instead of asking its authors to give you the screen clear behaviour of other terminal emluators. https://unix.stackexchange.com/a/375784/5132 – JdeBP Mar 16 '20 at 10:37

3 Answers3

1

With set -o histignorespace, command lines starting with a SPC character are not inserted in the history (though you can always recall the command that was entered immediately before).

So if you make it:

set -o histignorespace
alias c=' printf "\e[H\e[3J"'

And enter a command line whose first command is c, then that command line will not be added to the history.

Another approach could be to redefine the accept-line widget so that it cleans the screen and the editing buffer when it contains only c:

accept-line() case $BUFFER in
  (c) printf '\e[H\e[3J'; BUFFER=; zle redisplay;;
  (*) zle .accept-line
esac
zle -N accept-line

Note that it would only work if you enter c and that's all, not c; something-else for instance. It would also do it if you pressed Enter at the PS2 prompt like in:

for i do
c
  • That's a good workaround for older versions of zsh, but since 4.3.7 there's the zshaddhistory hook. – Gilles 'SO- stop being evil' Mar 14 '20 at 20:38
  • @Gilles zshaddhistory will however leave the command as the last entry in the history, until the next command is entered. Anyways: zshaddhistory(){ [[ ${1%?} != c ]] } –  Mar 14 '20 at 20:47
0

In bash, a HISTIGNORE='c' doesn't store a command line of a singlecto the memory list of commands. The similarly namedHISTORY_IGNOREin zsh is used to avoid sending the command to the history **file**, which is something different. AHIST_NO_STORE` also exists in zsh but seems to fail with aliases.

So, it seems that there is no solution to avoid an alias of c in history.

However, what you are doing, clearing the present window (clear) and removing the scrollback (printf "\e[3J") could also be done with another ANSI control sequence:

ESC c               # Full Reset (RIS), VT100.

So, a

printf '\ec'

should be enough.

Defining a keyboard shortcut for that should be simple and will avoid storing any command in the history.

0
Method1:

    Just Put one blank space before entering any command it wont be recorded in history

Method2:

    set +o history (After this No command will be recorded  in history)

    set -o history(After this again command will be recorded in history)
Paulo Tomé
  • 3,782