mrkaluzny homepage
Tips & Tricks

Terminal 101: Increase productivity in Terminal with aliases.

Feb 3, 2020

Working efficiently with the command-line often means limiting the amount of typing we have to do. This can be achieved by creating shortcuts to limit the typing you need to perform everyday tasks!

How to create aliases?

Aliases are basically shortcuts or, more precisely, abbreviations. They have their limitations but can speed up your work by avoiding excessive typing. In the previous article you could learn basic command line commands, now let's take a look at how we can optimize their usage.

Creating a temporary alias in the command line is really! Let's try setting an alias for force removing directory rm -rf. To set up our temporary alias let's open Terminal and type in the following command

alias rdir="rm -rf"

With the command above we have created an alias rdir. Running this command along with the directory name will result in removing the directory with all of its content. We can also remove the alias by running the following command.

unalias rdir

If you're interested if there are any aliases defined on your machine you can simply run alias command to see the full list of aliases.

How to set permanent aliases?

Using temporary aliases has many downsides. Most important of them being that you can't use them in other Terminal window or after reopening the program. But no worries! There's a way to set up permanent aliases.

Depending on which shell you're using there's going to be a slight difference! There are mainly 2 most popular shells - zsh (or Z Shell) and Bash.

Starting with macOS Catalina (10.16) Apple made the default shell zsh (switching from previously used Bash).

Depending on which shell you use, we have to change different files. If you're using Bash we need to open your .bash_profile file.

cd ~
atom ~/.bash_profile # I'm using atom to edit the file

If you're running zsh as your shell then look for the .zsh file instead.

atom ~/.zsh

Adding aliases is basically the same when using both shells. In the config of the shell, you need to add the same alias definition we used in our example (each on a new line). Here's a line from my .zsh file showing the definition of pa alias.

# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
alias pa="php artisan"

After adding aliases to your configuration file to use them you need to refresh your shell. You can do it by closing restarting the program you use or run the following command to apply changes.

source ~/.bash_profile # If you're using bash

source ~/.zsh # If you're using zsh

That's it! You can now add your own aliases that will make your work faster!

Useful aliases starter pack!

I put together a simple starter pack of aliases that you might find useful (I use them all the time) and save a couple minutes each day!

alias a="atom ." # Open the current directory in Atom
alias o="open ." # Open the current directory in Finder
alias clr="clear" # Clear your terminal screen
alias g="git" # Making git command shorter
alias pa="php artisan" # Shorthand artisan commands
alias ..="cd .." # Go to parent directory faster
alias dev="cd ~/Developer" # Go to developer folder

Let me know which are your favorite aliases, I'm always looking for speeding my work up!