Discussion:
zshaddhistory confusion
Jay Levitt
2011-09-26 17:11:15 UTC
Permalink
In addition to my normal zsh history, I want to write to a
.zsh_history_detail file with comments showing date, pwd, etc. I'm
trying

function zshaddhistory() {
print -sr -- ${1%%\n'}
fc -p .zsh_history_detail
print -sr "${1%%\n'} ### ${PWD} `date '+%Y-%m-%d %R'`"
}

but it now
1. writes what looks like a pid into .zsh_history, and
2. puts both the normal AND the detailed history into .zsh_history_detail.

I am probably misunderstanding the fairly terse zshaddhistory docs. Any hints?
Benjamin R. Haskell
2011-09-27 03:01:47 UTC
Permalink
Post by Jay Levitt
In addition to my normal zsh history, I want to write to a
.zsh_history_detail file with comments showing date, pwd, etc. I'm
trying
function zshaddhistory() {
print -sr -- ${1%%\n'}
${1%%\n'} -> ${1%%$'\n'}
Post by Jay Levitt
fc -p .zsh_history_detail
print -sr "${1%%\n'} ### ${PWD} `date '+%Y-%m-%d %R'`"
same here: missing $' before the \n'
Post by Jay Levitt
}
but it now
1. writes what looks like a pid into .zsh_history, and
2. puts both the normal AND the detailed history into .zsh_history_detail.
I am probably misunderstanding the fairly terse zshaddhistory docs.
Any hints?
It's mostly the missing syntax. But, you also somewhat
counterintuitively also have to return 1, so that zsh doesn't append an
implicit:

print -sr -- "${1%%$'\n'}"

If you return a true value, the shell thinks you've done what you want
to do and that it should log the line normally. Also, I added ~/ (the
example in the docs is to have a per-directory history file).

zshaddhistory () {
print -sr -- "${1%%$'\n'}"
fc -p ~/.zsh_history_detail
print -sr -- "${1%%$'\n'} ### ${PWD} $(date '+%Y-%m-%d %R')"
return 1
}


The following doesn't seem to work, though I'd have thought it would
based on the documentation...

zshaddhistory () {
fc -p ~/.zsh_history_detail
print -sr -- "${1%%$'\n'} ### ${PWD} $(date '+%Y-%m-%d %R')"
fc -P
}
--
Best,
Ben
Bart Schaefer
2011-09-27 14:17:36 UTC
Permalink
On Sep 26, 11:01pm, Benjamin R. Haskell wrote:
}
} The following doesn't seem to work, though I'd have thought it would
} based on the documentation...
}
} zshaddhistory () {
} fc -p ~/.zsh_history_detail
} print -sr -- "${1%%$'\n'} ### ${PWD} $(date '+%Y-%m-%d %R')"
} fc -P
} }

"Doesn't work" how? What does it [not] do that you were expecting?

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

What "handled specially" means here is that pushing the history stack
when you are inside the history hook automatically disables writing
to the normal history file, regardless of whether you "return 1" or
call "fc -P" or anything else. That should probably be made more
explicit in the doc, the only thing it's describing is the implicit
"fc -pa".
Benjamin R. Haskell
2011-09-28 04:11:10 UTC
Permalink
Post by Bart Schaefer
}
} The following doesn't seem to work, though I'd have thought it would
} based on the documentation...
}
} zshaddhistory () {
} fc -p ~/.zsh_history_detail
} print -sr -- "${1%%$'\n'} ### ${PWD} $(date '+%Y-%m-%d %R')"
} fc -P
} }
"Doesn't work" how? What does it [not] do that you were expecting?
A hook function may call `fc -p ...' to switch the history context
so that the history is saved in a different file from the that in
the global HISTFILE parameter. This is handled specially: the
history context is automatically restored after the processing of
the history line is finished.
What "handled specially" means here is that pushing the history stack
when you are inside the history hook automatically disables writing to
the normal history file, regardless of whether you "return 1" or call
"fc -P" or anything else. That should probably be made more explicit
in the doc, the only thing it's describing is the implicit "fc -pa".
Yes, it sounds like you understand my confusion:

I expected the implicit "fc -pa".

I didn't expect that writing to the normal history file was disabled.
--
Best,
Ben
Jay Levitt
2011-10-01 20:44:16 UTC
Permalink
Post by Benjamin R. Haskell
zshaddhistory () {
  print -sr -- "${1%%$'\n'}"
  fc -p ~/.zsh_history_detail
  print -sr -- "${1%%$'\n'} ### ${PWD} $(date '+%Y-%m-%d %R')"
  return 1
}
Thanks for the help! This fixes the double-write to
.zsh_history_detail, but is still writing weird stuff to .zsh_history:

: 1317501595:0;echo autojump.zsh
: 1317501605:0;git st
: 1317501607:0;echo git st
: 1317501691:0;cd
: 1317501694:0;tail .zsh_history

Ideas?
Geoff Wing
2011-10-02 00:21:26 UTC
Permalink
On Saturday 2011-10-01 16:44 -0400, Jay Levitt output:
:Thanks for the help! This fixes the double-write to
:.zsh_history_detail, but is still writing weird stuff to .zsh_history:
:
:: 1317501595:0;echo autojump.zsh
:: 1317501605:0;git st
:: 1317501607:0;echo git st
:: 1317501691:0;cd
:: 1317501694:0;tail .zsh_history
:Ideas?

If you mean the file format: Timestamp:duration;command

This is from "setopt EXTENDED_HISTORY"

Regards,
Geoff
Benjamin R. Haskell
2011-10-01 21:24:01 UTC
Permalink
Post by Jay Levitt
Post by Benjamin R. Haskell
zshaddhistory () {
  print -sr -- "${1%%$'\n'}"
  fc -p ~/.zsh_history_detail
  print -sr -- "${1%%$'\n'} ### ${PWD} $(date '+%Y-%m-%d %R')"
  return 1
}
Thanks for the help! This fixes the double-write to
: 1317501595:0;echo autojump.zsh
: 1317501605:0;git st
: 1317501607:0;echo git st
: 1317501691:0;cd
: 1317501694:0;tail .zsh_history
By "weird stuff" I assume you mean the prefix?

': ' {timestamp} ':' {runtime} ';'

That's controlled by the 'extended_history' option. You can turn it off
via:

setopt no_extended_history

I love it (It provides half of what you're adding with your hook.), but
I've also wanted to add PWD to my historyfiles for a while.
--
Best,
Ben
Loading...