The alias feature makes it easy to enter frequently used lengthy commands. For example:
% alias gr="grep '^ABC' data*.txt | cut -d^A -f2,4,6,7 | less" % gr % gr
To make an alias definition permanent (for the next time you log in)
you need to put it in a file and source that file.
One way of doing this is to put all alias definitions in $HOME/.bash_alias
and put the following line in $HOME/.bash_profile
:
source $HOME/.bash_alias
Since $HOME/.bash_profile
is executed by all login shells,
any alias definitions you put in $HOME/.bash_alias
will
be in effect when you first log in.
This is all well and good but ... the time and forethought required to create these definitions is often a significant barrier that is difficult to overcome. Even though the time it would take to create an alias would save much time in the long run (and much stress on hands and wrists and arms) the task at hand always seems to take priority. And so one rarely does.
This collection of bash alias utilities helps overcome the clerical burden of creating aliases.
The installation of these utilities is detailed below but first let's take a look at what the utilities provide.
There are these commands:
% ga % al % alc % galc % acd % adir
$HOME/.bash_alias
and when finished, sources it. Any alias you add will immediately be in
effect.
.al
and when finished, sources it.
With proper use of local aliases, you can have 'e' mean to edit one thing
here and another thing there. There is less of a need to
invent unique alias names. The name .al
is sort of cryptic
but it does serve to keep the file hidden from view.
'ga' and 'al' are sometimes used just to see what aliases you have defined. The global and local alias files are often easier to look at than the output of the bash command 'alias'.
The editor used for 'ga' and 'al' is by default vi. If you have defined $FCEDIT or $EDITOR it will be used instead.
% alc 1 vi pookie.pl 2 grep '^ABC' data*.txt | cut -d^A -f2,4,6,7 | less 3 cd ~/src/lib 4 ls -l 5 vi bigg.pl 6 vi big.pl 7 perl big.pl 8 ls -lt *.pl 9 perldoc -f split Which command and what alias? 2 gr %The new alias 'gr' is put in the local alias file,
.al
,
and then that file is sourced.
Which command and what alias? 2 gr 7 x 1 vp
If you want to see more than the last 9 commands you can define an environment variable named NALIASES.
% export NALIASES=20 % alc 1 vi little.pl 2 awacs ls -stream -f strict | less 3 cd ~/src/lib 4 ls -l ... 19 vi the_file.txt 20 vi vif.pl Which command and what alias? 19 t
$HOME/.bash_alias
.
% cd /net/data/properties/shopping/source/daily % acd alias for /net/data/properties/shopping/source/daily? nds % cd /home/joe/data/daily/shopping % acd alias for /home/joe/data/daily/shopping? jds % nds /net/data/properties/shopping/source/daily % cp out.txt $jdsThe last two commands move to
/net/data/properties/shopping/source/daily
and then copy the file
out.txt
to /home/joe/data/daily/shopping
. Note that these
'directory' aliases are global - they live in $HOME/.bash_alias
.
Also note that a 'pwd' is issued after moving to the new directory
to visually confirm where you ended up.
AND, most importantly, on moving to the new directory the local alias file is sourced for you. To see the utility of this feature let's say that you have two current projects that live in two different directories. In each, you edit and run a Perl script. With proper use of acd and alc, command entry becomes easy to remember, simple, and quick.
% cd /home/charlie/dev/properties/mail/sent % acd alias for /home/charlie/dev/properties/mail/sent? sn % vi tally.pl % perl tally.pl % alc 1 vi tally.pl 2 perl tally.pl Which command and what alias? 1 e 2 x % cd /home/y/share/htdocs/gui % acd alias for /home/y/share/htdocs/gui? gu % vi show.pl % perl show.pl % alc 1 vi show.pl 2 perl show.pl Which command and what alias? 1 e 2 xIf we perform the above commands once we can then proceed very smoothly:
% sn ( move to the mail/sent directory ) % e ( edit tally.pl ) % x ( run tally.pl ) % e % x % gu ( move to the htdocs/gui directory ) % e ( edit show.pl ) % x ( run show.pl ) % e % x
acd adir alc bash_alias
/usr/bin/perl
is present.
If not, you will need to change the first line of acd, adir
and alc
to
where Perl is located on your system.
bash_alias
to $HOME/.bash_alias
.
$HOME/.bash_profile
(or $HOME/.bashrc
if you use that, instead)
and add this line:
source .bash_alias
$HOME/.bash_alias
file is re-sourced.
If you liked these time (and hand and wrist) saving tips also see: Perl and Vi - A Poor Man's IDE