Using aliases on the command line.
If you use the command line and you’re lazy, this is for you. I’ve picked up a lot of tips in customzing my command line over the course of many years. Most of them I abandon after the “new” wears off, but a few stick around. Specifically, I have some aliases that I find useful enough that they’re part of the set up process when I get a new Mac. (Yes, these are Mac specific but you can adapt to your system.)
I keep my aliases in a file named .aliases
at the root of my home directory. That’s not some “magic” name though. It’s just named in a way that makes sense to me. It doesn’t automatically get added to my command line environment. To make that happen, you need to add a line to one of your files that is automatically called when you open a new terminal window.
Make sure you are in your home directory and then make the .aliases file.
cd ~
touch .aliases
You should now have an empty file called .aliases
there. You can edit it with a text editor like Sublime Text, Atom, or VSCode or if you want to do everything from the command line, Nano is an easy way to do that.
Next we’ll make sure it gets automatically invoked when a new termimal shell is opened. Take a look at the Bash documentation to see what order these automatically invoked files are loaded in.
When Bash is invoked as an interactive login shell… it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.
In my case, I use the .bash_profile
to include the aliases. To do this you need to use the source
command like this.
# this line is added to the .bash_profile file
source ~/.aliases
That’s it. The next time you open a new terminal window, your .aliases
file will be automatically invoked, adding the aliases we’re about to set up to your list of available commands.
Relative directory short cuts
One of the most basic things you do on the command line is move between directories. Moving backwards up the chain of directories can be made easier by making use of the .
character. 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 terminal. That’s far too much work. We’re trying to be lazy here.
You can reload your terminal with a simple command exec $SHELL -l
. It’s much easier to remember a word that describes what you’re trying to do when you reload the terminal window though. I keep the following at the very bottom 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 terminal and anything you’ve added to your aliaes will be available immediately. Since you just added this though, you will either need to open a new terminal window or use the long version of this command one last time, exec $SHELL -l
.
Specific directory shortcuts
There are a few spots in my computer where I keep most of my stuff. I’ve set up single character aliases to make getting to these quick and easy. Obviously 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 personalized shortcuts in place, just reload
your terminal session.
Listing directory contents
I mentioned earlier that this is Mac specific. The -G
flag we’ll use here is the way you get a list of files with color on a Mac. Other systems may need a different option. Even without the color option though, these will end up saving 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 terminal, just type l
or la
and see all the files in your directory.
This will make you so much better at being lazy. We’re getting good at this!
There are many posts online on how to fully customize terminal life. This is just the beginning of what you can do there.
If you use the command line and you’re lazy, this is for you. I’ve picked up a lot of tips in customzing my command line over the course of many years. Most of them I abandon after the “new” wears off, but a few stick around. Specifically, I have some aliases that I find useful enough that they’re part of the set up process when I get a new Mac. (Yes, these are Mac specific but you can adapt to your system.)
I keep my aliases in a file named .aliases
at the root of my home directory. That’s not some “magic” name though. It’s just named in a way that makes sense to me. It doesn’t automatically get added to my command line environment. To make that happen, you need to add a line to one of your files that is automatically called when you open a new terminal window.
Make sure you are in your home directory and then make the .aliases file.
cd ~
touch .aliases
You should now have an empty file called .aliases
there. You can edit it with a text editor like Sublime Text, Atom, or VSCode or if you want to do everything from the command line, Nano is an easy way to do that.
Next we’ll make sure it gets automatically invoked when a new termimal shell is opened. Take a look at the Bash documentation to see what order these automatically invoked files are loaded in.
When Bash is invoked as an interactive login shell… it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.
In my case, I use the .bash_profile
to include the aliases. To do this you need to use the source
command like this.
# this line is added to the .bash_profile file
source ~/.aliases
That’s it. The next time you open a new terminal window, your .aliases
file will be automatically invoked, adding the aliases we’re about to set up to your list of available commands.
Relative directory short cuts #
One of the most basic things you do on the command line is move between directories. Moving backwards up the chain of directories can be made easier by making use of the .
character. 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 terminal. That’s far too much work. We’re trying to be lazy here.
You can reload your terminal with a simple command exec $SHELL -l
. It’s much easier to remember a word that describes what you’re trying to do when you reload the terminal window though. I keep the following at the very bottom 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 terminal and anything you’ve added to your aliaes will be available immediately. Since you just added this though, you will either need to open a new terminal window or use the long version of this command one last time, exec $SHELL -l
.
Specific directory shortcuts #
There are a few spots in my computer where I keep most of my stuff. I’ve set up single character aliases to make getting to these quick and easy. Obviously 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 personalized shortcuts in place, just reload
your terminal session.
Listing directory contents #
I mentioned earlier that this is Mac specific. The -G
flag we’ll use here is the way you get a list of files with color on a Mac. Other systems may need a different option. Even without the color option though, these will end up saving 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 terminal, just type l
or la
and see all the files in your directory.
This will make you so much better at being lazy. We’re getting good at this!
There are many posts online on how to fully customize terminal life. This is just the beginning of what you can do there.