You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wanted to make some of my zsh aliases behave like vim abbreviations: Namely, as soon as you press space, the alias is expanded and you see the expanded command before executing it. Since I have a large number of clunky aliases, I didn't want all aliases to be expanded by default.
Consequently, I came up with the following (based on something I first saw here).
typeset -a ealiases
ealiases=()
functionpermealias ()
{
alias"$*";echo ealias "$*">>~/.oh-my-zsh/custom/zsh-ealiases.zsh
}
functionealias()
{
alias$1
ealiases+=(${1%%\=*})
}
functionexpand-ealias()
{
if [[ $LBUFFER=~"(^|[;|&])\s*(${(j:|:)ealiases})\$" ]];then
zle _expand_alias
zle expand-word
fi
zle magic-space
}
zle -N expand-ealias
bindkey -M viins '' expand-ealias
bindkey -M emacs '' expand-ealias
bindkey -M viins '^ ' magic-space # control-space to bypass completion
bindkey -M emacs '^ ' magic-space
bindkey -M isearch '' magic-space # normal space during searches
To define an expandable alias use ealias. The syntax of ealias is the same as that of alias For example, I use
and paste the above aliases or create your own
To add an ealias from the command line you can use the permealias function:
$ permealias "dslogs='docker service logs -f --tail=100'"
The quotes order is important when entering an expandable alias because with more than one word because of the shell different evaluation of single quote and double quotes. Further reading
Now typing gc in a position where zsh expands a command will appear like you typed git commit.
Installation
Your preferred zsh plugin manager, or just clone into ${HOME}/.oh-my-zsh/plugins/ and then add it as the last plugin in the list of oh-my-zsh plugins