ZSH Gem #19: Global aliases

Posted by | Comments (3) | Trackbacks (3)

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

0xINT3 sent a Trackback on : (permalink)

RT @Agarri_FR: Using zsh? Not aware of global aliases? That's a pity! https://t.co/8bO1audHqh

Comments

There have been 3 comments submitted yet. Add one as well!
Rob
Rob wrote on : (permalink)
Hi. Is there any way to alias event designators? I'd like to do % alias -g lst=!#:1:h so I could use % cp /usr/very/long/path/file lst/renamed
Janek Bevendorff
Janek Bevendorff wrote on : (permalink)
Hi, I tried several variants, including single-quoted strings, eval and sub shells, but couldn't find a way to make it work the way you want. I guess there is no way since event designators seem to work only on the command line at first level, not in sub commands.
alivefromupstairs
alivefromupstairs wrote on : (permalink)
Global aliases do not tab complete for me. Is this the case for everyone or just on my end?

Write a comment:

E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

By submitting a comment, you agree to our privacy policy.

Design and Code Copyright © 2010-2024 Janek Bevendorff Content on this site is published under the terms of the GNU Free Documentation License (GFDL). You may redistribute content only in compliance with these terms.