Advertisement
truongngoclinh

leo zshrc

Aug 28th, 2016
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 14.65 KB | None | 0 0
  1. # General
  2. export LANG=en_US.UTF-8
  3. export LC_CTYPE=en_US.UTF-8
  4. export LC_ALL=en_US.UTF-8
  5. # Leo's Preferences home folder
  6. LEO_PREF_PATH=~/leospreferences
  7. LEO_UNIX_PREF_PATH=$LEO_PREF_PATH/Unix
  8. # disable ctrl-D closes terminal: http://unix.stackexchange.com/questions/139115/disable-ctrl-d-window-close-in-terminator-terminal-emulator
  9. set -o ignoreeof
  10. # default editors
  11. export EDITOR='nvim'
  12. export VISUAL='nvim'
  13. # Path to your oh-my-zsh installation.
  14. export FZF_HOME=$LEO_UNIX_PREF_PATH/fzf
  15. export ZSH=$LEO_UNIX_PREF_PATH/\[dot\]oh-my-zsh # DO NOT change variable name "$ZSH" to anything else
  16. export MANPATH="/usr/local/man:$MANPATH"
  17. export SSH_KEY_PATH=$HOME/.ssh/dsa_id
  18. # zsh custom folder
  19. export ZSH_CUSTOM=$ZSH/custom # DO NOT change "ZSH_CUSTOM" to anything else
  20. # Set name of the theme to load. Look in ~/.oh-my-zsh/themes/
  21. # Optionally, if you set this to "random", it'll load a random theme eachtime that oh-my-zsh is loaded.
  22. ZSH_THEME="bullet-train"
  23. # 12h mode for zsh's bullet train theme
  24. BULLETTRAIN_TIME_12HR="true"
  25. # Disable case sensitive
  26. CASE_SENSITIVE="false"
  27. # Disable correction
  28. ENABLE_CORRECTION="false"
  29. # Dissplay red dots whilst waiting for completion.
  30. COMPLETION_WAITING_DOTS="true"
  31. # History timestamp. The optional three formats: "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
  32. HIST_STAMPS="yyyy-mm-dd"
  33. # Highlight everything
  34. ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern cursor)
  35.  
  36.  
  37.  
  38. ########################################## oh-my-zsh ##############################################
  39. # Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
  40. # Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
  41. # Example format: plugins=(rails git textmate ruby lighthouse)
  42. # Add wisely, as too many plugins slow down shell startup.
  43. #plugins=(git tmux)
  44. plugins=(zsh_reload git adb colored-man zsh-syntax-highlighting gradle battery osx)
  45. # source
  46. source "$ZSH/oh-my-zsh.sh"
  47. ##################################################################################################
  48.  
  49.  
  50.  
  51. ####################################### Leo's configuration #######################################
  52. ### Leo's own Mac
  53. # PATHS
  54. export PATH=$PATH:/Users/leolink/Documents/DevelopmentTools/Android/sdk/platform-tools
  55. export PATH=$PATH:/Users/leolink/Documents/DevelopmentTools/sdk/tools
  56. export PATH=$PATH:/usr/local/bin
  57. export PATH=$PATH:$LEO_PREF_PATH/Git/git-diff-highlight/
  58. export GROOVY_HOME=/usr/local/opt/groovy/libexec
  59. # ALIASES
  60. # Utilities
  61. alias ll='ls -l'
  62. alias lla='ls -la'
  63. alias skype='sudo /Applications/Skype.app/Contents/MacOS/Skype'
  64. alias fuck='sudo'
  65. alias ggd="cd ~/Google\ Drive"
  66. alias leospref="cd $LEO_PREF_PATH"
  67. alias dropbox='cd ~/Dropbox'
  68. alias desktop='cd ~/Desktop'
  69. # VIM
  70. alias vi='nvim'
  71. alias vim='nvim'
  72. alias vimdiff='nvim -d' # E.g. vimdiff file1.java file2.java
  73. alias vimconf='vi ~/.config/nvim/init.vim'
  74. # SUBLIME TEXT
  75. alias subl="/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl"
  76. # ZSHRC
  77. alias zconf='vi ~/.zshrc'
  78. alias zreload='rm -f ~/.zcompdump* & src'
  79. alias zrestart='exec zsh'
  80. alias -s txt=vim
  81. alias -s rb=vim
  82. alias -s py=vim
  83. alias -s xml=vim
  84. alias -s json=vim
  85. alias -s JSON=vim
  86. alias -s gradle=vim
  87. alias -s md=vim
  88. alias -s java=vim
  89. alias -s h=vim
  90. alias -s m=vim
  91. alias -s c=vim
  92. alias -s cpp=vim
  93. # JAVA
  94. alias javahome='/usr/libexec/java_home -V'
  95. # TMUX
  96. # Automatically start a tmux session upon logging in
  97. #ZSH_TMUX_AUTOSTART="true"
  98. #ZSH_TMUX_AUTOCONNECT="false"
  99. #alias tconf='vi ~/.tmux.conf'
  100. #alias treload='tmux source-file ~/.tmux.conf'
  101. #alias tkw='tmux kill-window -t' # + name
  102. #alias tlkeys='tmux list-keys'
  103. #alias tls='tmux ls'
  104. #alias tns='tmux new-session -s'
  105. #alias tas='tmux attach -t' # + name
  106. #alias tks='tmux kill-session -t' # + name
  107. # ANDROID
  108. alias adb_foreground_activity='adb shell dumpsys window windows | grep -E "mCurrentFocus|mFocusedApp"'
  109. alias adb_kill_app='adb shell am force-stop'
  110. # Octave
  111. alias octave=$'/usr/local/octave/3.8.0/bin/octave-3.8.0 --persist --eval \'setenv("GNUTERM","qt")\'' # dollar sign is amazing: http://stackoverflow.com/questions/1250079/how-to-escape-single-quotes-within-single-quoted-strings
  112. # Ruby
  113. alias workspace-ruby='~/Dropbox/workspace-Ruby'
  114. # Python
  115. alias python='python3'
  116. alias pip='pip3.5'
  117. # less
  118. alias less='less -R -s -M +Gg -F -X' # http://stackoverflow.com/questions/1049350/unable-to-make-less-to-indicate-location-in-percentage
  119. # ag (The Silver Searcher)
  120. alias ag='ag --pager "less -R"'
  121. # tig
  122. # prevent ctrl-y from suspending tig: https://github.com/jonas/tig/issues/214
  123. stty dsusp undef
  124. alias tigconf='vi ~/.tigrc'
  125. # git
  126. alias gitconf='vi ~/.gitconfig'
  127. alias ga='git add'
  128. alias gaa='git add -A'
  129. alias gs='git status'
  130. alias gss='tig status'
  131. alias gl='tig'
  132. alias gla='tig --all'
  133. alias gd='git diff'
  134. alias gdt='git difftool'
  135. alias gdty='git difftool -y'
  136. alias gdta='git difftool -y'
  137. alias gca='git commit --amend'
  138. alias gcm='git commit -m'
  139. alias gsno='git show --name-only'
  140. alias gt='git tag'
  141. alias gta='git tag -a'
  142. alias gck='git checkout'
  143. alias gckb='git checkout -b'
  144. alias gco='git checkout --ours'
  145. alias gct='git checkout --theirs'
  146. alias gbd='git branch -d'
  147. alias gbD='git branch -D'
  148. alias gsl='git stash list'
  149. alias gp='git fetch origin --prune && git merge --ff-only `git rev-parse --abbrev-ref --symbolic-full-name @{u}`' # avoid ugly graph when pull: https://adamcod.es/2014/12/10/git-pull-correct-workflow.html
  150. function git_history() {
  151.    fzf_res=`ag -g "" | fzf`
  152.    if [ "$fzf_res" ]; then
  153.        tig log $fzf_res
  154.    fi
  155. }
  156. alias gh='git_history'
  157.  
  158. ### Work's Mac
  159. # PATHS
  160. export STUDIO_VM_OPTIONS=$LEO_PREF_PATH/Android\ Studio/studio_for_smooth_font_on_thunderbolt_monitor.vmoptions
  161. export STUDIO_JDK=/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk
  162. export PATH=$PATH:/Users/linh.le/DeveloperTools/android-sdk-macosx/platform-tools
  163. export PATH=$PATH:/Users/linh.le/DeveloperTools/android-sdk-macosx/tools
  164. export JAVA_HOME=`/usr/libexec/java_home -v "1.8" -F`
  165. export JAVA7_HOME=`/usr/libexec/java_home -v "1.7" -F`
  166. export JAVA8_HOME=`/usr/libexec/java_home -v "1.8" -F`
  167. launchctl setenv JAVA7_HOME `/usr/libexec/java_home -v "1.7" -F`
  168. launchctl setenv JAVA8_HOME `/usr/libexec/java_home -v "1.8" -F`
  169. launchctl setenv ICHIBA_STORE_PASS "device"
  170. launchctl setenv ICHIBA_ALIAS "gw"
  171. launchctl setenv ICHIBA_ALIAS_PASS "device"
  172. export ICHIBA_STORE_PASS="device"
  173. export ICHIBA_ALIAS="gw"
  174. export ICHIBA_ALIAS_PASS="device"
  175. # ALIASES
  176. alias workspace='cd ~/Workspace'
  177. alias ws='cd ~/Workspace'
  178. ###################################################################################################
  179.  
  180.  
  181.  
  182. ############################################# UPDATE ##############################################
  183. ## CONSTANTS
  184. FILE_LAST_UPDATE="$HOME/.zsh-leos-config-last-update"
  185. UPDATE_INTERVAL=604800 # a week
  186. ## CHECK
  187. function should_update_leos_config() {
  188.     if [ ! -f $FILE_LAST_UPDATE ] # if file doesn't exist
  189.     then
  190.         echo "$FILE_LAST_UPDATE doesn't exist!"
  191.         local now=$(date +%s) # get number of seconds since the epoch
  192.         echo $now >> $FILE_LAST_UPDATE
  193.         return 0
  194.     else
  195.         local last_update=$(head -n 1 $FILE_LAST_UPDATE) # get last update
  196.         # validate
  197.         if ! [[ "$last_update" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
  198.             rm -rf $FILE_LAST_UPDATE
  199.             echo "Failed to validate $FILE_LAST_UPDATE, removing it..."
  200.             return 0
  201.         fi
  202.  
  203.         local now=$(date +%s) # get number of seconds since the epoch
  204.         local elapsed=$(expr $now - $last_update)
  205.         if [ $elapsed -gt $UPDATE_INTERVAL ] # if update is stale
  206.         then
  207.             echo "It's been a while since last update..."
  208.             echo $now > $FILE_LAST_UPDATE
  209.             return 0
  210.         else
  211.             echo "[UPDATE LEO'S TERMINAL APPS]\nNo need to update!"
  212.             return 1
  213.         fi
  214.     fi
  215. }
  216. ## UPDATE
  217. function update_leos_config() {
  218.     # save current directory
  219.     local cwd="$(pwd)"
  220.  
  221.     # update git's plugins
  222.     tput setaf 1; tput bold; echo "=====>> [UPDATING] Git's plugins..."; tput sgr0
  223.     wget -O "$LEO_PREF_PATH/Git/hub/hub.zsh_completion" "https://raw.githubusercontent.com/github/hub/master/etc/hub.zsh_completion"
  224.     wget -O "$LEO_PREF_PATH/Git/git-diff-highlight/diff-highlight" "https://raw.githubusercontent.com/git/git/master/contrib/diff-highlight/diff-highlight"
  225.     wget -O "$LEO_PREF_PATH/Git/git-diff-highlight/diff-so-fancy" "https://raw.githubusercontent.com/so-fancy/diff-so-fancy/master/diff-so-fancy"
  226.     wget -O "$LEO_PREF_PATH/Git/git-diff-highlight/libexec/diff-so-fancy.pl" "https://raw.githubusercontent.com/so-fancy/diff-so-fancy/master/libexec/diff-so-fancy.pl"
  227.     chmod +x "$LEO_PREF_PATH/Git/git-diff-highlight/diff-highlight"
  228.     chmod +x "$LEO_PREF_PATH/Git/git-diff-highlight/diff-so-fancy"
  229.     chmod +x "$LEO_PREF_PATH/Git/git-diff-highlight/libexec/diff-so-fancy.pl"
  230.  
  231.     # update fzf
  232.     tput setaf 1; tput bold; echo "=====>> [UPDATING] fzf..."; tput sgr0
  233.     cd $FZF_HOME && git pull && ./install --all
  234.  
  235.     # update tldr cache
  236.     tput setaf 1; tput bold; echo "\n\n=====>> [UPDATING] tldr's cache..."; tput sgr0
  237.     tldr --update
  238.     if [ $? -eq 0 ]
  239.     then
  240.         echo "tldr's cache updated!"
  241.     fi
  242.  
  243.     # update oh-my-zsh
  244.     tput setaf 1; tput bold; echo "\n\n=====>> [UPDATING] oh-my-zsh..."; tput sgr0
  245.     upgrade_oh_my_zsh
  246.  
  247.     # update zsh-syntax-highlighting
  248.     tput setaf 1; tput bold; echo "\n\n=====>> [UPDATING] zsh-syntax-highlighting..."; tput sgr0
  249.     cd "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting"
  250.     git pull
  251.     cd $cwd # cd back to current directory
  252.  
  253.     # upgrade brew
  254.     tput setaf 1; tput bold; echo "\n\n=====>> [UPDATING] home brew..."; tput sgr0;
  255.     brew update
  256.     brew upgrade
  257.  
  258.     ## update vim's pathogen's bundles
  259.     tput setaf 1; tput bold; echo "\n\n=====>> [UPDATING] Vim's pathogen's bundles..."; tput sgr0;
  260.     # update Pathogen itself
  261.     echo "Updating $(tput bold)Pathogen$(tput sgr0)..."
  262.     PATHOGEN_UPDATE_URL="https://tpo.pe/pathogen.vim"
  263.     curl -LSso "$HOME/.vim/autoload/pathogen.vim" $PATHOGEN_UPDATE_URL
  264.     if [ $? -eq 0 ]
  265.     then
  266.         echo "Updated successfully from $PATHOGEN_UPDATE_URL"
  267.     fi
  268.     # update bundles
  269.     cd ~/.vim/bundle
  270.     for i in `ls`; do
  271.         echo "\nUpdating $(tput bold)$i$(tput sgr0)..."
  272.         cd "$i"
  273.         git clean -df
  274.         git reset --hard HEAD
  275.         git pull
  276.         # update vim help
  277.         vim -u NONE -c "helptags ./doc" -c q
  278.         cd ..
  279.     done
  280.     # cd back to current directory
  281.     cd $cwd
  282.  
  283.     ## DONE
  284.     tput setaf 1; tput bold; echo "\n=====>> DONE!!!\nHave a nice day!"; tput sgr0;
  285.     zreload
  286. }
  287. if should_update_leos_config
  288. then
  289.     update_leos_config
  290. fi
  291. ## CLEAN UP
  292. unset FILE_LAST_UPDATE
  293. unset UPDATE_INTERVAL
  294. ###################################################################################################
  295.  
  296.  
  297.  
  298. ########################################## Scripts ################################################
  299. # Colorful man pages
  300. export LESS_TERMCAP_mb=$'\E[01;31m'
  301. export LESS_TERMCAP_md=$'\E[01;31m'
  302. export LESS_TERMCAP_me=$'\E[0m'
  303. export LESS_TERMCAP_se=$'\E[0m'
  304. export LESS_TERMCAP_so=$'\E[01;47;34m'
  305. export LESS_TERMCAP_ue=$'\E[0m'
  306. export LESS_TERMCAP_us=$'\E[01;32m'
  307. export LESS=-r
  308.  
  309. # ZSH: Redefine prompt_context for hiding user@hostname
  310. prompt_context () { }
  311.  
  312. # fzf
  313. [ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
  314. FZF_DEFAULT_COMMAND='ag -g ""' # not using export so vim will use 'find' instead of ag, use ag to respect .gitignore, .hgignore, and svn:ignore
  315. export FZF_DEFAULT_OPTS='--color=fg:202,hl:118,hl+:82,info:45,pointer:255,prompt:15' # color ref: http://www.calmar.ws/vim/256-xterm-24bit-rgb-color-chart.html, customizable properties: "man fzf"
  316. export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
  317. # fix FZF's function definition file not found: https://github.com/junegunn/fzf/issues/553
  318. alias fixfzf='rm -f ~/.zcompdump*'
  319. # fbr - checkout git branch
  320. fbr() {
  321.     local branches branch
  322.     branches=$(git branch -vv) &&
  323.     branch=$(echo "$branches" | fzf +m --no-mouse) &&
  324.     git checkout $(echo "$branch" | awk '{print $1}' | sed "s/.* //")
  325. }
  326. # fba - checkout git branch (including remote branches)
  327. fba() {
  328.     local branches branch
  329.     branches=$(git branch --all | grep -v HEAD) &&
  330.     branch=$(echo "$branches" | fzf-tmux -d $(( 2 + $(wc -l <<< "$branches") )) +m --no-mouse) &&
  331.     git checkout $(echo "$branch" | sed "s/.* //" | sed "s#remotes/[^/]*/##")
  332. }
  333. # fco - checkout git branch/tag
  334. fco() {
  335.   local tags branches target
  336.   tags=$(
  337.     git tag | awk '{print "\x1b[31;1mtag\x1b[m\t" $1}') || return
  338.   branches=$(
  339.     git branch --all | grep -v HEAD             |
  340.     sed "s/.* //"    | sed "s#remotes/[^/]*/##" |
  341.     sort -u          | awk '{print "\x1b[34;1mbranch\x1b[m\t" $1}') || return
  342.   target=$(
  343.     (echo "$branches"; echo "$tags") |
  344.     fzf-tmux -l30 -- --no-hscroll --ansi +m --no-mouse -d "\t" -n 2) || return
  345.   git checkout $(echo "$target" | awk '{print $2}')
  346. }
  347. # fzf + git tag
  348. fgt() {
  349.     local tags tag
  350.     tags=$(git tag) &&
  351.     tag=$(echo "$tags" | fzf-tmux -d $(( 2 + $(wc -l <<< "$tags") )) +m --no-mouse) &&
  352.     git checkout $(echo "$tag")
  353. }
  354.  
  355. alias gb='fbr'
  356. alias gba='fba'
  357. alias gbt='fco'
  358. alias gt='fgt'
  359.  
  360. # hub (https://github.com/github/hub)
  361. fpath=("$LEO_PREF_PATH/Git/hub/hub.zsh_completion" $fpath)
  362. autoload -U compinit && compinit
  363.  
  364. # Key Binding
  365. bindkey '^R' fzf-history-widget
  366. bindkey '^T' fzf-file-widget
  367.  
  368. # Update leo's preferences
  369. function update_leos_preferences() {
  370.     # save current dir
  371.     local cwd="$(pwd)"
  372.     # cd to $LEO_PREF_PATH
  373.     cd $LEO_PREF_PATH
  374.     git remote update # update remote refs: http://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
  375.     local LEOS_PREF_GIT_LOCAL=$(git rev-parse @)
  376.     local LEOS_PREF_GIT_REMOTE=$(git rev-parse @{u})
  377.     local LEOS_PREF_GIT_BASE=$(git merge-base @ @{u})
  378.     if [ $LEOS_PREF_GIT_LOCAL = $LEOS_PREF_GIT_REMOTE ]; then
  379.         echo "Up-to-date"
  380.     elif [ $LEOS_PREF_GIT_LOCAL = $LEOS_PREF_GIT_BASE ]; then
  381.         git pull
  382.         # only reload if git pull suceeded
  383.         if [ $? -eq 0 ]
  384.         then
  385.             zreload
  386.         else
  387.             git status
  388.         fi
  389.     elif [ $LEOS_PREF_GIT_REMOTE = $LEOS_PREF_GIT_BASE ]; then # TODO: fix this case, sometimes it leads to infinite recursive calls
  390.         git pull
  391.         # only reload if git pull suceeded
  392.         if [ $? -eq 0 ]
  393.         then
  394.             zreload
  395.         else
  396.             git status
  397.         fi
  398.     else
  399.         echo "$LEO_PREF_PATH is diverged!"
  400.         git status
  401.     fi
  402.     # cd back
  403.     cd $cwd
  404. }
  405. echo "[UPDATE LEO'S PREFERENCES]"
  406. update_leos_preferences
  407. ###################################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement