Bash
Be fast with Bash Aliases
by Matt on Jun.12, 2009, under Bash
How to use bash alias commands to your advantage
Using command line tools saves a developer an unbelievable amount of time. The question is how do you save more time and increase your efficiency even more?
One of my favorite ways is one I’ve used a long time, by using bash aliases. This post is directed to Mac OS X but may work with some linux distros as well, files are named differently.
How can this help you? Lets say everytime you open a shell window you have to cd into a generally deep project directory like:
/Users/mattsimpson/Clients/My\ Client/Web/Email\ Dispatch\ App/branches/0_8_0_1_ccd_migration
This can be tedious and annoying very easily, especially if you like to keep your folder names standard english format like “My Client” rather than cammal case “MyClient” or “my_client”, etc.
With Aliases we turn this:
[msimpson@dakota ~]$ cd /Users/mattsimpson/Clients/My\ Client/Web/Email\ Dispatch\ App/branches/0_8_0_1_ccd_migration
Into this
[msimpson@dakota ~]$ email_dispatch_app
which you can tab out after emai → [tab], that’s jimmy johns fast.
How do I get there?
First, Open a terminal window and edit your .bash_login file (.bashrc for linux) with your favorite text editor:
[msimpson@dakota ~]$ vi ~/.bash_login
And Insert the following near the end of the file:
# Alias definitions. # You may want to put all your additions into a separate file like # ~/.bash_aliases, instead of adding them here directly. # See /usr/share/doc/bash-doc/examples in the bash-doc package. if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi
Save and close.
Create a new file, let’s name it .bash_aliases in your home directory:
[msimpson@dakota ~]$ vi ~/.bash_aliases
and insert the following into the first line of the newly created file, but replace the alias name (email_dispatch_app) with your desired command alias name and the command path (/Users/mattsimpson/Clients/My Client/Web/Email Dispatch App/branches/0_8_0_1_ccd_migration) with your desired command path…
alias email_dispatch_app='cd /Users/mattsimpson/Clients/My\ Client/Web/Email\ Dispatch\ App/branches/0_8_0_1_ccd_migration'
Save and close.
Execute the following command to reset your bash session:
[msimpson@dakota ~]$ . ~/.bash_login
You are now able to execute your command (email_dispatch_app) and it’s tab completable! Go ahead and try it and see below for my list of aliases I have collected through the past couple years.
[msimpson@dakota ~]$ email_dispatch_app
Obviously you can see how this can speed up your development and allow you to focus on the things that matter, not on where you need to change directories, or that super long command that you always have to type in over and over.
Here is the list of aliases I have, feel free to contribute your aliases, I’d love to see how others are using aliases.
my .bash_aliases file:
# General Use Aliases
alias l=’ls -lh’
alias c=’clear’
alias cx=’chmod +x’
alias tree=”find . -print | sed -e ‘s;[^/]*/;|____;g;s;____|; |;g’”# Ruby / Ruby on Rails Aliases
alias cons=’./script/console’
alias gen=’./script/generate’
alias spec=’./script/spec’
alias srv=’./script/server -u’
function import_email { email_dispatch && ./script/runner “IncomingEmailHandler.direct_mail(STDIN.read, ‘$1′)”; }# Project Directory Shortcuts
# …
# a bunch of directory shortcuts
alias email_dispatch_app=’cd /Users/mattsimpson/Clients/My\ Client/Web/Email\ Dispatch\ App/branches/0_8_0_1_ccd_migration’
# …# SSH Shortcuts
# …
# a bunch of ssh commands so i don’t have to remember users and ip addresses like:
alias sshenvion=’ssh msimpson@xx.xx.xx.xxx’
# …# Services
alias mysqlservice=’sudo /Library/StartupItems/MySQLCOM/MySQLCOM’
alias recover_scanner=”cd /var/www/projects/cdops2/current/ && sudo ./script/runner -e production “IncomingEmailHandler.recovery_scanner”"
function mksvn { ssh msimpson@trogdor setupsvn.sh $1; }
# instead of
# $ svn checkout -m ‘some svn message’
# you do
# $ svc some svn message
function svc { svn commit -m “‘$*’”; }
function checkout { svn checkout svn+ssh://trogdor/var/svnroot/$1; }
My favorite is by far the srv and the svc commands. Enjoy