Utilities for Managing Bash AliasesIntroductionThe bash shell has three features that facilitate speedy command entry (and thus help prevent repetitive stress syndrome and carpal tunnel medical problems):
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
source $HOME/.bash_alias
Since 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 gaglobal aliases. This invokes your editor on$HOME/.bash_alias
and when finished, sources it. Any alias you add will immediately be in
effect.
alalias local. This command is similar to 'ga' except that it manages aliases local to the current directory. It invokes your editor on a file named.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 '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. alcalias last command. This consults the history list for the last 9 commands and displays them. It then asks which of the commands you want to make an alias for and what alias to assign to it. For example:% 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.
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 galcThis is the same as alc except that the newly created alias is put in the global alias file$HOME/.bash_alias .
acdalias current directory. You are prompted for an alias to associate with the current directory. A shell variable with the same alias name is also set to the directory name. For example:% 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 adiraliased directories. This shows the aliases that have been created with acd. Only the last component of the directory is shown. Give the -l option if you want the full directory name.Installation
If you liked these time (and hand and wrist) saving tips also see: Perl and Vi - A Poor Man's IDE |