Last active
March 21, 2024 18:47
-
-
Save alextremblay/316d2beede9acd1a29d45d9d32dd310f to your computer and use it in GitHub Desktop.
atuin up-arrow mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# replace the default: | |
[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh | |
eval "$(atuin init bash)" | |
# with this: | |
[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh | |
eval "$(atuin init bash --disable-up-arrow)" | |
export ATUIN_ARROW_INDEX=-1 | |
export ATUIN_CYCLE_MODE=0 | |
__atuin__up_arrow() { | |
if [[ $ATUIN_CYCLE_MODE -eq 0 && -n "$READLINE_LINE" ]]; then | |
# execute traditional atuin_history up arrow function, interactive backwards prefix search or whatever | |
__atuin_history --shell-up-key-binding | |
return | |
fi | |
export ATUIN_ARROW_INDEX=$(( ATUIN_ARROW_INDEX + 1)) | |
export ATUIN_CYCLE_MODE=1 | |
HISTORY=$(atuin search --cmd-only --filter-mode host --limit 1 --offset $ATUIN_ARROW_INDEX) | |
READLINE_LINE=${HISTORY} | |
READLINE_POINT=${#READLINE_LINE} | |
return | |
} | |
__atuin__down_arrow() { | |
export ATUIN_ARROW_INDEX=$(( ATUIN_ARROW_INDEX - 1)) | |
if [[ $ATUIN_ARROW_INDEX -lt 0 ]]; then | |
__atuin__reset_arrow | |
HISTORY="" | |
else | |
HISTORY=$(atuin search --cmd-only --filter-mode host --limit 1 --offset $ATUIN_ARROW_INDEX) | |
fi | |
READLINE_LINE=${HISTORY} | |
READLINE_POINT=${#READLINE_LINE} | |
return | |
} | |
__atuin__reset_arrow() { | |
export ATUIN_ARROW_INDEX=-1 | |
export ATUIN_CYCLE_MODE=0 | |
} | |
bind -x '"\e[A": __atuin__up_arrow' | |
bind -x '"\eOA": __atuin__up_arrow' | |
bind -x '"\e[B": __atuin__down_arrow' | |
bind -x '"\eOB": __atuin__down_arrow' | |
precmd_functions+=(__atuin__reset_arrow) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
eval "$(atuin init zsh --disable-up-arrow)" | |
export ATUIN_ARROW_INDEX=-1 | |
export ATUIN_CYCLE_MODE=0 | |
__atuin__up_arrow() { | |
if [[ $ATUIN_CYCLE_MODE -eq 0 && -n "$READLINE_LINE" ]]; then | |
# execute traditional atuin_history up arrow function, interactive backwards prefix search or whatever | |
__atuin_history --shell-up-key-binding | |
return | |
fi | |
export ATUIN_ARROW_INDEX=$(( ATUIN_ARROW_INDEX + 1)) | |
export ATUIN_CYCLE_MODE=1 | |
COMMAND=$(atuin search --cmd-only --filter-mode host --limit 1 --offset $ATUIN_ARROW_INDEX) | |
LBUFFER="$COMMAND" | |
return | |
} | |
__atuin__down_arrow() { | |
export ATUIN_ARROW_INDEX=$(( ATUIN_ARROW_INDEX - 1)) | |
if [ $ATUIN_ARROW_INDEX -lt 0 ]; then | |
__atuin__reset_arrow | |
LBUFFER="" | |
return | |
fi | |
COMMAND=$(atuin search --cmd-only --filter-mode host --limit 1 --offset $ATUIN_ARROW_INDEX) | |
LBUFFER="$COMMAND" | |
return | |
} | |
__atuin__reset_arrow() { | |
export ATUIN_ARROW_INDEX=-1 | |
export ATUIN_CYCLE_MODE=0 | |
} | |
__atuin__return_key() { | |
__atuin__reset_arrow | |
zle accept-line | |
} | |
zle -N __atuin__up_arrow | |
zle -N __atuin__down_arrow | |
zle -N __atuin__return_key | |
bindkey '\eOA' __atuin__up_arrow | |
bindkey '\eOB' __atuin__down_arrow | |
bindkey '^M' __atuin__return_key |
This was very helpful to me, but it seems this has native support in atuin now.
this is working well for me and i prefer the behavior to the inline_height
solution because it preserves all other readline key bindings by default (eg. home/end, Ctrl+left/right, Ctrl+w,)
one caveat is that if you have anything in the command buffer, it will still use the atuin TUI.
also, users may want to change their filter mode.
with this modified version of the gist, Ctrl+N can be used to cycle filter modes:
[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh
eval "$(atuin init bash --disable-up-arrow)"
export ATUIN_ARROW_INDEX=-1
export ATUIN_CYCLE_MODE=0
__atuin__up_arrow() {
if [[ $ATUIN_CYCLE_MODE -eq 0 && -n "$READLINE_LINE" ]]; then
# execute traditional atuin_history up arrow function, interactive backwards prefix search or whatever
__atuin_history --shell-up-key-binding
return
fi
export ATUIN_ARROW_INDEX=$(( ATUIN_ARROW_INDEX + 1))
export ATUIN_CYCLE_MODE=1
HISTORY=$(atuin search --cmd-only --filter-mode ${ATUIN_FILTER_MODE} --limit 1 --offset $ATUIN_ARROW_INDEX)
READLINE_LINE=${HISTORY}
READLINE_POINT=${#READLINE_LINE}
return
}
__atuin__down_arrow() {
export ATUIN_ARROW_INDEX=$(( ATUIN_ARROW_INDEX - 1))
if [[ $ATUIN_ARROW_INDEX -lt 0 ]]; then
__atuin__reset_arrow
HISTORY=""
else
HISTORY=$(atuin search --cmd-only --filter-mode ${ATUIN_FILTER_MODE} --limit 1 --offset $ATUIN_ARROW_INDEX)
fi
READLINE_LINE=${HISTORY}
READLINE_POINT=${#READLINE_LINE}
return
}
__atuin__reset_arrow() {
export ATUIN_ARROW_INDEX=-1
export ATUIN_CYCLE_MODE=0
}
bind -x '"\e[A": __atuin__up_arrow'
bind -x '"\eOA": __atuin__up_arrow'
bind -x '"\e[B": __atuin__down_arrow'
bind -x '"\eOB": __atuin__down_arrow'
precmd_functions+=(__atuin__reset_arrow)
export ATUIN_FILTER_MODE="directory"
__atuin_filter_mode() {
if [[ "$ATUIN_FILTER_MODE" = "host" ]]; then
export ATUIN_FILTER_MODE="directory"
elif [[ "$ATUIN_FILTER_MODE" = "directory" ]]; then
export ATUIN_FILTER_MODE="session"
elif [[ "$ATUIN_FILTER_MODE" = "session" ]]; then
export ATUIN_FILTER_MODE="host"
fi
}
bind -x '"\C-n": __atuin_filter_mode'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@alextremblay While looking at working implementations of this behavior (e.g. https://github.com/zsh-users/zsh-history-substring-search) I found that nobody really hooks into CTRL-C. Instead they have variable in global scope which contains the previous line buffer content. If this matches with our current line buffer, we know that we are supposed to look at the next element. Otherwise reset the offset index.
For a zsh implementation of this, see here: https://gist.github.com/tyalie/7e13cfe2ec62d99fa341a07ed12ef7c0