Skip to content

Instantly share code, notes, and snippets.

@mattmc3
Last active January 8, 2026 13:37
Show Gist options
  • Select an option

  • Save mattmc3/fbb4d90c3b131bdb5525900368467af2 to your computer and use it in GitHub Desktop.

Select an option

Save mattmc3/fbb4d90c3b131bdb5525900368467af2 to your computer and use it in GitHub Desktop.
Fish like up/down history/buffer navigation in Zsh
# 0 = history mode, 1 = edit mode
typeset -g __editing=0
# Up: history until editing begins
function smart-up() {
if (( __editing )); then
zle up-line-or-history
else
zle up-history
fi
}
zle -N smart-up
# Down: history until editing begins
function smart-down() {
if (( __editing )); then
zle down-line-or-history
else
zle down-history
fi
}
zle -N smart-down
# Reset state when command is accepted
function accept-line() {
__editing=0
zle .accept-line
}
zle -N accept-line
# Mark editing on any non-history action
function __mark_editing() {
__editing=1
zle .${WIDGET}
}
# Any normal editing widget flips the mode
for w in self-insert backward-char forward-char \
beginning-of-line end-of-line delete-char
do
zle -N $w __mark_editing
done
# Bind smart up and down
bindkey "${terminfo[kcuu1]}" smart-up
bindkey "${terminfo[kcud1]}" smart-down
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment