Oct 312010
 

halloweenIn several commonly used shell commands can be shortened with convenient alias, a classic example is:
alias ll='ls -al'

Also in this field zsh has some interesting features, which enable you to define traditional aliases but also things a bit more particular.



Classic Alias

They are defined like in bash so alias something='your command here'

Example: Define an alias that show all files of current directory sorted by their size:

alias sz='ls -l | sort -n -k +5 | tail -10'

now you can just type sz to get as output the name of files in current directory sorted by size.

Global Aliases

zsh lets you use aliases even if they are not the first word on the command line. These kinds of aliases are called global aliases.
To use them the syntax is alias -g something='your command here' the -g stand for global.

Example
alias -g L="|less"

Now you can easily page through the output of another command by adding L to it
for example ps -ef L

Suffix Alias


Another kind of variant are suffix aliases. The goal is this kind of aliases is to define a program to be used with a typology of files.
To use them the syntax is alias -s suffix='your program' for example:

alias -s txt=vim
alias -s pdf=xpdf
alias -s html=w3m

This says that if we enter a file as if it were a command, it will be opened by Vim if it has the .txt extension, by xpdf if it has the .pdf extension and by w3m if it has the .html extension.

Disabling and Deleting aliases

You can delete an alias foo entirely by typing: unalias foo
You can temporarily disable an alias foo by running: disable -a foo
You can turn it back on with: enable -a foo

Finally, you may have an alias such as ftp that actually runs a different program, such as ncftp. If you’d like to use the actual ftp program, use =ftp or ftp on the command line.

References:

http://zsh.sourceforge.net/Intro/intro_8.html

http://www.acm.uiuc.edu/workshops/zsh/alias.html

Popular Posts:

Flattr this!

  2 Responses to “Alias in Zsh”

  1. […] of Linux to actually implement commands with names that have a clear meaning in plain English): Alias are a great tool to help increment your productivity on the terminal with bash (or any shell […]

  2. […] Alias ​​são uma grande ferramenta para ajudar a incrementar a sua produtividade no terminal bash (ou qualquer outro programa shell você está usando), mas geralmente estamos com preguiça de pensar em quais são os comandos mais comuns, ou não queremos perder tempo para preparar um apelido para eles. […]

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

*