A blog probably of interest only to nerds by John Morton.

21Mar2018

Using alias­es on the com­mand line.

If you use the com­mand line and you’re lazy, this is for you. I’ve picked up a lot of tips in cus­tomz­ing my com­mand line over the course of many years. Most of them I aban­don after the new” wears off, but a few stick around. Specif­i­cal­ly, I have some alias­es that I find use­ful enough that they’re part of the set up process when I get a new Mac. (Yes, these are Mac spe­cif­ic but you can adapt to your system.)

I keep my alias­es in a file named .aliases at the root of my home direc­to­ry. That’s not some mag­ic” name though. It’s just named in a way that makes sense to me. It doesn’t auto­mat­i­cal­ly get added to my com­mand line envi­ron­ment. To make that hap­pen, you need to add a line to one of your files that is auto­mat­i­cal­ly called when you open a new ter­mi­nal window. 

Make sure you are in your home direc­to­ry and then make the .alias­es file.

cd ~
touch .aliases

You should now have an emp­ty file called .aliases there. You can edit it with a text edi­tor like Sub­lime Text, Atom, or VSCode or if you want to do every­thing from the com­mand line, Nano is an easy way to do that.

Next we’ll make sure it gets auto­mat­i­cal­ly invoked when a new ter­mi­mal shell is opened. Take a look at the Bash doc­u­men­ta­tion to see what order these auto­mat­i­cal­ly invoked files are loaded in.

When Bash is invoked as an inter­ac­tive login shell… it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and exe­cutes com­mands from the first one that exists and is readable.

In my case, I use the .bash_profile to include the alias­es. To do this you need to use the source com­mand like this.

# this line is added to the .bash_profile file
source ~/.aliases

That’s it. The next time you open a new ter­mi­nal win­dow, your .aliases file will be auto­mat­i­cal­ly invoked, adding the alias­es we’re about to set up to your list of avail­able commands.

Rel­a­tive direc­to­ry short cuts

One of the most basic things you do on the com­mand line is move between direc­to­ries. Mov­ing back­wards up the chain of direc­to­ries can be made eas­i­er by mak­ing use of the . char­ac­ter. Add this to your .aliases file and save it.

alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias .....="cd ../../../.."

Make this easier

When I first wrote this, at this point in the post I said to open a new ter­mi­nal. That’s far too much work. We’re try­ing to be lazy here.

You can reload your ter­mi­nal with a sim­ple com­mand exec $SHELL -l. It’s much eas­i­er to remem­ber a word that describes what you’re try­ing to do when you reload the ter­mi­nal win­dow though. I keep the fol­low­ing at the very bot­tom of my .aliases file to make this easier.

# Reload the shell
alias reload="exec $SHELL -l"

After you add this to your file, you can just type reload into your ter­mi­nal and any­thing you’ve added to your ali­aes will be avail­able imme­di­ate­ly. Since you just added this though, you will either need to open a new ter­mi­nal win­dow or use the long ver­sion of this com­mand one last time, exec $SHELL -l.

Spe­cif­ic direc­to­ry shortcuts

There are a few spots in my com­put­er where I keep most of my stuff. I’ve set up sin­gle char­ac­ter alias­es to make get­ting to these quick and easy. Obvi­ous­ly you’ll update these to match where you spend your time on your system.

alias d="cd ~/Dropbox/Clients"
alias g="cd ~/git"
alias v="cd ~/git/laravelvalet"

Once you’ve got your per­son­al­ized short­cuts in place, just reload your ter­mi­nal session.

List­ing direc­to­ry contents

I men­tioned ear­li­er that this is Mac spe­cif­ic. The -G flag we’ll use here is the way you get a list of files with col­or on a Mac. Oth­er sys­tems may need a dif­fer­ent option. Even with­out the col­or option though, these will end up sav­ing time. 

# List files colorized in long format
alias l="ls -lF -G"

# List files colorized in long format, including ".invisible" files
alias la="ls -laF -G"

After you reload your ter­mi­nal, just type l or la and see all the files in your directory. 

This will make you so much bet­ter at being lazy. We’re get­ting good at this!

There are many posts online on how to ful­ly cus­tomize ter­mi­nal life. This is just the begin­ning of what you can do there.