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 event 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
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"
My Aliases
The following are the alises 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.
#tools
alias storm='open -a "/Users/dave/Applications/JetBrains Toolbox/PhpStorm.app" "`pwd`"'
alias code='open -a "/Applications/Visual Studio Code.app" "`pwd`"'
Running Tests
These will run either pest or PHPUnit depending which 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
}
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"
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"
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"
Laravel Valet
alias vs='valet secure'
alias tunnel='valet share -subdomain=dc -region=eu'
Edit hosts file
#host file
alias hostfile="sudo vi /etc/hosts"
Composer
#composer
alias cu='composer update'
alias ci='composer install'
alias cda='composer dump-autoload -o'
alias cr='composer require'
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'
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 }'"
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"
SSH
#ssh
alias sshkey="cat ~/.ssh/id_rsa.pub"
alias sshconfig="vi ~/.ssh/config"
alias copykey='command cat ~/.ssh/id_rsa.public | pbcopy'
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'
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.