Contextual abbreviations (cabbrevs) are like aliases that are only valid
in certain contexts and expand automatically. For example, the cabbrev
docker,a,attach
means "Expand a
to attach
, but only when the current
command line starts with docker
". With this cabbrev, when you type
docker a<SPACE>
, it will automatically expand to docker attach
, but
echo a<SPACE>
is still just echo a
.
The following demo shows just how many keystrokes you can save. docker a
expands to docker attach
. Pressing space again shows the available cabbrevs,
entering n d
results in docker attach --no-stdin --detach-keys
.
Another example: git ce q j 4 be
expands to git clone --quiet --jobs 4 --bare
.
This repo also contains a script for generating cabbrevs from --help
text.
Clone the repo somewhere:
git clone https://github.com/langston-barrett/zsh-contextual-abbrevs ~/somewhere
Then add this line to your ~/.zshrc
:
source ~/somewhere/src/contextual-abbrevs.zsh
TODO(#1): PRs welcome :-)
cabbrevs are defined in ZSH arrays named _ABBREVS
, like so:
_ABBREVS=(
"git restore,s,--source"
"git restore,p,--patch"
"git restore,W,--worktree"
)
(This example was extracted from git.zsh.)
Each file in ./src/abbrevs
contains one such array.
These arrays must be sorted in decreasing prefix length.
You can generate abbreviations from help text using
extract-abbrevs.py
, e.g.
./scripts/extract-abbrevs.py git
Contextual abbreviations work like globalias or zsh-abbr,
except that they are contextual - a
expands differently when it comes after
*git
vs. docker
.