Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
daanvdk committed Jan 3, 2025
0 parents commit f079c1b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# ZSH Autosuggest Abbr History
This plugin defines a strategy called `abbr_history` for zsh-autosuggestions
that works similar to the standard history strategy but takes `zsh-abbr` into
account.

## Example
Lets say you have the following abbr setup:
```sh
abbr dc="docker compose"
```

And you typed `dc ps` which expanded to `docker compose ps`. If you now type
type `dc` again this will not match the `docker compose ps` entry in your
history and thus the standard history strategy will not suggest anything.

With the strategy of this plugin instead it will suggest `dc ps` and on
suggestion accept expand that to `docker compose ps` for you.

## Sidenotes
At the moment zsh-autosuggestions only supports appending a suggestion. Since
this plugin has to expand the abbreviation this is not enough. So at the moment
this plugin only works with my fork of zsh-autosuggestions.
32 changes: 32 additions & 0 deletions zsh-autosuggest-abbr-history.plugin.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function _zsh_autosuggest_strategy_abbr_history() {
emulate -L zsh
setopt EXTENDED_GLOB

local prefix="${1//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}"
local pattern="($prefix*"
IFS=$'\n'
for line in $(abbr list); do
if [[ $line =~ '^"(.*)"="(.*)"$' ]]; then
local abbr=$match[1]
local command=$match[2]
if [[ $abbr == "$prefix"* ]]; then
pattern+="|$command(| *)"
fi
fi
done
pattern+=")"

suggestion="${history[(r)$pattern]}"

for line in $(abbr list); do
if [[ $line =~ '^"(.*)"="(.*)"$' ]]; then
local abbr=$match[1]
local command=$match[2]
if [[ $abbr == "$prefix"* && $suggestion == "$command"(| *) ]]; then
new_buffer=$suggestion
suggestion=$abbr${suggestion:${#command}}
break
fi
fi
done
}

0 comments on commit f079c1b

Please sign in to comment.