How to create alias for a command in terminal (bash) [2021 tutorial]

If you are used to working with terminal you might encounter you are writing the same commands again and again.

For example:

  • git pull
  • git push
  • composer install
  • npm install
  • … and so on

Writing them time and again can take up on nerves and it would be more comfortable to use the same commands but only with less text – and this is exactly what possible.

For example instead of npm install you might set up an alias like ni, for composer update it would be cu and so on.

How to do that

  1. fire up your terminal and change directory into the home folder with cd
  2. now, depending on your bash terminal you need to choose a configuration file
    1. .zshrc
    2. .bash_profile

The two have the same configuration syntax as long as the aliases are concerned.

The alias syntax is as follows:

alias gs='git status'
alias gd='git diff'
alias gc='git checkout'
alias gcm='git checkout master'
alias td='git checkout devel'
alias gl='git pull'
alias gh='git push'
alias phptest='vendor/bin/phpunit'
alias ni='npm i'
alias gp='git commit -am "change" && git push'

As you can see, you start with a keyword alias then put down the actual alias keyword, going on with an equals sign and in the quotes is the actual command.

Easy peasy right?

Tips for choosing alias names

Always choose an alias name that seems comfortable for you. For example I have one guy having an alias for pulling git changes (git pull) to be gg, having commit changes (with git commit) aliased with ga which totally not makes sense at all, but made sense to them and they were able to remember it.

Wrapping up

In this tutorial I guides you through how to create an alias in your terminal so as you can be more effective while working.

If you liked this tutorial, leave the comment in the comment section below letting me know it helped you.

Until next time,

Yours in coding,

Ivan

Sources:

Credits:

Leave a comment

Your email address will not be published. Required fields are marked *