ZSH Gem #19: Global aliases
I guess each shell implementation knows something like aliases. With aliases you can give commands different names which might be more convenient for you. That's nothing new, but ZSH has a feature called global aliases.
An example of a normal alias would be to create a shorter name for the rename
command:
alias rn="rename"
Now you are able to call the rename
command via the short name rn
. You can also redefine commands with frequently used options:
alias ls --color=auto
But that's nothing you couldn't do in Bash as well. But let's come to the so called global aliases. To define a global alias you need to option -g
of the alias command. Global aliases are not limited to command names and can be used as a replacement of (nearly) any part of the syntax. For example if you often pipe things to grep and you don't always want to write | grep -i
you can define an alias for that:
alias -g gp="| grep -i"
From now on you only have to write gp
instead of | grep -i
. For example:
echo 'Foo bar' gp foo
and you get all lines containing foo.
As I said before: with global aliases you can give nearly everything on the shell an alias name. Even globbing patterns:
alias -g asterisk="*"
ls asterisk
That's particularly useful if you have a very complex globbing pattern that you need more than once. Just define an alias name for that. To demonstrate that even better, let's use our monster globbing pattern from the extended globbing article and define an alias name for it:
alias -g monsterglob="**/([^A-Z[:digit:]])##(#q.x^X^u1002Lm+30mM-1)"
ls -l monsterglob
I guess you see how extremely convenient and powerful this is and any why you should always use the -U
parameter when auto loading functions. You can even define aliases for variables:
alias -g p='$PATH'
echo p
Okay, that doesn't look very expedient, but imagine what you could do when combining that with other expressions such as parameter expansion:
alias -g nlpath='${PATH//:/
}'
That defines an alias which expands to a newline-separated list of all entries in $PATH
. In Bash you could only define such an alias together with a command. Such a command would look like this:
alias nlpath='echo -e ${PATH//:/"\n"}'
The advantage of the ZSH alias is obvious: you can use the alias anywhere where you could use a variable. The Bash alias, however, always needs to be piped to STDIN of another command in order to do further processing because it is an alias for a command, not an alias for some arbitrary part of the syntax.
Besides extended globbing and all the other completion and expansion features, global aliases are the reason why I love ZSH and I hope, I could enthuse you as well. If not, then hopefully with one of the following articles before this series is over. :-)
Read more about global aliases:
Trackbacks
RT @Agarri_FR: Using zsh? Not aware of global aliases? That's a pity! https://t.co/8bO1audHqh
RT @Agarri_FR: Using zsh? Not aware of global aliases? That's a pity! https://t.co/8bO1audHqh