Atuin "off the bottom" of local history (i.e. bound on ⬇️)

TL;DR: find below zsh / zle snippets for firing atuin when you push down arrow (specifically more times than or equivalent)

Whereas: When setting up atuin again after a re-image of hard drive, I was looking through configs and saw and quickly enabled the invert option (very much in keeping with self being person who first suggested Warp implement ~upside-down-mode),

And thereupon whereas: Overall, think its a better way to be, but then of course I experienced some cognitive dissonance when atuin still bound itself to trigger from

And separately whereas: While I was most of the time within the group of the people the docs refer to when they say " Many people who found it a bit jarring at first have since come to love it" , sometimes I just want to re-run (maybe a slight variation) of the last command (& my :brain:-macro still does Enter before e.g. !!for better or worse),

Therefore, I decided to put some time into seeing if I could configure logic for:

  • to go back to normal,
  • and as felt logical for me from that, for after to work “normally” as well … as far as local history had been visited, BUT
  • “at the bottom” (on a new prompt or e.g. 1 more times than to fire atuin search.)

And: yes, at least for the zsh set:

eval "$(atuin init zsh --disable-up-arrow)"

_down_line_or_search_but_atuin_if_at_end() {    
  if ! zle down-line-or-search -f nolast; then
    # keep 1 "row" of "just present an empty prompt" (on up[s] then down[s])
    if [[ "$LASTWIDGET" = "up-line-or-history" ]]; then
      zle -I
      zle kill-buffer
      case "$KEYMAP" in
        viins|viicmd) zle atuin-search-$KEYMAP ;;
        *) zle atuin-search ;; 

bindkey '^[[B' down-line-or-search-but-atuin-if-at-end
bindkey '^[OB' down-line-or-search-but-atuin-if-at-end
bindkey -M viins '^[[B' down-line-or-search-but-atuin-if-at-end
bindkey -M viins '^[OB' down-line-or-search-but-atuin-if-at-end
bindkey -M vicmd 'j' down-line-or-search-but-atuin-if-at-end

Additionally messing with this made me more aware of how atuin takes input from command line when that up arrow trigger fires.

And I think maybe I probably do still want that sometimes? in like “oh yeah that variation on this thing I was just trying from the other day”,

so I re-instituted atuin trigger for situations in which I have moved-from-but-returned-to the end of line (and/or shifted it by insertion - for example starting on a new prompt without traveling in history first)

(that is: when I’ve reduced what is between the cursor and end of buffer to only whitespace [unless I just landed there via o in vi mode, in which case its likely I actually want up arrow to go back to the previous line-- though at present the atuin-up bails on multi-line buffers regardless) )

# if I decide to push up-arrow after typing something or just after truncating to a prefix
# lets go ahead and assume I want atuin in the mix; but if I'm just pushing up, lets "be normal"
_atuin_up_search_if_prefix() {
  case "$LASTWIDGET" in
    up*) zle "$LASTWIDGET" ;;
       # provided that the cursor is (visually) at end of the buffer,
       if [[ "${#RBUFFER}" = 0 || "$RBUFFER" =~ "[[:space:]]+" ]]; then
        : # continue on to search
        zle up-line-or-search; 
    '') # "if the very first thing I do upon opening a new shell is push up"
      : ;& # fallthrough to atuin, but also might be a good thing to special case
      case "$KEYMAP" in
        viins|viicmd) zle atuin-up-search-$KEYMAP ;;
        *) zle atuin-up-search ;; 
    vi-open-line-below|*) zle up-line-or-search;;

zle -N atuin-up-search-if-prefix _atuin_up_search_if_prefix

bindkey '^[[A' atuin-up-search-if-prefix
bindkey '^[[OA' atuin-up-search-if-prefix 
bindkey -M vicmd 'k' atuin-up-search-if-prefix

These very much might just be my own special foibles; but perhaps not, so thought I’d share.

1 Like