Intro to Terminal Aliases

Terminal aliases are just shortcuts for the terminal so you can type less. For example, if you use git on the command line you probably type git status a lot. Well, with an alias, you can do something like gs instead. Here’s how you do it:

First, you need to figure out where your configuration file is. I use ohmyzsh so my config file is located at ~/.zshrc If you use bash, you’ll want to use ~/.bash_profile . If it doesn’t exist, you’ll want to create it. You can do it in terminal simply by typing:

touch ~/.bash_profile

Then open it in your text editor of choice. I you like Atom you would do this:

open -a Atom ~/.bash_profile

or, in my case:

open -a Atom ~/.zshrc

To create an alias, just type:

alias thingyouwanttotype=‘action you want to happen’

So if you want to use gs for git status just do:

alias gs="/usr/local/bin/git status"

(You need the fully qualified path for applications when you call them from within the config file.)

Important You have to “source” the config file before the changes take place. So if you made an alias and it’s not working, chances are you forgot to source it. Save the config file and then back in the terminal type:

source ~/.zshrc

And yes you should create an alias for that:

alias zshsource='source ~/.zshrc'

Another really handy alias is showing the “dot” files in Finder (.gitignore for example). By default they’re hidden. So if you want to show them you can do this:

alias showFiles='defaults write com.apple.finder AppleShowAllFiles -boolean true ; killall Finder /System/Library/CoreServices/Finder.app'

alias hideFiles='defaults write com.apple.finder AppleShowAllFiles -boolean false ; killall Finder /System/Library/CoreServices/Finder.app'

Since it’s so easy to show them, I like to keep them hidden so my Finder is less cluttered.

Anything you frequently type on the command line should be reduced to an alias or function if possible. This means you should make it as easy as possible to edit your config file. I can just type in zshconfig, add a quick alias, and then type in zshsource, and go about my day. This is also good for when you haven’t created an alias for 3 months and now you have a really good one to add but can’t quite remember what the config file is called or where it is. It’s a lot easier to remember zshconfig.

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.