Sunday, December 2, 2007

ShortHowTo: Aliasing

This is a ShortHowTo on aliasing commands in gnulinux so lemme get to the point fast.

Every time We write some command on the terminal like poweroff,or mplayer or some other long command, We have to hit a lot of keys. And this really is time consuming. Although auto-completion of commands is really useful but I guess We have to be much more efficient. Well the solution to this problem is aliasing commands.

In gnulinux or other UNIX like environment you have a .bashrc shell script in your home directory. Every time you open a new terminal or new terminal tab this script executes and sets some environment variables for current terminal or terminal tab opened.

Lets alias a command say clear to just c.

1. open the .bashrc file using your favorite text editor.

$vim ~/.bashrc

2.Go to the end of the file and type following line.

alias c=clear

3.save the file.

Now to check if it really worked open a new tab into your termianl and spread some mess on the termianl screen say

$ls
$c


WoW it worked.

aliasing long commands is really very good but the real exploitation of alias is when you use it for most frequently typed commands by you.

Following are the commands which I have aliased. Tell me if also have some more aliases up your sleeve.

alias c=clear
alias l=ls
alias t=touch
alias d=cd
alias v=vim
alias mp=mplayer
alias off=poweroff

3 comments:

Kazim Zaidi said...

Some great aliases I would like to share

alias mplayer="mplayer -nojoystick -nolirc $*"
That is, don't try to detect joystick or IR remote

alias showpkg="apt-cache show $*"
To know what a particular package does, just try showpkg mplayer, for example

alias :q='exit'
For vi users that only know one way to exit, namely :q (ah yes, Ctrl+D is fast too)

alias findpkg='apt-cache pkgnames | grep $*'
Find the correct name of a package you wish to install. For example, findpkg php5 gives you all packages related to php5.


I've leaked many of my troubleshooting secrets, eh!

Waseem Ahmad said...

So here you you have some really *advanced* level aliases. See the power of aliasing...Your troubleshooting secrets will now become *fast* troubleshooting techniques to many now.

Kazim Zaidi said...

bash aliases are so much of a personal thing. It relates to your experince..

Did you know about $CDPATH?
Say you cd to the directory /path/to/directory/music a lot. Just add it to your $CDPATH

export CDPATH=.:~:/path/to/directory

Next time, you can just
cd music
to cd to /path/to/directory/music. Time saver, na?