How to set up aliases for the commands you often use

How to save keystrokes at the command line with alias

Save some hassle by setting up aliases for the commands you often use.

How to save keystrokes at the command line with alias
Image by : 

opensource.com

x

Get the newsletter

Join the 85,000 open source advocates who receive our giveaway alerts and article roundups.

The alias command-line tool is both useful and relatively simple. Its purpose is to simplify a single-line command by creating a custom name for it. There is a good chance that you already have some aliases even if you've never used the tool. In Bash, aliases can be created with a simple text editor and are stored in your $HOME/.bashrc file. If you want to see what aliases have been set up, take look at that file, or simply type alias on a command line and press Enter/Return.

Here are some simple aliases, several of which were already in the file when I installed Fedora:

alias cp='cp -v'
alias rm='rm -v'
alias mv='mv -v'
alias ls='ls --color'
alias ll='ls -l --color'
alias lh='ll -h'

The first three aliases above ensure that whenever I use cp, rm, or mv I get verbose output. The others slightly modify the ls command's output.

All six also show you how to write an alias: First is the alias command, followed by the name you give the alias, then the equal sign, and finally the complete command between single quotes. You can use an existing alias name or create a unique one.

These are obviously simple, but let's look at some more complex aliases I have set up.

Opening Emacs files

alias emacs='emacs %f --no-splash --maximize -fg "white" -bg "dark slate gray" -cr "yellow"'

I use Emacs quite a bit, and the command above ensures its size and appearance are the way I like them. Notice the %f, which is a placeholder for the filename I type after the emacs command. If I want to create a new file in Emacs, I just type a filename that doesn't exist.

Using a temperature-conversion script

alias temp='/home/gregp/perl/tempconv.pl'

Here is the alias I created to use a temperature-conversion Perl script I wrote about last year. It's not shown, but you have to follow temp with a number for the script to work.

Keeping track of people

alias census2file='pg_dump -vt census gregp > census.out'

alias fromfile='psql -e gregp < census.out'

When I was still practicing medicine, I used to keep track of my hospital patient list and individual visits with PostgreSQL, so I could slice and dice the information as I wanted. I used both a laptop and desktop and to keep these up to date on both systems, I made the two aliases above.

With the first alias, I could dump the contents of the table "census" in one system, then use the second alias to import it with psql on the other system. I also used these whenever I upgraded Fedora to a newer version.

Running multiple commands together

Sometimes you may want to use an alias to run more than one command. You can do this with the alias tool, just as you can on the command line.

For example, I downloaded and compiled a command-line tool, weather, that worked well enough, although it took a while for me to find the precise name for the weather station I wanted, and it also only worked from inside its directory.

alias weather='cd /home/gregp/Downloads/weather-2.3;./weather "Louisville East CCD";cd /home/gregp'

In the example above, three commands are separated by semicolons (just like they would be on the command line). The alias takes me to the correct directory, runs the weather command, then goes back to /home.

alias forecast='cd /home/gregp/Downloads/weather-2.3;./weather -f -n "Louisville East CCD";cd /home/gregp'

Getting a forecast requires some switches to be set, and this forecast alias keeps me from having to repeatedly look them up.

Updating software

Finally, here's the most complicated alias I've created so far.

alias cmake15x='cmake . -DCMAKE_INSTALL_PREFIX:PATH=/home/gregp/scribus15x -DWANT_DEBUG=1 -DWANT_HUNSPELL=1 -DWANT_GRAPHICSMAGICK=1'

I wrote this because I am constantly keeping up to date with the latest versions of and compiling Scribus. It's easy enough to remember svn up to update Scribus from the repository, but then I have to run cmake with a number of settings. With this script, I can just type cmake15x, which is so much easier than trying to remember all the steps.

The alias command-line tool saves me a lot of time and keystrokes, especially for frequently used or complex commands. I would, however, recommend keeping some notes about how to use the more complex commands, especially in cases when more than just the command is required.

About the author

Greg Pittman - Greg is a retired neurologist in Louisville, Kentucky, with a long-standing interest in computers and programming, beginning with Fortran IV in the 1960s. When Linux and open source software came along, it kindled a commitment to learning more, and eventually contributing. He is a member of the Scribus Team.