1.3. Alias#
last update: Feb 07, 2024
If you are using command line, there must be commands you use frequently.
Some commands are long and you might think that copying them every time is a waste of time. In such case, you can use alias for a command. If you want to use h as an alias for cd ~, you define alias h='cd ~' in ~/.zshrc (or ~/.bashrc). If you can’t find such files in your home directory, you need to make it with touch ~/.zshrc.
If you find some useful aliases below, write them in ~/.zshrc.
1.3.1. Basic#
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.
Put a blank line before every prompt except the first one.#
precmd() { precmd() { echo } }
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 top-most 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 astreecommand.--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.
Hint
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 "{}" +}
fd() { find . -name "*.$1" -type f -delete }
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'
Function
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 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.
fd:find . -name "*.$1" -type f -deletefinds files with the extension$1and deletes them.rnrenames files with the extension$1by removing$2from the file name. For example,rn txt asdfrenamesaaasdfff.txttoaaff.txt.
Note
-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 fire='open /Applications/Firefox.app'
alias chrome='open /Applications/Google\ Chrome.app'
alias safari='open /Applications/Safari.app'
hropens the current directory.copens CotEditor.vsopens Visual Studio Code.fireopens Firefox.chromeopens Google Chrome.safariopens Safari.
others#
alias his='history'
alias rl='exec ${SHELL} -l' #reload
zip encryption#
zipen(){
	zip -er enc.zip "$@"
}
zipenzips files and encrypts them with a password asenc.zip. Use likezipen file1 file2 dir1.
Note
"$@" is a special variable that expands to all arguments. For example, "$@" is expanded to file1 file2 dir1 in the above example.
1.3.2. Mac OS settings#
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'
1.3.3. 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'
Function#
gacpm() { git add -A && git commit -m "$1" && git push origin main }
gacpmadds all files, commits with the message$1, and pushes to the main branch of the origin remote repository. Use likegacpm "update README.md".
For example, you can define a function to make a new repository with just one command.
# $1 = private or 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 gh command.
gitignore.io#
gitignore.io enable us to make .gitignore file easily
function gi() { curl -sLw n https://www.toptal.com/developers/gitignore/api/$@ ;}
1.3.4. 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'
Make, activate, deactivate venv#
alias mkv='python3 -m venv venv; acv; pip install --upgrade pip'
alias acv='source venv/bin/activate'
alias deac='deactivate'
1.3.5. Latex#
Copy latex-template directory to somewhere;#
If you write a lot of latex documents, you should make a template directory. You can copy the template directory to somewhere with this function.
mklt(){
	cp -r ~/latex-template ./"$1"
}
mkbt(){
	cp -r ~/beamer-template ./"$1"
}