-
-
Save alextremblay/316d2beede9acd1a29d45d9d32dd310f to your computer and use it in GitHub Desktop.
# 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) |
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 |
@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
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'
I'd love to somehow hook bash's ctrl-C with a function that resets the state variables and then does normal bash ctrl-C stuff, but to be honest I haven't the foggiest idea how to do that
i get around this limitation by getting into the habit of hitting enter after hitting ctrl-c, to trigger the precmd function. works well enough for me