If you use the command line, there are probably some commands you run frequently. Some of these commands can be quite long, and copying or typing them each time can be tedious. In such cases, you can create an alias for a command.
For example, if you want to use h as a shortcut for cd ~, you can define it in your shell configuration file:
alias h='cd ~'Add this line to your ~/.zshrc (or ~/.bashrc).
If you donโt see such a file in your home directory, you can create one with:
touch ~/.zshrcIf you find any useful aliases below, add them to your ~/.zshrc file.
Basic
change directory
cs() { cd $@ && la }
alias cd='cs'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias cb='cd -'
alias d='cd ~/Desktop'
alias dl="cd ~/Downloads"
alias h='cd ~'
alias /='cd /'
# Change working directory to the topmost Finder window location.
cdf() { cd "$(osascript -e 'tell app "Finder" to POSIX path of (insertion location as alias)')" }show files
alias ls='gls --color --group-directories-first -F'
alias l='ls'
alias la='ls -A'
alias ll='ls -AhlS'
alias ds='du -d 1 -h 2>/dev/null | sort -h'
alias pwd='sed "s/ /\\\ /g" <<< ${PWD/#$HOME/"~"}'
alias p='pwd'
alias path='echo -e ${PATH//:/\\n}'-
ls: To usegls, you need to installcoreutilswithbrew install coreutils. You can use the same colorization as thetreecommand.--coloroption colorize the output ofglscommand.--group-directories-firstoption puts directories first.-Foption adds a trailing/to directory names,@to symbolic links, and so on.
-
la,ll:lsis defined asgls --color --group-directories-first -Fbeforela='ls -A'. This meansla='gls --color --group-directories-first -F -A'and the same forll.-Aoption shows all files and directories except.and...-hoption shows the size in human readable format.-loption shows the file size, owner, group, and permissions.-Soption sorts by file size.
-
ds:du -d 1shows the size of directories in the current directory.-hoption shows the size in human readable format.2>/dev/nullhides error messages.sort -hsorts by file size using pipe.
-
pwd:sed "s/ /\\\ /g"puts\before every space.<<<is a "here string".${PWD/#$HOME/"~"}replaces$HOMEwith~in the current directory path.
When you specify options, you can use ls -AhlS instead of ls -A -h -l -S.
edit files
alias v='vi'
alias cp='cp -iv'
alias mv='mv -iv'
alias rm='rm -iv'
alias rf='rm -rf'-ioption asks you before overwriting a file.-voption shows the name of the file being copied, moved, or removed.
search
fb() { find . -size +$2M -type f -name $1 -exec ls -lhS "{}" +}
rn() { for filename in *.$1; do mv -f "$filename" $(echo "$filename" | sed -e "s/$2//g"); done }
dif(){ diff --color -u $1 $2 }
alias imgopt='open -a ImageOptim .'
alias grep='grep --color'You can make an alias with arguments, which is called a function. Functions are defined as function_name() { commands }. For example, fb takes two arguments, $1 and $2. $1 is the first argument and $2 is the second argument. Use it like fb "*.pdf" 10 to find files with the name pdf larger than 10 MB.
In addition to $1 and $2, there are other special variables: $0 is the function name. $@ is all arguments. $# is the number of arguments. $? is the exit status of the last command. $$ is the process ID of the current shell. $! is the process ID of the last command run in the background.
fbfinds files larger than$2MB with the name$1in the current directory.-size +$2Moption finds files larger than$2MB.-type foption finds only files (-type dfinds only directories).-name $1option finds files with the name$1.-exec ls -lhS "{}" +option executesls -lhScommand for each file found.
rnrenames files with the extension$1by removing$2from the file name. For example,rn txt asdfrenamesaaasdfff.txttoaaff.txt.
-exec <command> {} + is a common syntax to execute <command> for each file found. {} is a placeholder for the file name. + is a delimiter to tell the end of the command.
open apps
alias hr='open .'
alias c='open /Applications/CotEditor.app'
alias vs='code'
alias chrome='open /Applications/Google\ Chrome.app'hropens the current directory.copens CotEditor.vsopens Visual Studio Code.chromeopens Google Chrome.
others
alias his='history'
alias rl='exec ${SHELL} -l' #reloadzip encryption
zipen(){
zip -er enc.zip "$@"
}zipen zips files and encrypts them with a password into enc.zip. Use like zipen file1 file2 dir1.
"$@" is a special variable that expands to all arguments. For example, "$@" is expanded to file1 file2 dir1 in the above example.
Mac OS settings
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"You can also use Command + Shift + . to show/hide hidden files in Finder.
Hide/show all desktop icons
alias dhide="defaults write com.apple.finder CreateDesktop -bool false && killall Finder"
alias dshow="defaults write com.apple.finder CreateDesktop -bool true && killall Finder"Screenshot settings
alias dwl='defaults write com.apple.screencapture location'
alias ddl='defaults delete com.apple.screencapture location'
alias drl='defaults read com.apple.screencapture location'You can change the location of screenshots by dwl ~/path/to/dir.
sleep setting
alias sleepon='sudo pmset -a disablesleep 0'
alias sleepoff='sudo pmset -a disablesleep 1'Python
alias wpy='which python'
alias pip='pip3'
alias pin='pip install'
alias puin='pip uninstall'
alias pup='pip install --upgrade pip'
alias pinreq='pip install -r requirements.txt'
alias pf='pip list --format=freeze'
alias pfr='pip list --format=freeze > requirements.txt'Activate, deactivate venv
alias acv='source venv/bin/activate'
alias deac='deactivate'GitHub
alias g='git'
alias ga='git add'
alias gb='git branch'
alias gc='git commit'
alias gch='git checkout'
alias gcl='git clone'
alias gd='git diff'
alias gf='git fetch'
alias gi='git init'
alias gm='git merge'
alias gps='git push'
alias gpl='git pull'
alias gpom='git push origin main'
alias gs='git status'Create a new GitHub repository and make the initial commit
You can define a function to make a new repository with just one command.
# Usage: ginit private[public]
ginit() {
git init
git add .
git commit -m "๐ Initial commit"
gh repo create --"$1" --source=. --push
}You need to install GitHub CLI to use the gh command.
Commit Messages with Emoji
# Git Commit, Add all and Push โ in one step.
gacp() { git add . && git commit -m "$*" && git push origin main }
gini() { gacp "๐ Initial commit"}
gnew() { gacp "โจ NEW: $@" }
gimp() { gacp "๐ IMPROVE: $@" }
gprg() { gacp "๐ง PROGRESS: $@" }
gmtn() { gacp "๐ง MAINTAIN: $@" }
gfix() { gacp "๐ FIX: $@" }
ghot() { gacp "๐ HOTFIX: $@" }
gbrk() { gacp "โผ๏ธ BREAKING: $@" }
grem() { gacp "๐๏ธ REMOVE: $@" }
gmrg() { gacp "๐ MERGE: $@" }
gref() { gacp "โป๏ธ REFACTOR: $@" }
gtst() { gacp "๐งช TEST: $@" }
gdoc() { gacp "๐ DOC: $@" }
grls() { gacp "๐ RELEASE: $@" }
gsec() { gacp "๐ฎ SECURITY: $@" }
# Show commit type
gtyp() {
NORMAL='\033[0;39m'
GREEN='\033[0;32m'
echo "$GREEN gini$NORMAL โ ๐ Initial commit
$GREEN gnew$NORMAL โ โจ NEW
$GREEN gimp$NORMAL โ ๐ IMPROVE
$GREEN gprg$NORMAL โ ๐ง PROGRESS
$GREEN gmtn$NORMAL โ ๐ง MAINTAIN
$GREEN gfix$NORMAL โ ๐ FIX
$GREEN ghot$NORMAL โ ๐ HOTFIX
$GREEN gbrk$NORMAL โ โผ๏ธ BREAKING
$GREEN grem$NORMAL โ ๐๏ธ REMOVE
$GREEN gmrg$NORMAL โ ๐ MERGE
$GREEN gref$NORMAL โ โป๏ธ REFACTOR
$GREEN gtst$NORMAL โ ๐งช TEST
$GREEN gdoc$NORMAL โ ๐ DOC
$GREEN grls$NORMAL โ ๐ RELEASE
$GREEN gsec$NORMAL โ ๐ฎ SECURITY"
}
gitignore.io
gitignore.io enables us to make .gitignore file easily
function gi() { curl -sLw n https://www.toptal.com/developers/gitignore/api/$@ ;}Extra: Customize and colorize PROMPT
PS1="%F{082}%n%f %F{051}%~%f %# "
RPROMPT='%T'PS1is the main (left) prompt andRPROMPTis the right prompt.%nmeans username%~means current directory%#shows#if you are root,%if not.%Tshows the current time in 24-hour format (%tfor 12-hour format).- you can colorize your prompt by using
%F{color number}~%f. You can find color numbers here.
Read more about Prompt Expansion in this link.
If you want to put a blank line before every prompt except the first one, you can use the following code:
precmd() { precmd() { echo } }