If you're a veteran of Linux, OSX, or have been using a Unix-like system for further back than you can remember, the likelihood of gaining anything useful from this tutorial video on aliases is slim.

However, if you are relatively new to Linux or OSX, chances are you may have heard of aliases, but never created your own.

Notice, I say never created your own - not, never used aliases - and that's because by default you get some aliases created for you for free.

Simply open up your terminal and type in alias and you will be presented with a list of default aliases available for you to use. It might look something like this:

alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

That's why, if you're ever in a directory and you type ll for example, you get a nice list of your files and folders, including hidden files (-a), in long listing format (-l), and they will have been classified (-F). You can see more find more information on the ls switches here, if you're so inclined.

Beyond The Defaults

Now, the default aliases are great. They come for free and are immediately usable.

But of course, anything that comes default has to be generic, and that means as useful as possible to as many people as possible.

In other words, you're never going to find prod set up for you out of the box, which would help you run the Symfony php app/console --env=prod command prepended to anything you type after it. An example might be prod cache:clear, which is much nicer, and shorter, than constantly having to type php app/console cache:clear --env=prod, or what have you.

However, we can create that for ourselves, and it really isn't very difficult at all.

First, jump across to your home directory:

cd ~

Once there, check if you already have an aliases file configured.

If you're using the bash shell (not exactly correct English, but I'm going with it for clarity), then you can open up your .bashrc file and see if you have a definition for loading custom alias files:

vi .bashrc

Hit shift + G to jump to the bottom of the file.

In there, by default (on Debian / Ubuntu machines at least), you should have a definition for loading a custom aliases file. It might look something like:

if [ -f ~/.bash_aliases ]; then 
    . ~/.bash_aliases
fi

If you are happy with that file, feel free to exit and we can move on. For the sake of our demo, we are going to create another file for our aliases simply called .aliases. After all, these aliases could also be used by OhMyZsh or other shell.

Below the fi line, we will add in a new section:

if [ -f ~/.bash_aliases ]; then 
    . ~/.bash_aliases
fi

# our aliases
if [ -f ~/.aliases ]; then 
    source ~/.aliases
fi

Save and quit (Esc, :wq).

Now, we need to know if our .aliases file already exists.

ll

If you see a file called .aliases then skip this step, otherwise:

touch .aliases

Cool, we now have our empty file ready for our custom aliases. Let's add some:

vi .aliases

We can add in as many as we like, and they all follow the format alias our_alias="some command here".

We will add in some Symfony ones:

alias dev="php app/console --env=dev"
alias prod="php app/console --env=prod"
alias devcc="dev cache:clear"

Notice, we are using an alias inside an alias for our third entry.

Save and quit (Esc, :wq).

Now, by default, simply creating these aliases won't put them into use. For that, you need to reload the shell's running configuration. To do that, we can run either:

cd ~
. .bashrc
# or 
source .bashrc

If you're not in your home directory be sure to prefix the path properly, eg:

. ~/.bashrc
# or 
source ~/.bashrc

Now, if we type alias again, we should see our three new entries.

Mistakes Happen

Removing an alias is very easy. Simply call unalias alias_name, eg: unalias dev.

Now, if you re-run the alias command, you will see the entry for dev has been removed.

Keeping In Sync

Of course, this is all well and good if you regularly use this machine. Or a small few machines where copying this .alias file around whenever you make a few changes isn't a total pain in the rear end.

But if you're anything like me, you will likely work on tens of machines frequently - your laptop, your desktop, myriad VMs, physical servers spread across the world... the list of boxes never seems to shrink.

One of my reluctances to starting using aliases was this very issue. Sure they are nice and all, but keeping them sync'd across all my infrastructure wasn't my idea of a fun time.

Instead, I recommend you switch to using Ansible to manage your infrastructure, and you can have your custom aliases deployed from anywhere to anywhere. What's great about this is you can keep your dotfiles (files beginning with a dot, e.g. .aliases) on your github page, and then use Ansible to pull down the latest changes whenever you run your Playbook. So cool.

Do you OhMyZsh ?

Another thing that put me off diving into aliases initially was that I simply couldn't fathom how to use them with OhMyZsh, which in hindsight is a bit chimpish, but hey ho. That's how we learn.

If you have folllowed along but use Zsh, instead of editing .bashrc, simply change .zshrc instead.

You can use the same aliases file - that's the main reason I didn't both using the .bash_aliases file - as I wasn't using bash.

Further Viewing

I would highly recommend watching Javier's SymfonyCon Madrid 2014 talk on Symfony Tips and Tricks. That's where I learned this tip, and many more besides. It's an awesome talk, you will definitely learn something.


Share This Episode

If you have found this video helpful, please consider sharing. I really appreciate it.


Episodes in this series

# Title Duration
1 php app/console quick:walk-through --no-frills 02:22
2 Quick Linux (and Mac) Alias Tutorial 04:41
3 How To Quickly Unfreeze An SSH Session 02:17