Em man bash
, encontrei a variável de ambiente HISTIGNORE
, cuja descrição é a seguinte:
HISTIGNORE
A colon-separated list of patterns used to decide which command
lines should be saved on the history list. Each pattern is anchored
at the beginning of the line and must match the complete line (no
implicit '*' is appended). Each pattern is tested against the line
after the checks specified by HISTCONTROL are applied. In addition
to the normal shell pattern matching characters, '&' matches the
previous history line. '&' may be escaped using a backslash; the
backslash is removed before attempting a match. The second and
subsequent lines of a multi-line compound command are not tested,
and are added to the history regardless of the value of HISTIGNORE.
Então, eu exportei o seguinte valor em ~/.bashrc
:
bind '"\C-xr": ". ~/.bashrc \C-x\C-z1\C-m"'
bind '"\C-x\C-z1": re-read-init-file'
export HISTIGNORE="clear:history:. ~/.bashrc "
A última linha deve impedir que bash
salve qualquer comando clear
, history
e mais importante . ~/.bashrc
.
Para zsh
, encontrei a opção HIST_IGNORE_SPACE
, descrita em man zshoptions
:
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.
De acordo com o que diz, se esta opção estiver habilitada, zsh
deve remover do histórico qualquer comando que comece com um espaço. No entanto, permanece temporariamente no histórico interno, até que o próximo comando seja executado. Então, eu configurei essa opção em ~/.zshrc
e, em seguida, redefinei a ligação de chave para recomprar a configuração do shell da seguinte forma:
setopt HIST_IGNORE_SPACE
bindkey -s '^Xr' ' . ~/.zshrc^M ^M'
# │ │
# │ └─ command containing only a space to force 'zsh'
# │ to “forget“ the previous command immediately
# └─ space added to prevent 'zsh' from logging the command