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.
history
, is it also working when you only press theENTER
key and thenUP ARROW
?ENTER
and thenUP
the history is correct. What does that mean?