1

From reading the zsh docs, history and fc won't access the full ~/.zsh_history in non-interactive mode. I'm looking for a way to make this happen so I can script a search utility. Things I've tried:

➜  ~ fc -l 0 1
    1  ls
➜  ~ echo $HISTFILE
/Users/username/.zsh_history
➜  ~ zsh -c 'echo $HISTFILE'

➜  ~ zsh -c 'HISTFILE=~/.zsh_history; echo $HISTFILE'
/Users/username/.zsh_history
➜  ~ zsh -c 'HISTFILE=~/.zsh_history; history'
zsh:fc:1: no such event: 1
➜  ~ zsh -c 'HISTFILE=~/.zsh_history; fc -l 0 1'
zsh:fc:1: no events in that range
➜  ~ zsh -c 'HISTFILE=~/.zsh_history; fc -R; fc -l 0 1'
zsh:fc:1: no events in that range
➜  ~ zsh -c 'HISTFILE=~/.zsh_history; fc -R -l 0 1'
<NO OUTPUT>

In bash, there's a history option you can enable with set -o history but that doesn't exist for zsh.

The alternative approach is to access ~/.zsh_history file directly but then I'd lose out on the formatting support that history and fc do. Is there a built-in utility I could pipe ~/.zsh_history to do said formatting?

1

Call fc -R to read a history file. This only reads the last $HISTSIZE entries, so set HISTSIZE to a large value before calling fc.

HISTSIZE=999999999
fc -R ~/.zsh_history

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.