Make Shell Aliases Easy

if you run a command with at least one argument twice a day, then you should make an alias for it..

Mike Hostetler

This blog started as part of a discussion about shell usage and putting aliases in. This is of interest to me because, before I went into full-time development, I did a lot of system administration in Unix and Linux. I spent most of my days in the shell and in a Unix editor. And I still get out of the IDE and into a shell.

Once upon a time, I remember reading somewhere that, if you run a command with at least one argument twice a day, then you should make an alias for it. Their reasoning made sense - the problem was that it was painful to do. They gave an example of an uber-alias — something in your shell init file ta helps you create them. What they went something like this:

alias realias="$EDITOR $HOME/.aliases; source $HOME/.aliases"

Then you don’t store your aliases in your .profile but in it’s own file. For me that file is ~/.aliases for commands and ~/.diraliases for directory aliases (which is a zsh thing. If you haven’t checked out zsh you really should).

[ -f $HOME/.aliases ];  source $HOME/.aliases
[ -f $HOME/.diraliases ]; source $HOME/.diraliases

I read this article over 10 years ago and it’s still a setup that I use to today. I find a command that I suddenly use a lot of, I can quickly make an alias for it and use it immediately.

Here is a few of the aliases that have come so ingrained into my workflow that I really can’t live without:

alias l="ls -l"
alias unzip="unzip -q"
alias edit=$EDITOR
alias dl='dirs -v'
alias c=clear
alias ack="nocorrect ack"
alias redir="$EDITOR ~/.diraliases; source $HOME/.diraliases"

### git stuff
alias gss='git status --short'
alias gfl='git reflog'
alias gl='git smart-log'
alias gm='git smart-merge'
alias gup='git smart-pull'

###### grails stuff
alias gclean="grails clean"
alias gapp="grails clean && grails run-app"
alias gtest="grails clean && grails test-app"
alias gwar="grails clean && grails war"

The git smart-something aliases are from the git-smart project which could be the subject of another blog post, but let’s just say I find it very handy.

Sometimes you have a series of commands that you run over and over again. You could put it in a shell script in ~/bin but (if you are like me) that directory gets rather full. For me, I’d rather put them in function inside my ~/.aliases. Here is an example:

function grepJars() {

	 for x in `find . -name "*.jar"`; do
             echo $x
            unzip -l $x|grep $1
    done
}

So save yourself some keystrokes and embrace the power of aliases in your shell.

Share this Post

Related Blog Posts

Unknown

Goodbye Google Code

March 24th, 2015

Google Code is shutting down  (because everyone uses github anyway) So long and thanks for all the fish.  It has been a good ride — thank you for pushing open collaboration forward! Its been a good transition to git. If you haven’t yet, consider…

Object Partners
Unknown

Travis Build Matrix Badge

February 17th, 2015

Currently, Travis doesn’t have a way to display a build status badge for individual jobs in a matrix build. You can only get an individual badge which displays “passing” if all the jobs pass or “failed” if one job fails. I’ve created a small open…

Brandon Fish
Unknown

Setting up your own Apache Kafka cluster with Vagrant - Step by Step

May 6th, 2014

This step-by-step walk-through will guide you through building an Apache Kafka cluster from the ground up, with vanilla Debian as a base on Vagrant boxes.

Object Partners

About the author

Mike Hostetler

Sr. Consultant

Mike has almost 20 years of experience in technology. He started in networking and Unix administration, and grew into technical support and QA testing. But he has always done some development on the side and decided a few years ago to pursue it full-time. His history of working with users gives Mike a unique perspective on writing software.