Command Aliases, Let’s Save Time And Not Repeat Our Commands
You can watch this video if you want to see step by step execution of this tutorial.
or you can keep reading, totally up to you. 😄
Let’s take a scenario, you created a laravel project to work on and named it my-awesome-application
composer create-project laravel/laravel my-awesome-application
which will create a directory with name my-awesome-application
Then you went inside that directory with the command
cd my-awesome-application
Then you ran a command to start some server there.
In my case I use the command: sail up -d
but let's say you need the same command. Then we need to compile our JS libraries so we use one more command.
npm run dev
or
yarn run dev
I am leaving the npm install or yarn command since those are needed only once.
So now every time I start my computer I need to run this combination of commands.
cd my-awesome-application
sail up -d
yarn run dev
And you might need some more if you start exploring like cleaning docker images etc which is a discussion of a different day.
or assume any repetitive command in your development work.
you can shorthand this to just writing sma (start my application) or whatever shorthand you wish, totally up to you.
let's see how.
start terminal by pressing ctrl + shift + T
Then here type:
nano ~/.bashrc
Press Enter.
Now scroll all the way down with the down arrow key.
here at bottom of the file type:
alias sma='cd ~/my-awesome-application && sail up -d && yarn run dev'
you can just add whatever combination of commands you need, just keep adding them with &&.
Then press CTRL + O (write out)
Press Enter
Then press CTRL + X ( To exit)
now in the terminal type this:
source ~/.bashrc
we are all set.
Now try your alias by typing sma in the terminal and pressing Enter.
you will see it executes your commands one by one.
now whenever you turn on your computer just type sma in the terminal press Enter and you are ready to code.