Note that Ctrl-Alt-E in bash
does not only expand aliases. It also expands variables, command substitution (!), process substitution (!), arithmetic expand and removes quotes (it doesn't do filename generation (globbing) or tilde expansion).
It doesn't always manage to expand aliases. So while it has its uses, it's important to realise its outcome potentially changes the meaning of the command line, has side effects and is potentially dangerous.
For instance in:
$ a=';w' b=1
$ alias foo=bar
$ b=2; echo $b $a; cd /tmp/dir && for i do foo $(pwd) <(ls); done
If I press M-C-E
here, that gives me:
$ b=2; echo 1 ;w; cd /tmp/dir && for i do foo / /dev/fd/63; done
Which gives me a completely different command line altogether (and imagine what would have happened if I had had rm -rf *
instead of pwd
above) and doesn't expand the foo
alias.
With zsh
, to build up on Gilles' note on aliases expanded inside functions, you could do:
expand-aliases() {
unset 'functions[_expand-aliases]'
functions[_expand-aliases]=$BUFFER
(($+functions[_expand-aliases])) &&
BUFFER=${functions[_expand-aliases]#$'\t'} &&
CURSOR=$#BUFFER
}
zle -N expand-aliases
bindkey '\e^E' expand-aliases
That will expand the aliases only if the current command line is syntactically valid (so it doubles as a syntax checker).
Contrary to bash
's M-C-E, it also resolves the aliases fully. For instance if you have:
$ alias ll='ls -l'; alias ls='ls --color'
$ ll
Will be expanded to:
$ ls --color -l
Note that it also canonicalises the syntax so things like:
$ for i (*) cmd $i; foo
will be changed to:
$ for i in *
do
cmd $i
done
foo
C-x a
expands the alias under the cursor (assuming you're using the completion system). – Stéphane Chazelas Aug 17 '14 at 15:21_expand_alias (^Xa): expands the word the cursor is on if it is an alias
. That is helpful, however still its a pity that in bash one can expand whole line but in zsh not. – WeSenseASoulInSearchOfAnswers Aug 17 '14 at 15:54_expand_alias
until the edit buffer no longer changes. – vinc17 Aug 17 '14 at 16:39