Add Aliases in Linux
~1 minute read
Sometimes, when you are using command-line a lot, it can be boring to always type the full command to perform the action. But you can create shortcuts, which are also called “alias”.
For example, you have a Python virtual environment and you are using it a lot. To activate it, you generally have to be in the appropriate folder or to source it by typing the absolute path:
pi@raspberrypi:~ $ source venv/bin/activate
It would be easier if, by typing “venv”, it automatically loads the virtual environment. To do so, we have to edit the ./bashrc file which is in your user folder root. This document is hidden by default. To edit it (from /home/your_username/):
pi@raspberrypi:~ $ nano .bashrc
Then, at the end of the file, we will add:
# Aliases
alias venv='cd /home/pi/ && source venv/bin/activate'
In this case, we have combined two commands by adding “&&”. We save the file (Ctrl + O) and we close (Ctrl + X).
To reload it, you can logout or type in command-line:
pi@raspberrypi:~ $ source ~/.bashrc
Then if you just type “venv” and enter, it will load your commands (in my case, the virtual environment).