Skip to content

Instantly share code, notes, and snippets.

@olets
Last active February 4, 2026 01:55
Show Gist options
  • Select an option

  • Save olets/05192d42308500b690d9a671612220bd to your computer and use it in GitHub Desktop.

Select an option

Save olets/05192d42308500b690d9a671612220bd to your computer and use it in GitHub Desktop.
preview_zsh-abbr_expansion
This goes in .zshrc
# Copyright (c) 2026 Henry Bley-Vroman
# An addendum to zsh-abbr
# https://github.com/olets/zsh-abbr
# and released under its license
# https://github.com/olets/zsh-abbr/blob/main/LICENSE
# Key points from the license:
#
# You are free to
#
# - Share — copy and redistribute the material in any medium or format
# - Adapt — remix, transform, and build upon the material
#
# Under the following terms
#
# - Attribution — You must give appropriate credit, provide a link to the license,
# and indicate if changes were made. You may do so in any reasonable manner, but
# not in any way that suggests the licensor endorses you or your use.
# - Non-commercial — You may not use the material for commercial purposes.
# - [Hippocratic License 3.0 not https://creativecommons.org/licenses/by-nc-sa/4.0]
# Ethics - You must abide by the ethical standards specified in the Hippocratic License 3
# with Ecocide, Extractive Industries, US Tariff Act, Mass Surveillance, Military
# Activities, and Media modules.
# - Preserve terms — If you remix, transform, or build upon the material, you must
# distribute your contributions under the same license as the original.
# - No additional restrictions — You may not apply legal terms or technological measures
# that legally restrict others from doing anything the license permits.
# Limitations:
#
# - Requires zsh-abbr >= 6.5.0
#
# - The region_highlight memo stuff requires zsh > 5.8
#
# https://zsh.sourceforge.io/Doc/Release/Zsh-Line-Editor.html#User_002dDefined-Widgets
#
# - Doesn't seem play nice with selection menus
# load zsh-abbr
preview_zsh-abbr_expansion() {
local -i highlight_length
local highlight_memo
local -i highlight_offset
local highlight_specification
local -A reply
# There must be a nicer way of doing this, probably something
# related to zsh-abbr's _abbr_accept-line pattern
# https://github.com/zsh-users/zsh-syntax-highlighting/issues/974
(( ${+functions[_zsh_highlight__zle-line-pre-redraw]} )) && _zsh_highlight__zle-line-pre-redraw
highlight_memo="memo=preview_zsh-abbr_expansion"
highlight_specification="fg=cyan,underline"
region_highlight=( ${region_highlight:#*$highlight_memo} )
abbr-expand-line $LBUFFER $RBUFFER # sets `reply`
[[ -n $reply[expansion] ]] && {
highlight_length=$(( ${#reply[expansion]} ))
highlight_offset=$(( ${#reply[linput]%%$reply[abbreviation]} ))
region_highlight+=( "P$highlight_offset $highlight_length $highlight_specification $highlight_memo" )
zle -M "abbr expansion: $reply[expansion]"
return
}
abbr-set-line-cursor $BUFFER # sets `reply`
(( $? )) && {
zle -M ""
return 1
}
highlight_offset=$(( ${#reply[loutput]} ))
highlight_length=$(( ${#reply[loutput]} + ${#ABBR_LINE_CURSOR_MARKER} ))
region_highlight+=( "P$highlight_offset $highlight_length $highlight_specification $highlight_memo" )
zle -M "abbr cursor"
}
zle -N zle-line-pre-redraw preview_zsh-abbr_expansion
@olets
Copy link
Author

olets commented Feb 3, 2026

zsh-abbr v6.5.0 surfaces the line-processing features, largely in abbr-expand-line and abbr-set-line-cursor. The initial motivation was to make expansion previews possible, as proposed in discussion #199. This is a proof of concept I used to test the new functions during development.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment