A beginners guide to terminal aliases and functions

By far the simplest time saving tools I’ve found since working more in the OSX terminal are aliases and functions.

Aliases stay true to the saying that lazy developers are efficient developers; they are simple shorthand commands that you can define to reference longer commands you find yourself commonly using.

So for example, if you use git on the command line you can find yourself typing out commands like git commit -m 'message', git status -s or git branch -r pretty often. Sure, they aren't huge commands to type out, but if you’re using them regularly it can get pretty repetitive.

Likewise, you may find yourself doing the same few commands in a row over and over again. Ever create a new directory in the terminal and then cd straight into it afterwards?

This is where aliases and functions can help you out.

By creating a shell script to include in your terminal profile, you can create any number of aliases and functions that you can then use in the Terminal as a replacement for the long-form commands. If you have no idea what a shell script or a terminal profile are, read on, we’ll get to that in a second…

Note. I’ll be taking you through the steps to create aliases and functions on Mac OSX. On Windows, the concept is similar, but file locations and specifics may differ.

Creating your aliases

First up, you’ll need to create a shell script that will hold the aliases and functions that you want to add.

Much in the way that HTML is a language that your browser can interpret, a shell script is simply a file that your terminal, or shell, can read. Shell script has it’s own syntax, much like any language; I won’t go into detail about the specifics of shell scripting, but there are plenty of good guides available on the web for anyone who wants to learn more about this.

Before you create your shell script, you’ll need to make sure that you have hidden files and folders showing in Finder. To do this, open up your terminal and enter the following command:

defaults write com.apple.finder AppleShowAllFiles -bool TRUE && killall Finder

This will make all files and folders visible in Finder and restart any currently running Finder windows that were open.

You’re now ready to create your shell script. We’ll be creating our aliases in the root of our User folder, so whatever your user name is on your computer, navigate to that folder in Finder. Alternatively, open up a terminal window and you should find that it positions you at your User folder by default.

Create a file and call it .aliases.sh – if you’re in terminal, type touch .aliases.sh.

Open the file up in your text editor of choice and add the following lines of code to it:

alias myip='curl ip.appspot.com'

function mkd() {
    mkdir -p "[email protected]" && cd "[email protected]"
}

You'll notice that the above code has one alias and one function.

An alias is usually used for simple shortening or chaining terminal commands together. A function can take values passed to it by the user and manipulate those values in some way to perform commands.

In the above code, we have defined an alias of myip which will output your public ip to the terminal by performing a curl request to ip.appspot.com. We have also defined a function mkd which when passed a value, such as mkd new-folder, will first make a new directory with the name new-folder and then cd (change directory) into the newly created directory.

As you add more aliases and functions, it can be a good idea to organise them into different shell scripts. For example, it would make sense to group all of your defined git aliases into one file – perhaps called .git-shortcuts.sh. This makes maintenance of your shell scripts easier, in the same way that separating out files when coding in any language helps structure and organisation.

Ok, so now we have created our shell script, we need to link this into our terminal profile so that these commands can be referenced in our terminal.

Editing your terminal profile

The final stage is to make a reference to your newly created shell script in your terminal profile – or bash profile.

In simple terms, the .bash_profile file is something that is executed when a new terminal window is opened and bash is invoked. If you’d like to read a more detailed explanation, check out this article on the subject.

As the .bash_profile file is executed when you open a new terminal window, we can add files to be run when this execution happens. Your .bash_profile is located at the root of your User folder, in the same place that you will have just created your shell script file.

Open up the .bash_profile file in a text editor, and add the following line to the bottom of the file:

source "/Users/my-username/.aliases.sh"  # General aliases and functions defined by me

Be sure to replace my-username with your OSX username – if you don't know what this is, press cmd + i on the .aliases.sh file in Finder and check the 'Where' value of the file. Once you have done this, save and close the file.

Test it works

Now that you've created your aliases and referenced your newly created script in your bash profile, you should be able to use them in your terminal.

Open up a new terminal window – make sure it's a new window, as previously open windows won't have run your newly created scripts – and type in myip.

Your terminal should output your public IP to the screen.

Similarly, writing mkd new-folder will create a folder and cd into the newly created folder.

Next steps to terminal domination

This is simply an introduction to the aliases and functions you can setup in your terminal. There are many other useful aliases and functions you can add to your setup to save you time when working in the terminal.

A full list of my system aliases are up on github as well as a separate set of functions. These have been built upon and edited from the excellent set of dot files by Mathias Bynens.

If you use git, or want to start using git, on the command line, aliases can help you out big-time. I recommend taking a look at the excellent git-convenience tools by Jake Archibald, or check out my git alias file which is an extended set of aliases built on top of what you'll find in the git convenience tools.

Finally, if you just want to have a look at the sort of things that are possible using aliases and functions, check out alias.sh, which has tonnes of useful snippets to copy and use in yur own script files.