Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Finer control over what gets written to the history file



On Tue, 15 Oct 2013 09:41:00 -0700
Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> I often find myself running a lot of similar but not quite identical
> commands to test some obscure bug or otherwise experiment with a shell
> construct.  This stuff tends to push useful history out of the history
> file when I exit from the shell, if I don't remember to use my "die"
> alias that disables history and other exit-time operations.
> 
> It'd be nice to be able to selectively exclude those from history after
> the fact, also without having to remember to type a leading space on
> every such line.

You know about the zshaddhistory hook?  Excluding by pattern is a
fairly easy instance of this, and it should be readily extensible for
various different ways of checking.  You could make it configurable by
styles.

  zshistory_veto () {
    local -a line
    line=(${(Q)${(z)1}})
    # too lazy to do the array stuff...
    if [[ $line[1] = ${~HISTIGNORE} ]]
    then
    	return 1
    fi
  }
  autoload -Uz add-zsh-hook
  add-zsh-hook zshaddhistory zshaddhistory_veto

Just seen a typo.


zshaddhistory
       Executed  when  a  history line has been read interactively, but
       before it is executed.  The sole argument is the  complete  his‐
       tory  line  (so  that  any  terminating  newline  will  still be
       present).

       If any of the hook functions return a non-zero value the history
       line will not be saved, although it lingers in the history until
       the next line is executed allow you to reuse or edit it  immedi‐
       ately.

       A  hook function may call `fc -p ...' to switch the history con‐
       text so that the history is saved in a different file  from  the
       that  in  the  global  HISTFILE parameter.  This is handled spe‐
       cially: the history context is automatically restored after  the
       processing of the history line is finished.

       The  following  example  function first adds the history line to
       the normal history with the newline stripped,  which is  usually
       the  correct behaviour.  Then it switches the history context so
       that the line will be written to a history file in  the  current
       directory.

              zshaddhistory() {
                print -sr -- ${1%%$'\n'}
                fc -p .zsh_local_history
              }

pws



Messages sorted by: Reverse Date, Date, Thread, Author