4

I have setopt share_history and set opt inc_append_history set in my ~/.zshrc. This is almost working.

In session 1, I execute a command, it's written to history.

In session 2, the command is not visible when I press the UP arrow, until I execute history. Once I execute history it appears.

How can I get history to show up immediately when I press the UP arrow?

2
  • 1
    Instead of history, is it also working when you only press the ENTER key and then UP ARROW?
    – mpy
    Nov 21, 2014 at 21:02
  • Yes, if I please ENTER and then UP the history is correct. What does that mean?
    – jordelver
    Nov 23, 2014 at 15:34

1 Answer 1

4

When the Z shell displays the prompt it is idle and IMHO does not do any background tasks. So, the history file $HISTFILE is re-read only when rendering a new prompt. That why it is sufficient, but also necessary that you press ENTER in a terminal to re-read your history.

If you you want to save this additional keystroke you can use the following zle widget in your ~/.zshrc:

up-line-or-history-reread() {
        [[ -z $BUFFER ]] && fc -R $HISTFILE
        zle up-line-or-history
}
zle -N up-line-or-history-reread
bindkey '^[[A' up-line-or-history-reread

This new widget, which is bound to UP ARROW, checks if the current command line is empty and if so re-reads the history. After that it invokes the default widget (up-line-or-history).

This approach has at least these two flaws compared to the default behavior:

  • If you're up in the history a few command, and delete the entire command line, the next press on UP ARROW starts again at the end of the command history.
  • If you use some other history machanism, like history search, you still need to press the ENTER key before or you write a similar widget for these purposes, too.
1
  • Thanks, I was more confused thinking that it wasn't working. Now that I know how it works, it all makes sense. Thank you :)
    – jordelver
    Nov 24, 2014 at 12:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .