[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):

  • history
  • filename completion
  • aliases

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

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:
    % 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 do not give a command number it will default to the last command.
  • If you give only a command number you will be prompted for the alias.
  • You can give multiple pairs of command number and alias like so:
    Which command and what alias?  2 gr  7 x  1 vp
    
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.

    % 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
    

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:
    % 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 $jds
    
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.

    % 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 x
    
If 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
    

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:
      acd
      adir
      alc
      bash_alias
      
  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:
      source .bash_alias
      
  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