[image of the Head of a GNU]

Utilities for Managing Bash Aliases

Introduction

The 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:

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:

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

global aliases. This invokes your editor on $HOME/.bash_alias and when finished, sources it. Any alias you add will immediately be in effect.

al

alias 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 .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

alias 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: The new alias 'gr' is put in the local alias file, .al, and then that file is sourced. Note that the list of commands displayed has been trimmed of duplicates and of commands that are so short (<= 3 chars) that they likely don't need an alias.

If you want to see more than the last 9 commands you can define an environment variable named NALIASES.

galc

This is the same as alc except that the newly created alias is put in the global alias file $HOME/.bash_alias.

acd

alias 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: The 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.

If we perform the above commands once we can then proceed very smoothly:

adir

aliased 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

  1. Download this tar file: bash_alias.tar and untar it into $HOME/bin. You should have these files:
  2. Check that /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.

  3. Move bash_alias to $HOME/.bash_alias.

  4. Edit $HOME/.bash_profile (or $HOME/.bashrc if you use that, instead) and add this line:
  5. Logout and log back in again so that your $HOME/.bash_alias file is re-sourced.
That's all, folks. Now all the utilities discussed above are at your service.

If you liked these time (and hand and wrist) saving tips also see: Perl and Vi - A Poor Man's IDE

jon@logicalpoetry.com