Skip to content

Instantly share code, notes, and snippets.

@liuderchi liuderchi/.zshrc
Last active Jan 23, 2018

Embed
What would you like to do?
My `.zshrc` config with installed `oh-my-zsh` and `PowerLevel9k` theme
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
# Path to your oh-my-zsh installation.
export ZSH="/Users/$USER/.oh-my-zsh"
# Set name of the theme to load. Optionally, if you set this to "random"
# it'll load a random theme each time that oh-my-zsh is loaded.
# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
# ZSH_THEME="robbyrussell"
ZSH_THEME="powerlevel9k/powerlevel9k" # for some customization theme please visit https://github.com/liuderchi/powerlevel9k/commit/b7249443b0aa7d17de56679a6b7240b229c67e92
#ZSH_THEME="random"
# NOTE
# use $ chsh -s /bin/zsh # setup default bash source
# Set list of themes to load
# Setting this variable when ZSH_THEME=random
# cause zsh load theme from this variable instead of
# looking in ~/.oh-my-zsh/themes/
# An empty array have no effect
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )
# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"
# Uncomment the following line to use hyphen-insensitive completion. Case
# sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"
# Uncomment the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"
# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13
# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"
# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"
# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"
# Uncomment the following line to display red dots whilst waiting for completion.
# COMPLETION_WAITING_DOTS="true"
# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"
# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# The optional three formats: "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# HIST_STAMPS="mm/dd/yyyy"
# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder
# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
# plugin list: https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins#osx
plugins=(osx git history zsh-autosuggestions zsh-syntax-highlighting)
# NOTE install custom plugin with cmd:
# git clone https://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions
# git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
# NOTE plugin katas
# history: hs 'git pull' # show usage
# debian: di # 'sudo dpkg -i'
source $ZSH/oh-my-zsh.sh
# NOTE my custom scripts for some command or theme
source "$HOME/zshrc/personal.cfg" # your personal sensitive data
# e.g.
# EMAILS=('amy@example.com' 'bob@domain.com')
# GH_LOGIN=myusernamegithub
# GH_TOKEN=$(cat $HOME/path/to/github-token)
# set auto suggestion color https://github.com/zsh-users/zsh-autosuggestions/issues/12
export ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=magenta'
# User configuration
# export MANPATH="/usr/local/man:$MANPATH"
# You may need to manually set your language environment
# export LANG=en_US.UTF-8
# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
# export EDITOR='vim'
# else
# export EDITOR='mvim'
# fi
# Compilation flags
# export ARCHFLAGS="-arch x86_64"
# ssh
# export SSH_KEY_PATH="~/.ssh/rsa_id"
# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
## NOTE 2017-05-16 which one: alias or function command?
# A. not runnable -> should use alias
# e.g. grbonto
# B. runnable and return value is usable or require default bash arg -> should use command
# e.g. currentbranch
# C. runnable but return value is not used -> dependent, use most readable one, considering which usage
# e.g. aptUpdateInstall
# 1. alias aptUpdateInstall='aptUpdate; aptInstall'
# 2. aptUpdateInstall() { aptUpdate; aptInstall; }
# note: IMO 2. is more readable in this case
alias clearHistoryZSH='rm $HISTFILE'
alias k8s='kubernetes'
alias k8ctl'kubectl'
alias lnsf='ln -sf'
alias lns='ln -s'
alias psAUX='ps -aux'
alias sourceBashrc='source ~/.bashrc'
alias sourceZshrc='source ~/.zshrc'
cddirname() { cd $(dirname ${1:-$(pwd)}) }
checkReboot() { if [ -f /var/run/reboot-required ]; then echo 'reboot required'; else echo 'no need reboot'; fi }
pd() { pushd -${1:-1}} # go find right (from top) (pushd -1 or push -X)
pd-() { pushd +${1:-0}} # go find left (from bottom)
psAUXGrep() { psAUX | awk '{if(NR==1)print}'; psAUX | grep $1 }
shellOptions() { printf %s\\n "$-" }
# ===== PowerLevel9k
# NOTE config theme: powerlevel9k
# NOTE run before running `source $ZSH/oh-my-zsh.sh` in zshrc
POWERLEVEL9K_MODE='awesome-fontconfig'
POWERLEVEL9K_PROMPT_ON_NEWLINE=true
POWERLEVEL9K_REVERSE_DISPLAY=true
pl9kVars() { set | awk '/^POWERLEVEL9K_/ {print $0}' }
plToggle() {
eval set_default $1 true # NOTE init: if not init, set a non-empty value
if [ $(eval "printf \$$1") == false ]; then eval $1=true; else eval $1=false; fi
}
pl_toggle_REVERSE_DISPLAY() { plToggle POWERLEVEL9K_REVERSE_DISPLAY }
pl_toggle_PROMPT_ON_NEWLINE() { plToggle POWERLEVEL9K_PROMPT_ON_NEWLINE }
pl_toggle_SHORTEN_DIR_LENGTH() { POWERLEVEL9K_SHORTEN_DIR_LENGTH="${1:-1}" }
# ===== Git
# NOTE use $GH_LOGIN, $EMAILS from personal.cfg
# NOTE use git alias degined in `$ZSH/oh-my-zsh.sh`
# g, gb, gc, gco, gf, gl, gp
gcurrentbranch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/' }
# git
alias gs='git status' # overwrite ghostscript (Sep 9 16')
alias gbmergable='gb -vv | grep behind' # show branches mergable with remote
alias gcaamend='git commit -a --amend'
alias gcaamendnoedit='git commit -a --amend --no-edit'
alias gcamend='git commit --amend'
alias gcamendnoedit='git commit --amend --no-edit'
alias gcAllowEmpty='gc --allow-empty'
alias grbonto='grb --onto' # zsh has grb: 'git rebase'
alias gshow='git show' # show detail of commit(s) separately
alias gstl='git stash list'
alias glprune='gl --prune' # use zsh alias gl: git pull (Dec 20 16')
alias gfprune='gf --all --prune' # use zsh alias gf: git fetch (Apr 21 17')
gco-() { gco - }
gpoCurrentbranch() { gp origin $(gcurrentbranch) } # use zsh alias in custom alias 'gp', push current branch to origin (Jan 5 17')
gpoCurrentbranchTracking() { gp origin -u $(gcurrentbranch) }
gloa--() { git log --all -- $1 } # log with --all option to includes commits involves renaming
grbMasterPush() { gco ${1:-$(gcurrentbranch)} && grb ${1:-$(gcurrentbranch)} && grb master && gp -f }
gfpmergable() { gfprune && gbmergable } # fetch with prune and show mergable branches
gdCommitCount() { git diff ${1:-HEAD}~$2 ${1:-HEAD} } # show diff of some range of commits starting from specific one
gconflist() { git config --list --show-origin }
gconflistglobal() { git config --list --global --show-origin }
gconfsetgpg() { git config commit.gpgsign true }
gconfigshowUser() { git config user.name; git config user.email }
gconfsetNameEmail() { git config user.name $GH_LOGIN; git config user.email $EMAILS[1] }
gconfsetHttpsClone() { git config --global url."https://".insteadOf git:// }
glistAllAuthors() { g log --format='%an < %ae >' | sort -u }
gbackupBranch() { # NOTE create a brach with timestamp for backup. https://stackoverflow.com/questions/13210880/replace-one-substring-for-another-string-in-shell-script
local _date=$(date +'%m%d/%I-%M%p') # NOTE fails if res='$(date +"%m-%d")'. double quote for _template string_
local branch=$(printf "bkp/$_date" | awk '{ gsub (" ", "", $0); print}' | awk '{print tolower($0)}' ) # NOTE remove space and lower branch name
git branch $branch &&\
echo "created branch: $branch"
}
grmuntrackfiles() { rm $(git ls-files --others --exclude-standard) }
gsync() { gco $1 && gl && gp origin } # sync current branch by mergin if not arg specified
gsyncRebase() { gco $1 && grbonto upstream/${1:-$(gcurrentbranch)} HEAD && gp origin -f } # sync current branch by rebasing if not arg specified
gRepoName() { basename -s .git $(git config --get remote.origin.url) }
# ===== GitHub
# use $GH_LOGIN, $GH_TOKEN from personal.cfg
# Github REST API
ghUpstreamUrl() {
local repoName=${1:-$(gRepoName)}
local responseFile='repoInfo.json'
curl -f https://api.github.com/repos/$GH_LOGIN/$repoName -H "Authorization: token $GH_TOKEN" > $responseFile &&\
printf $(node -pe "require('./$responseFile').source.clone_url") &&\
rm $responseFile
}
ghRemoteAddUpstream() {
local repoName=$1 # NOTE get current repo name
local remoteUrl=$(ghUpstreamUrl $repoName)
g remote add upstream $remoteUrl &&\
echo "added $remoteUrl to upstream remote"
}
ghSyncRepoWithUpstream() { # NOTE by entering personal fork repo, sync it with upstream repo
local repoName=$1
g clone "https://github.com/$GH_LOGIN/$repoName.git" &&\
cd $repoName &&\
g remote add upstream $(ghUpstreamUrl $repoName) &&\
gfprune &&\
gco master && gb -u upstream/master &&\
gsync master &&\
# NOTE may mave merge conflict issue
echo "\n$GH_LOGIN/$repoName has been updated with upstream\nDeleting downloaded repo..." &&\
cd ../ &&\
rm -rf ./$repoName &&\
echo 'Completed\n'
}
ghArchiveRepo() {
# NOTE CANNOT unarchive repo via API https://developer.github.com/v3/repos/#edit | require token have access of `public_repo`
local repoName=$1
curl -f -X PATCH https://api.github.com/repos/$GH_LOGIN/$repoName \
-H "Authorization: token $GH_TOKEN" \
-d "{\"name\": \"$repoName\", \"archived\": true}" > /dev/null && \
# NOTE name field is required
echo "repo $GH_LOGIN/$repoName has been archived\n"
}
ghDeleteRepo() { # NOTE https://developer.github.com/v3/repos/#edit | require token have access of `delete_repo`
local repoName=$1
curl -f -X DELETE https://api.github.com/repos/$GH_LOGIN/$repoName \
-H "Authorization: token $GH_TOKEN" > /dev/null &&\
echo "repo $GH_LOGIN/$repoName has been deleted\n"
}
ghStarRepo() { # https://developer.github.com/v3/activity/starring/#star-a-repository | NOTE you'll need to set Content-Length to zero when calling out to this endpoint
_getUpstreamFullName() { # NOTE it's not accessible outside of the function
local repoName=$1
local responseFile='repoInfo.json'
curl -f https://api.github.com/repos/$GH_LOGIN/$repoName -H "Authorization: token $GH_TOKEN" > $responseFile &&\
printf $(node -pe "require('./$responseFile').source.full_name") &&\
rm $responseFile
}
curl -f -X PUT https://api.github.com/user/starred/$(_getUpstreamFullName $1) -H "Authorization: token $GH_TOKEN" > /dev/null
echo 'star success'
}
ghUnStarRepo() { curl -f -X DELETE https://api.github.com/user/starred/$(_getUpstreamFullName $1) -H "Authorization: token $GH_TOKEN" > /dev/null; echo 'Unstar success'}
ghstarThenArc() { ghStarRepo $1 && ghArchiveRepo $1}
ghstarThenSync() { ghStarRepo $1 && ghSyncRepoWithUpstream $1}
ghstarThenDel() { ghStarRepo $1 && ghDeleteRepo $1}
# ===== nvm
# nvm commands to add node to $PATH
# nvm performance issue https://github.com/creationix/nvm/issues/860
# set default version manually avoid "nvm alias", "nvm use"
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" --no-use # just load nvm avoid nvm use
NODE_VERSION='v9.3.0'
export PATH="$NVM_DIR/versions/node/$NODE_VERSION/bin:$PATH" # insert path to override built-in node path
# ===== npm
alias npmi='npm i' # zsh plugin has npmS: 'npm i -S'
alias npmisd='npm i --D' # --save-dev
alias npminos='npm i --no-save' # npm@5
alias npminol='npm i --no-package-lock' # npm@5
alias npmlg='npm list -g --depth=0 2>/dev/null'
alias npml='npm list --depth=0 2>/dev/null'
alias npmod='npm outdated'
npmlgrep() { npm list --depth=0 2>/dev/null | grep ${1:-''} }
catPackageGrep() { cat package.json | grep ${1:-''} }
# ===== yarn
alias ya='yarn'
alias yai='yarn install'
alias yainolock='yarn install --no-lockfile'
alias yaipure='yarn install --pure-lockfile'
alias yaifrozlo='yarn install --frozen-lockfile'
alias yarun='yarn run'
alias yaa='yarn add'
alias yaad='yarn add --dev'
alias yaupg='yarn upgrade'
alias yaupglatest='yarn upgrade --latest'
alias yacc='yarn cache clean'
# ===== brew
alias br='brew'
alias brl='brew list'
alias bri='brew install'
alias bruni='brew uninstall'
alias bru='brew update'
alias brupg='brew upgrade'
alias brs='brew search'
alias brinfo='brew info'
alias brdep='brew deps' # list dependency
alias brd='brew doctor'
alias brc='brew cleanup' # cleanup old versions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.