Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zsh alias expansion

Tags:

zsh

zshrc

Is it possible to configure zsh to expand global aliases during tab completion? For example, I have the common aliases:

alias -g '...'='../..'
alias -g '....'='../../..'

but when I type, for example, cd .../some<tab> it won't expand to cd .../something or cd ../../something. Consequently, I frequently won't use these handy aliases because they are incompatible with tab completion.

like image 785
Bryan Ward Avatar asked Jan 19 '11 17:01

Bryan Ward


2 Answers

I'm a user of Mikael Magnusson's rationalise-dot. From my zshrc:

# This was written entirely by Mikael Magnusson (Mikachu)
# Basically type '...' to get '../..' with successive .'s adding /..
function rationalise-dot {
    local MATCH # keep the regex match from leaking to the environment
    if [[ $LBUFFER =~ '(^|/| |      |'$'\n''|\||;|&)\.\.$' ]]; then
      LBUFFER+=/
      zle self-insert
      zle self-insert
    else
      zle self-insert
    fi
}
zle -N rationalise-dot
bindkey . rationalise-dot
# without this, typing a . aborts incremental history search
bindkey -M isearch . self-insert
like image 55
fow Avatar answered Sep 20 '22 00:09

fow


Try looking up zsh abbreviations. They allow you to enter an "abbreviation" which automatically gets replaced with its full form when you hit a magic key such as space. So you can create one which changes ...<SPACE> to ../...

For example, this is what you need in your profile:

typeset -A abbrevs
abbrevs=(
        "..." "../.."
        "...." "../../.."        
)

#create aliases for the abbrevs too
for abbr in ${(k)abbrevs}; do
   alias -g $abbr="${abbrevs[$abbr]}"
done

my-expand-abbrev() {
    local MATCH
    LBUFFER=${LBUFFER%%(#m)[_a-zA-Z0-9]#}
    LBUFFER+=${abbrevs[$MATCH]:-$MATCH}
    zle self-insert
}

bindkey " " my-expand-abbrev 
like image 39
dogbane Avatar answered Sep 19 '22 00:09

dogbane