TerminalTutorial

Speed up your development workflow with bash aliases

By January 8, 2020 February 25th, 2020 No Comments
3 min read

Bash aliases speed up your development workflow by allowing you to create shortcuts for longer commands. In other words, they allow you to create new commands that are short and can easily be remembered thereby reducing typing time on the command line interface/terminal and searching the internet for the right command.

For example, Typing vendor/bin/phpunit 50 times a day while testing my codes can be annoying or typing php artisan every time to run certain laravel commands can seem daunting.

Bash aliases to the rescue, for the above examples, we can assign a new shortcut command for vendor/bin/phpunit and php artisan with the following commands phpunit and art respectively.

How to create Bash Aliases

  1. Go to your home directory by entering   cd ~/   in your terminal
  2. List all the files including the files with the following command   ls -la  .
    • On Ubuntu and Windows, the file is called   .bashrc  . If it doesn’t exist in your home directory enter the command below to create it
      mkdir ~/.bashrc
    • On Mac, it is called   .bash_profile   or   .profile  . If none of them exists create one using the command below
      mkdir ~/.bash_profile
  3. Time to add our aliases or shortcuts. In your terminal open the bash file using the command below  or use your Operating System file explorer to open and edit the file which is located in your home directory located at /c/Users/<your-computer>(windows) or /Users/<your-computer> (Mac)
    nano ~/.bashrc

    Note

    • Replace .bashrc with .bash_profile if you are using a Mac.
  4. If there is any content in the file, all your aliases should be added at the bottom of the file. Add the following alias as your first aliases and save
    alias gs='git status'
    alias phpunit='vendor/bin/phpunit'
    alias art='php artisan'

    If you are using the terminal to edit the bash file press   CTRL + X   to save. When prompted to confirm enter   Y   to confirm and press   ENTER  

  5. Activate the aliases in the bash file by restarting the terminal or type the command below
    source ~/.bashrc
  6. From now on you can type in gs in your terminal and it will expand to git status, same for every other alias command in the bash file

That is all on how to create Bash aliases to speed up your development workflow.

If you ever forget the aliases you have set up for yourself, simply use the alias command to list the available aliases:

alias

As a bonus point for reaching the end of the article, I will be sharing bash aliases to speed up your development workflow below. If you loved this tutorial do not hesitate to like and share for others to learn. Thank you !!!

Navigation Aliases

#custom navigation aliases
alias .="cd .."
alias ..="cd ../.."
alias ...="cd ../../.."
alias ....="cd ../../../.."
alias .....="cd ../../../../.."

 

Open Your Project In Just One Command

alias my-project="cd ~/project/path-to-your-project && code ./ && npm run start"

 

Git Aliases

alias g='git'
alias gst='git status'
alias gd='git diff'
alias gdc='git diff --cached'
alias gl='git pull'
alias gup='git pull --rebase'
alias gp='git push'
alias gd='git diff'
alias gc='git commit -v'
alias gc!='git commit -v --amend'
alias gca='git commit -v -a'
alias gca!='git commit -v -a --amend'
alias gcmsg='git commit -m'
alias gco='git checkout'
alias gcm='git checkout master'
alias gr='git remote'
alias grv='git remote -v'
alias grmv='git remote rename'
alias grrm='git remote remove'
alias grset='git remote set-url'
alias grup='git remote update'
alias grbi='git rebase -i'
alias grbc='git rebase --continue'
alias grba='git rebase --abort'
alias gb='git branch'
alias gba='git branch -a'
alias gcount='git shortlog -sn'
alias gcl='git config --list'
alias gcp='git cherry-pick'
alias glg='git log --stat --max-count=10'
alias glgg='git log --graph --max-count=10'
alias glgga='git log --graph --decorate --all'
alias glo='git log --oneline --decorate --color'
alias glog='git log --oneline --decorate --color --graph'
alias gss='git status -s'
alias ga='git add'
alias gm='git merge'
alias grh='git reset HEAD'
alias grhh='git reset HEAD --hard'
alias gclean='git reset --hard && git clean -dfx'
alias gwc='git whatchanged -p --abbrev-commit --pretty=medium'

 

Laravel Aliases

#installation
alias laravel-installer="composer create-project --prefer-dist laravel/laravel "
alias key="php artisan key:generate"
alias serve="php artisan serve"

#database 
alias mm="php artisan make:migration"
alias mmm="php artisan make:migration -m "
alias migrate="php artisan migrate"
alias mfs="php artisan migrate:fresh --seed"

#controllers
alias mc="php artisan make:controller"
alias mrc="php artisan make:controller --resource "

#composer
alias cdo="composer dump-autoload -o"
alias ci="composer install"
alias co="composer outdated"
alias cu="composer update"

 

Docker Aliases

# Get latest container ID
alias dl="docker ps -l -q"

# Get container process
alias dps="docker ps"

# Get process included stop container
alias dpa="docker ps -a"

# Get images
alias di="docker images"

# Get container IP
alias dip="docker inspect --format '{{ .NetworkSettings.IPAddress }}'"

# Run deamonized container, e.g., $dkd base /bin/echo hello
alias dkd="docker run -d -P"

# Run interactive container, e.g., $dki base /bin/bash
alias dki="docker run -i -t -P"

# Execute interactive container, e.g., $dex base /bin/bash
alias dex="docker exec -i -t"

Cheers !!!

Leave a Reply

%d bloggers like this: