DEV Community

David Carr
David Carr

Posted on • Originally published at dcblog.dev on

My Terminal Aliases

What is a terminal alias?

A terminal alias is a shortcut for a terminal command. For example, as a Laravel user, I would type PHP artisan often so I've created an alias of a so I can then type a route:list to get a list of routes instead of having to type out PHP artisan route:list.

Aliases are a great way to be more efficient at using a terminal for common commands.

How to set up terminal aliases

To create an alias you need to edit a .bash_profile or .profile or even a .bash_aliases or a .zshrc when using ZSH

When using bash I'll use .bash_profile.

Use VIM to open .bash_profile in the home directory if the file doesn't exist it will be created.

vim ~/.bash_profile

Enter fullscreen mode Exit fullscreen mode

To create an alias use the keyword alias followed by the alias name="" then inside the quotes place the command to be aliased.

For example, create an alias called ls that will use the command ls -ls -a which will list all files in a vertical list and show hidden files.

alias ls="ls -ls -a"

Enter fullscreen mode Exit fullscreen mode

My Aliases

The following are the aliases I use on my machine.

Tools

Open PhpStorm or Vscode in the current directory, to open VSCode in a folder navigate to the folder and type code . to open the root folder in VSCode.

alias storm='open -a "/Users/dave/Applications/JetBrains Toolbox/PhpStorm.app" "`pwd`"'
alias code='open -a "/Applications/Visual Studio Code.app" "`pwd`"'

Enter fullscreen mode Exit fullscreen mode

Running Tests

These will run either pest or PHPUnit depending on if PEST is installed. Activate by pressing p.

To run a filter for a specific test use the alias pf followed by the method or file name.

# Run tests

function p() {
   if [-f vendor/bin/pest]; then
      vendor/bin/pest "$@"
   else
      vendor/bin/phpunit "$@"
   fi
}

function pf() {
   if [-f vendor/bin/pest]; then
      vendor/bin/pest --filter "$@"
   else
      vendor/bin/phpunit --filter "$@"
   fi
}

Enter fullscreen mode Exit fullscreen mode

Laravel

alias a="php artisan"
alias t="clear && php artisan test"
alias tp="clear && php artisan test --parallel"
alias phpunit="vendor/bin/phpunit"
alias pest="vendor/bin/pestphp"

Enter fullscreen mode Exit fullscreen mode

Mac show / hide files

# Show/hide hidden files in Finder
alias show="defaults write com.apple.finder AppleShowAllFiles -bool true && killall Finder"
alias hide="defaults write com.apple.finder AppleShowAllFiles -bool false && killall Finder"

Enter fullscreen mode Exit fullscreen mode

PHP version switch

When PHP is installed via homebrew use these aliases to switch between PHP 8.1 and 7.4

# PHP switcher
alias switch-php8="brew unlink php@7.4 && brew link --overwrite --force php@8.1"
alias switch-php74="brew unlink php && brew link --overwrite --force php@7.4"

Enter fullscreen mode Exit fullscreen mode

Laravel Valet

alias vs='valet secure' alias tunnel='valet share -subdomain=dc -region=eu'

Enter fullscreen mode Exit fullscreen mode

Edit hosts file

#host file 

alias hostfile="sudo vi /etc/hosts"

Enter fullscreen mode Exit fullscreen mode

Composer

#composer
alias cu='composer update'
alias ci='composer install'
alias cda='composer dump-autoload -o'
alias cr='composer require'

Enter fullscreen mode Exit fullscreen mode

GIT

use gac function to GIT Add and Commit files use it like gac . to commit all unstaged files. or use gac a-file-that-changed to commit a specific file.

I alias git to use HUB from GitHub

#git
function gac()
{
   #usage gac . 'the message'
   git add $1 && git commit -m "$2"
}

alias git=hub
alias g="hub"
alias gc="git checkout"
alias gm="git merge"
alias gl="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
alias gs="git status"
alias gp="git push"
alias gpu="git pull"
alias gno="git reset --hard HEAD"
alias glog='git log --oneline --decorate --graph --all'

Enter fullscreen mode Exit fullscreen mode

IP Lookup

# IP addresses
alias ip="curl https://diagnostic.opendns.com/myip ; echo"
alias localip="ifconfig -a | grep -o 'inet6\? \(addr:\)\?\s\?\(\(\([0-9]\+\.\)\{3\}[0-9]\+\)\|[a-fA-F0-9:]\+\)' | awk '{ sub(/inet6? (addr:)? ?/, \"\"); print }'"

Enter fullscreen mode Exit fullscreen mode

Directory paths

use aliases to make shortcuts for folder locations

#directories
alias ls="ls -ls -a"
alias projects="cd ~/Dropbox\ \(dcblog\)/local/projects"
alias personal="cd ~/Dropbox\ \(dcblog\)/local/personal"

Enter fullscreen mode Exit fullscreen mode

SSH

#ssh
alias sshkey="cat ~/.ssh/id_rsa.pub"
alias sshconfig="vi ~/.ssh/config"
alias copykey='command cat ~/.ssh/id_rsa.public | pbcopy'

Enter fullscreen mode Exit fullscreen mode

Complete collection

#tools
alias storm='open -a "/Users/dave/Applications/JetBrains Toolbox/PhpStorm.app" "`pwd`"'
alias code='open -a "/Applications/Visual Studio Code.app" "`pwd`"'

# Run tests
function p() {
   if [-f vendor/bin/pest]; then
      vendor/bin/pest "$@"
   else
      vendor/bin/phpunit "$@"
   fi
}

function pf() {
   if [-f vendor/bin/pest]; then
      vendor/bin/pest --filter "$@"
   else
      vendor/bin/phpunit --filter "$@"
   fi
}

#laravel
alias a="php artisan"
alias t="clear && php artisan test"
alias tp="clear && php artisan test --parallel"
alias phpunit="vendor/bin/phpunit"
alias pest="vendor/bin/pestphp"

# Show/hide hidden files in Finder
alias show="defaults write com.apple.finder AppleShowAllFiles -bool true && killall Finder"
alias hide="defaults write com.apple.finder AppleShowAllFiles -bool false && killall Finder"

# PHP switcher
alias switch-php8="brew unlink php@7.4 && brew link --overwrite --force php@8.1"
alias switch-php74="brew unlink php && brew link --overwrite --force php@7.4"

#valet
alias vs='valet secure'
alias tunnel='valet share -subdomain=dc -region=eu'

#host file
alias hostfile="sudo vi /etc/hosts"

#composer
alias cu='composer update'
alias ci='composer install'
alias cda='composer dump-autoload -o'
alias cr='composer require'

#git
function gac()
{
   #usage gac . 'the message'
   git add $1 && git commit -m "$2"
}

alias git=hub
alias g="hub"
alias gc="git checkout"
alias gm="git merge"
alias gl="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
alias gs="git status"
alias gp="git push"
alias gpu="git pull"
alias gno="git reset --hard HEAD"
alias glog='git log --oneline --decorate --graph --all'

# IP addresses
alias ip="curl https://diagnostic.opendns.com/myip ; echo"
alias localip="ifconfig -a | grep -o 'inet6\? \(addr:\)\?\s\?\(\(\([0-9]\+\.\)\{3\}[0-9]\+\)\|[a-fA-F0-9:]\+\)' | awk '{ sub(/inet6? (addr:)? ?/, \"\"); print }'"

#directories
alias ls="ls -ls -a"
alias projects="cd ~/Dropbox\ \(dcblog\)/local/projects"
alias personal="cd ~/Dropbox\ \(dcblog\)/local/personal"

#ssh
alias sshkey="cat ~/.ssh/id_rsa.pub"
alias sshconfig="vi ~/.ssh/config"
alias copykey='command cat ~/.ssh/id_rsa.public | pbcopy'

Enter fullscreen mode Exit fullscreen mode

Restart the terminal then type your shortcut and press enter to be taken to the aliased location.

Aliases are really useful and simple to set up.

Other Terminal Aliases

@joelwmale published a blog post on his terminal aliases - I definitely recommend checking it out.

We do share some of the same aliases, but if you combine both of ours, youll get over 10+ years worth of aliases that are used every day.

Top comments (0)