zsh: migrate to zsh-abbr

Migrate from `zsh-expand` to `zsh-abbr` as it seems more performant and
isn't giving me unwanted expansions, such as when typing `run` within
this repository and it completing to something else.

Whilst Home Manager does have `programs.zsh-attr`, there isn't an option
for setting global abbreviations that expand anywhere in the command,
which I need to replace global ZSH aliases such as `G` which expands to
`| grep`.

For now, I've installed `zsh-abbr` via zplug and am creating the
`user-abbreviations` file using Home Manager where I can use `attr -g`
for global aliases.

If or when support is added for global abbreviations in Home Manager,
I'll switch to `programs.zsh-abbr.abbreviations`.

`programs.zsh.shellAliases` is still used for aliases that shouldn't
expand, whereas everything else has been moved to abbreviations.
This commit is contained in:
Oliver Davies 2024-06-25 10:19:28 +01:00
parent d277199c23
commit 0df5f17dae

View file

@ -2,82 +2,24 @@
{ {
programs.zsh = { programs.zsh = {
enable = true;
enableCompletion = false;
dotDir = ".config/zsh"; dotDir = ".config/zsh";
enable = true;
enableCompletion = true;
# Aliases (which don't expand).
shellAliases = { shellAliases = {
"-" = "cd -"; "-" = "cd -";
".." = "cd .."; ".." = "cd ..";
"..." = "cd ../.."; "..." = "cd ../..";
"...." = "cd ../../.."; "...." = "cd ../../..";
"....." = "cd ../../../.."; "....." = "cd ../../../..";
dea = "direnv allow";
dee = "direnv edit";
cat = "bat"; cat = "bat";
cs = "create-script";
daily = "run create-daily next";
ls = "lsd";
run = "./run"; run = "./run";
s = "secrets"; s = "secrets";
secrets = ''doppler --project "$(whoami)" run''; secrets = ''doppler --project "$(whoami)" run'';
switch = "run nixos nixedo switch";
sz = "source ~/.config/zsh/.zshrc";
tag = "tag-release"; tag = "tag-release";
wt = "git worktree"; wt = "git worktree";
vss = "LC_ALL=C sort --unique ~/Code/personal/opdavies.nvim/spell/en.utf-8.add --output ~/Code/personal/opdavies.nvim/spell/en.utf-8.add"; vss = "LC_ALL=C sort --unique ~/Code/personal/opdavies.nvim/spell/en.utf-8.add --output ~/Code/personal/opdavies.nvim/spell/en.utf-8.add";
# tmux
ta = "tmux attach";
tl = "tmux list-sessions";
tk = "tmux kill-session";
# Docker and Docker Compose.
dk = "docker";
dkp = "docker ps";
dkpa = "docker ps -a";
dkpaq = "docker ps -a -q";
dkb = "docker build -t";
dks = "docker start";
dkt = "docker stop";
dkrm = "docker rm";
dkri = "docker rmi";
dke = "docker exec -ti";
dkl = "docker logs -f";
dki = "docker images";
dkpu = "docker pull";
dkph = "docker push";
dkbnc = "docker build --no-cache -t";
dkr = "docker run --rm";
dkrti = "docker run --rm -ti";
dkc = "docker compose";
dkcb = "docker compose build";
dkcu = "docker compose up";
dkclean = "docker ps -q -a -f status=exited | xargs -r docker rm && docker images -q -f dangling=true | xargs -r docker rmi";
# run scripts.
r = "run";
rc = "run composer";
rd = "run drush";
rdcr = "run drush cr";
rdup = "run drush updb -y";
rdce = "run drush config:export -y";
rdci = "run drush config:import -y";
rduli = "run drush uli";
};
shellGlobalAliases = {
A1 = "| awk '{print $1}'";
Fj = "| jq .";
Fy = "| yq .";
G = "| grep";
GH = "| grep HTTP";
Gi = "| grep -i";
H2 = "| head -n 20";
H = "| head";
L = "| less";
V = "| vim -";
X = "| xargs -I1";
}; };
initExtra = '' initExtra = ''
@ -128,7 +70,6 @@
} }
# Plugins # Plugins
source "''${ZPLUG_REPOS}/MenkeTechnologies/zsh-expand/zsh-expand.plugin.zsh"
source "''${ZPLUG_REPOS}/joshskidmore/zsh-fzf-history-search/zsh-fzf-history-search.plugin.zsh" source "''${ZPLUG_REPOS}/joshskidmore/zsh-fzf-history-search/zsh-fzf-history-search.plugin.zsh"
source "''${ZPLUG_REPOS}/robbyrussell/oh-my-zsh/plugins/git/git.plugin.zsh" source "''${ZPLUG_REPOS}/robbyrussell/oh-my-zsh/plugins/git/git.plugin.zsh"
source "''${ZPLUG_REPOS}/robbyrussell/oh-my-zsh/plugins/vi-mode/vi-mode.plugin.zsh" source "''${ZPLUG_REPOS}/robbyrussell/oh-my-zsh/plugins/vi-mode/vi-mode.plugin.zsh"
@ -182,42 +123,83 @@
name = "plugin/git"; name = "plugin/git";
tags = [ "from:oh-my-zsh" ]; tags = [ "from:oh-my-zsh" ];
} }
{ {
name = "plugin/vi-mode"; name = "plugin/vi-mode";
tags = [ "from:oh-my-zsh" ]; tags = [ "from:oh-my-zsh" ];
} }
{ name = "MenkeTechnologies/zsh-expand"; } { name = "olets/zsh-abbr"; }
{ name = "zsh-users/zsh-completions"; } { name = "zsh-users/zsh-completions"; }
{ name = "zsh-users/zsh-syntax-highlighting"; } { name = "zsh-users/zsh-syntax-highlighting"; }
]; ];
}; };
}; };
programs.zsh.localVariables = { home.file.".config/zsh-abbr/user-abbreviations".text = ''
ZPWR_EXPAND = true; abbr dea="direnv allow"
ZPWR_EXPAND_BLACKLIST = [ abbr dee="direnv edit"
"cat" abbr cs="create-script"
"cs" abbr daily="run create-daily next"
"daily" abbr switch="run nixos nixedo switch"
"full-system-clean" abbr sz="source ~/.config/zsh/.zshrc"
"full-system-repair"
"full-system-upgrade" abbr g="git"
"list-system-configurations" abbr ga="git add"
"set-default-boot" abbr gap="git add -p"
"system-rebuld" abbr gc="git commit"
"system-repair" abbr gri="git rebase -i"
"system-upgrade-information" abbr gs="git status
"ls"
"run" # tmux
"s" abbr ta="tmux attach"
"secrets" abbr tl="tmux list-sessions"
"sz" abbr tk="tmux kill-session"
"tag"
"vss" # Docker and Docker Compose.
"wt" abbr dk="docker"
]; abbr dkp="docker ps"
ZPWR_EXPAND_NATIVE = true; abbr dkpa="docker ps -a"
ZPWR_EXPAND_SECOND_POSITION = true; abbr dkpaq="docker ps -a -q"
}; abbr dkb="docker build -t"
abbr dks="docker start"
abbr dkt="docker stop"
abbr dkrm="docker rm"
abbr dkri="docker rmi"
abbr dke="docker exec -ti"
abbr dkl="docker logs -f"
abbr dki="docker images"
abbr dkpu="docker pull"
abbr dkph="docker push"
abbr dkbnc="docker build --no-cache -t"
abbr dkr="docker run --rm"
abbr dkrti="docker run --rm -ti"
abbr dkc="docker compose"
abbr dkcb="docker compose build"
abbr dkcu="docker compose up"
abbr dkclean="docker ps -q -a -f status=exited | xargs -r docker rm && docker images -q -f dangling=true | xargs -r docker rmi"
# run scripts.
abbr r="run"
abbr rc="run composer"
abbr rd="run drush"
abbr rdcr="run drush cr"
abbr rdup="run drush updb -y"
abbr rdce="run drush config:export -y"
abbr rdci="run drush config:import -y"
abbr rduli="run drush uli"
abbr -g A1="| awk '{print $1}'"
abbr -g Fj="| jq ."
abbr -g Fy="| yq ."
abbr -g G="| grep"
abbr -g GH="| grep HTTP"
abbr -g Gi="| grep -i"
abbr -g H2="| head -n 20"
abbr -g H="| head"
abbr -g L="| less"
abbr -g V="| vim -"
abbr -g X="| xargs -I1"
'';
} }