Alias Everything

Type less, do more

Posted by Jon on January 18, 2015

This post is part of an ongoing series based on a talk I gave called Command and Conquer for the Dayton Web Developers group.

Alias everything

As developers, we’re very concerned with productivity, and aliases are great for that. Aliases are simply shortcuts that we can define for commonly used commands.

Take me to your dir

Let’s say I have a directory that I find myself navigating to frequently:

alias pro='cd ~/Documents/projects'

Now I can run pro to automatically navigate to my “projects” directory from any location. Cool!

Gimme the details

We can also use aliases to set default flags and save ourselves a lot of typing. For example, we can use ls to list the contents of a directory, but I’m not a big fan of the default view, so I use this instead:

alias l='ls -lAh'

Now if I simply run l, I get a long listing of the contents of my current directory, including hidden files, in a more readable format.

Letter to the editor

As developers, we spend a lot of time editing files (when we’re not arguing about the best editor, that is). We can use an alias for that as well:

alias edit='$EDITOR'

You’ll notice something special here. $EDITOR refers to a special environment variable that tells bash which editor to use as the default. Here are a few examples of how to set it:

export EDITOR='vim'
export EDITOR='emacs'
export EDITOR='atom'

Now I can run edit to open my preferred editor inside my working directory, or edit file_name.txt to open a specific file.

Jogging your memory

Aliases are great for productivity, but sometimes we may forget what the alias actually does behind the scenes. This is no good. We want our aliases to be conveniences, not abstractions. Luckily, we can use an alias to help us with this as well:

alias ag='alias | grep'

This combines the power of the vertical pipe with the searching power of grep. Now we can run something like ag git to give us a nice filtered list of all of our aliases that use git. But that’s not all…

Send out the search party

Although grep is most commonly used to search for patterns in files, using it to search against the output of different commands is extremely useful as well. Let’s take a look at a few other aliases to leverage this technique.

Want to search through a list of running processes?

alias psg='ps -ef | grep'

How about searching your command history for the syntax of that command you ran months ago?

alias hg='history | grep'

And that’s just the beginning. Basically, if a command has output, you can grep it!

Gittin’ stuff done

As good, decent, responsible developers, we’re always sure to use version control for our projects. I’m partial to git, and my git aliases are a crucial part of my workflow. Here’s a sample:

# initialize a repo
alias gi='git init'

# view the status
alias gst='git status'

# stage changes
alias ga='git add'

# unstage changes
alias grh='git reset HEAD'

# view unstaged changes
alias gd='git diff'

# view staged changes
alias gdc='git diff --cached'

# commit changes
alias gc='git commit'

# commit changes, with message
alias gcm='git commit -m'

# push to a remote repo
alias gp='git push'

# fetch changes from a remote repo
alias gf='git fetch'

# merge changes
alias gm='git merge'

# check out a branch
alias gco='git checkout'

# list branches
alias gb='git branch'

# view list of commits (and make it look good!)
glog='git log --oneline --decorate --color --graph'

I find that since I’ve committed these aliases to muscle memory, not only am I much faster, but it has the added benefit of keeping me in my state of flow, allowing me to focus on the task at hand instead of fumbling around on the keyboard. Wins all around!

Even more goodness

If you’re not already using aliases as part of your every day workflow, hopefully this has compelled you to start. For even more, have a look at my alias file over on GitHub.

And remember, this is just a starting point. Feel free to use any of the aliases here (most of them are “borrowed” anyway), but more importantly, create your own! Make your aliases work for you.

Ok, that’s all for now. I’ll leave you with a few handy aliases for displaying hidden files in the mac finder.

# show hidden files in finder
alias sf="defaults write AppleShowAllFiles TRUE; killall Finder"

# hide hidden files in finder
alias hf="defaults write AppleShowAllFiles FALSE; killall Finder"

Thanks for reading!