Are you tired of writing long commands in your terminal ? Of course you are. It’s a nightmare when you have to enter a lengthy command in your terminal, especially the ones that have long file paths and the ones that you have to use often in your terminal. Most of the times, we’ll have to copy the command from somewhere and paste it into the terminal to execute it.
There is a way to solve this. What if I tell you that we can create shortcuts or aliases for commands and use them instead of the original commands. Yes, we can do it by using the alias command. The steps are as follows.
The first step is to figure out what command line shell your terminal is running on. It can be either bash or zsh based on your OS. You can check your shell type by opening your terminal and typing in the command
echo $0and pressing enter. The output will let you know if your shell is zsh or bash.
Once you figure out what your shell type is, the next step is to add the shortcut for your lengthy command. In order to add the command you should open the rc file based on your shell type.
Open your terminal and type
ls -aand press enter. This will list all the files along with the hidden ones. Scroll through the list of files to see if you have a .zshrc file if your shell type is zsh or a .bashrc file if your shell type is bash. If you don’t find the rc file, you can create one using the command
touch ~./bashrcif your shell type is bash or
touch ~./zshrcif your shell type is zsh.
Now open your rc file using the command
open ~/.bashrcif your shell type is bash or
open ~/.zshrcif your shell type is zsh. In a new line, add your shortcut by entering the command
For example, if the original command that you have to enter in the terminal is
export JAVA_HOME=`/usr/libexec/java_home-v 1.8` which is intended to change the java version to 1.8, and if you want the shortcut command to be
switchToJava8, then the rc file must contain the following line to set the alias command.
alias switchToJava8="export JAVA_HOME=`/usr/libexec/java_home-v 1.8`"
Save the rc file and reopen the terminal to use your shortcut command that you have set in your rc file. For the above example, you can just type
switchToJava8 in your terminal and press enter to see the original command being run.