This post is a follow up to the 6 Mac OS X Terminal Tips post I wrote a while back, and is proof that my forays into the scary world of the command line have continued. In it I’m going to explain how you can customise your terminal (shell for unix and linux users) by creating your own aliases for regularly used commands, creating your own functions and even customising the display.

The key to all this is a small file, that lives in your home directory, called .bash_profile.

Don’t worry about the . it just means it is a hidden file i.e. it won’t show up by default in finder. Adding text to this file can configure the command line environment in many many ways, from small tweaks to powerful new functionality. It’s probably best if I keep to the tweaks end of the spectrum for now.

First things first, check to see if the file exists. It is a hidden file (prefixed with .) so you’ll need to open the terminal, make sure you are in the home directory (type pwd to check and/or cd ~ to change into it) and then, finally, type ls -l to list all the files and directories.

If it’s there, excellent, if not, don’t worry we’ll just create it. Create it by typing touch .bash_profile. This creates a blank file with the name .bash_profile.

Alias

The first customisation we’ll look at is aliases. An alias is an alternative name, created by you, for a given command. They are used to create shortcuts for commands that get used regularly, especially if, like me, you always forget what flags and args you need to include.

To create and add an alias open up your .bash_profile (it makes sense to use a command line editor like nano or vi) and add lines similar to these.

# shorten list command to single l
alias l='ls -la'
# shorten history command to h
alias h='history'
# create editBash alias to open this file in vi editor
alias editBash='vi ~/.bash_profile'

As you can see an alias declaration takes the format alias <your chosen alias>='<command>'.

Add colour to your terminal

We all know the value of making things easier to parse visually and the terminal is no different. One of the easiest things to customise is the, frequently used,ls output. To do so you need to add 2 lines:

# enable colours in the terminal environment
export CLICOLOR=1
# set the colours for ls output
export LSCOLORS=gxfxbEaEBxxEhEhBaDaCaD

At the moment I’m not 100% sure what the format of the ls colours are but you can read more about customising the colours used by ls there.

Basic Functions

Next up we have functions. Functions allow you to create new commands to use in the terminal. I haven’t delved too deep yet but I have set up a few simple ones that have sped up my workflow by allowing me to open files directly into applications I use, i.e. opening files into my editor of choice or opening a file straight into a browser to test how it looks.

# Open files in Chrome browser
chrome() {
    open -a "Google Chrome" "$1"
}
# Open files or directory in TextMate
mate() {
    open -a /Applications/TextMate.app/ "$1"
}

If you want to learn more about function syntax I’d recommend this tutorial which includes 6 useful function examples

More Complicated Functions

In the above examples I created very simple functions that contained only single commands. The example below shows a more complicated function that executes a group of commands.

# create basic project (tsapp)
projectinit() {
    # create a new assets directory
    mkdir assets
    # download basic files needed for new web project
    wget https://raw.github.com/colmjude/Projectus/master/_js/script.js -O assets/script.js
    wget https://raw.github.com/colmjude/Projectus/master/_naked.html -O index.html
    # initiate compass CSS preprocessor
    compass create
}

Bonus geeky win

This function should get you a few geek points, it allows to make Google searches directly from your terminal. Who doesn’t want to do that?!
Once you add this snippet you’ll be able to enter @google followed by whatever search term you want.

# Google it!
function @google {
    open "https://google.com/search?q=$*"
}

Bringing it all together

Once you’re content with the awesome customisations and enhancements you’ve made to the terminal you need to make sure the file is reloaded. This is how the new changes take effect. For a long time I thought the only way to do this was by closing the terminal and reopening it, not ideal. However I was worng, there is a better way. Just run the following command:

source .bash_profile

Now that’s done I’ll leave you to it. I hope you enjoyed the post and found some or all of it useful. If you have any comments, questions or pointers feel free to leave a comment.

Useful resources

Leave a comment

Your email address will not be published. Required fields are marked *

*