Skip to content

Instantly share code, notes, and snippets.

@nfhipona
Last active September 12, 2022 15:15
Show Gist options
  • Save nfhipona/2b95b262c86e04ae7b01d17606c9536a to your computer and use it in GitHub Desktop.
Save nfhipona/2b95b262c86e04ae7b01d17606c9536a to your computer and use it in GitHub Desktop.
Creating Alias in Mac Terminal

To create an alias, use alias ALIAS_NAME="ALIAS_VALUE":

alias ll="ls -al"

To remove the alias, use the unalias command:

unalias ll

To see a list of all your aliases, use the alias command:

alias

Permanent Aliases

To make aliases permanent, we have to set them in a file that’s read when you open Terminal. Some common ones are ~/.bashrc and ~/.bash_profile. For this example, let’s use ~/.bash_profile.

From the command line, open to edit the file by running the following:

nano ~/.bash_profile

Add the following lines either at the bottom of the file or wherever you’d like:

# -------
# Aliases
# -------
alias ll="ls -al"

To reload the Terminal with set profile ~/.bash_profile. Use the source command:

source ~/.bash_profile

Useful Aliases

# -------
# Aliases
# -------
alias clr="clear" # Clear your terminal screen
alias flush="sudo discoveryutil udnsflushcaches" # Flush DNS (Yosemite)
alias flush="killall -HUP mDNSResponder" # Flush DNS (Mavericks, Mountain Lion, Lion)
alias flush="dscacheutil -flushcache" # Flush DNS (Snow Leopard, Leopard)
alias ip="curl icanhazip.com" # Your public IP address
alias ll="ls -al" # List all files in current directory in long list format
alias ldir="ls -al | grep ^d" # List all directories in current directory in long list format
alias o="open ." # Open the current directory in Finder
alias ut="uptime" # Computer uptime

Open Text Files with alias

alias opencode="open -a \"Visual Studio Code\""

Reference: Link


zsh

Open .zshrc file in vim:

vi ~/.zshrc

Add the following lines:

source ~/.bash_profile

Reload zsh:

source ~/.zshrc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment