Conveniently jumping to directories using Aliases in Terminal
One of the most common things that you would often do in a Linux/Unix-based system is to navigate to a directory. And if you’re a developer, you would find yourself doing this multiple times a day.
Now, this is not a big deal if you’re navigating to a directory that is on the same level as your current directory. But if you’re navigating to a directory that is nested deep inside the directory structure, it can be a bit of a hassle.
For instance, if you want to navigate to a directory that is nested deep inside the directory structure, you would have to do something like this:
$ cd /home/amitmerchant/workspace/amitmerchant.com/
As you can tell, this is a bit of a hassle. You would have to type in the whole path to the directory which is not only time-consuming but also error-prone. You can easily make a typo and end up in a different directory. Also, if these directories are something you visit frequently, you would have to type in the whole path every time you want to navigate to it.
Some terminals like Fish provides a way to autocomplete the path but it’s still not convenient.
To get around this problem, you can use Aliases in your terminal. Aliases are essentially shortcuts to commands.
So, for instance, if you want to create an alias for the above command, you can do it like so.
$ alias cdblog="cd /home/amitmerchant/workspace/amitmerchant.com/"
This will create an alias called
cdblog which will be pointing to the directory
/home/amitmerchant/workspace/amitmerchant.com/. Now, whenever you want to navigate to this directory, you can simply type in
cdblog in your terminal and it will take you to the directory.
This will work as long as you’re in the same terminal session. Once you close the terminal, the alias will be gone. To make it permanent, you can add the alias to the configuration file of your shell.
For instance, in my case, I use the Fish shell. So, I can add the alias to the
~/.config/fish/config.fish file like so.
alias cdblog="cd /home/amitmerchant/workspace/amitmerchant.com/"
Save the file and restart the terminal. Now, you can use the alias in any terminal session.
This works the same way for other shells as well, like Bash, Zsh, etc.
You can create multiple aliases for different directories and use them as you like. So, even if you want to navigate to a directory that is at a different level of the directory structure, you can do it using aliases. To jump back and forth between these directories becomes a cake walk.
Taking it further
I use aliases for frequently used commands as well. For instance, I have an alias called
runblog which is pointing to a pretty long command which I use to run my blog locally.
So, instead of typing in the whole command, I can simply type in
runblog and it will run the command for me.
$ alias runblog "bundle exec jekyll serve --config dev.yml"
Or to restart the MySQL server with
$ alias mysqlstart "brew services start mysql"
Or to open the Fish configuration file.
$ alias fishc "sudo nano ~/.config/fish/config.fish"
You get the idea, right?
Like this article? Consider leaving aTip