c6dd4996b554a219ab7d91b6c7f267dcd85cf0ad
[dotfiles.git] / zsh / zshrc
1 #!/bin/zsh
2 #
3 # Darwin or Linux
4 OSNAME=`uname` 
5
6 # For Emacs Tramp mode
7 [[ $TERM == "dumb" ]] && unsetopt zle && PS1='$ ' && return
8
9 autoload colors
10 colors
11
12 for color in RED GREEN YELLOW BLUE MAGENTA CYAN WHITE; do
13 eval _$color='%{$terminfo[bold]$fg[${(L)color}]%}'
14 eval $color='%{$fg[${(L)color}]%}'
15 (( count = $count + 1 ))
16 done
17
18
19 # right prompt
20 #RPROMPT=$(echo "$RED%D %T$FINISH")
21 #PROMPT=$(echo "$CYAN%n[$BLUE%~]$_YELLOW>>$FINISH ")
22 PROMPT=$(echo "%B$CYAN%n$_YELLOW@$BLUE%<<[%~%<<]$_YELLOW>> $WHITE%b")
23
24 export HISTSIZE=10000
25 export SAVEHIST=10000
26 export HISTFILE="$HOME/.zsh_history"
27 setopt INC_APPEND_HISTORY
28 setopt HIST_IGNORE_DUPS
29 setopt EXTENDED_HISTORY
30
31 setopt AUTO_PUSHD       
32 setopt HIST_IGNORE_SPACE
33
34
35
36 export CLICOLOR=1
37 export LSCOLORS=ExFxCxDxBxegedabagaced
38
39         
40
41 man() {
42         env \
43                 GROFF_NO_SGR=$(printf "1")      \
44                 LESS_TERMCAP_mb=$(printf "\e[1;31m") \
45                 LESS_TERMCAP_md=$(printf "\e[1;31m") \
46                 LESS_TERMCAP_me=$(printf "\e[0m") \
47                 LESS_TERMCAP_se=$(printf "\e[0m") \
48                 LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
49                 LESS_TERMCAP_ue=$(printf "\e[0m") \
50                 LESS_TERMCAP_us=$(printf "\e[1;32m") \
51                         man "$@"
52 }
53
54
55
56 LS_COLORS="rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:"
57 #export LS_COLORS
58 ###====================
59 #
60 #autoload -U compinit
61 #compinit
62 #zstyle ':completion:*:descriptions' format '%U%B%d%b%u'
63 #zstyle ':completion:*:warnings' format '%BSorry, no matches for: %d%b'
64 #
65 #
66 #
67 # Use modern completion system
68 autoload -Uz compinit
69 compinit
70
71 zstyle ':completion:*' auto-description 'specify: %d'
72 zstyle ':completion:*' completer _expand _complete _correct _approximate
73 #zstyle ':completion:*' format 'Completing %d'
74 zstyle ':completion:*' group-name ''
75 zstyle ':completion:*' menu select=2
76 #eval "$(dircolors -b)"
77 zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
78 zstyle ':completion:*' list-colors ''
79 zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s
80 zstyle ':completion:*' matcher-list '' 'm:{a-z}={A-Z}' 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=* l:|=*'
81 zstyle ':completion:*' menu select=long
82 zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s
83 zstyle ':completion:*' use-compctl false
84 zstyle ':completion:*' verbose true
85
86 zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31'
87 zstyle ':completion:*:kill:*' command 'ps -u $USER -o pid,%cpu,tty,cputime,cmd'
88
89
90 ######################## zsh alias #########################
91
92 alias grep='grep --color -E'
93 #alias mvim='mvim --remote-silent'
94 #alias vim='/usr/local/bin/mvim -v'
95 alias tmux='tmux -2'
96
97 if [[ $OSNAME == 'Darwin' ]]; then
98     alias l='ls -l'
99     alias ll='ls -l'
100     alias la='ls -a'
101 elif [[ $OSNAME == 'Linux' ]]; then
102     alias l='ls -l --color=auto'
103     alias ll='ls -l --color=auto'
104     alias la='ls -a --color=auto'
105     alias ls='ls --color=auto'
106 fi
107
108 alias diff='colordiff'
109
110 alias mv='mv -i'
111 alias cp='cp -i'
112 alias ln='ln -i'
113
114 #alias svn='colorsvn'
115 if [[ $OSNAME == 'Darwin' ]]; then
116     [[ -f '/usr/local/bin/gsed' ]] || echo "zshrc warning: no gsed"
117 fi
118 [[ -f '/usr/local/bin/gsed' ]] && alias sed='/usr/local/bin/gsed'
119
120 ##### Git 
121 alias 'gs'='git status'
122 alias 'ga'='git add'
123 alias 'gc'='git commit'
124 alias 'gl'='git log  --oneline --graph --all --color --decorate --abbrev-commit'
125 alias 'git-log'='git log  --oneline --graph --all --color --decorate --abbrev-commit'
126 alias 'gdiff'='git diff'
127 alias 'gpush'='git push origin master'
128
129
130
131 ###########################  zsh completion ##########################
132 #
133 #autoload -U compinit
134 #compinit
135
136 #zstyle ':completion:*:descriptions' format '%U%B%d%b%u'
137 #zstyle ':completion:*:warnings' format '%BSorry, no matches for: %d%b'
138 #
139 #autoload -U compinit
140 #compinit
141 #
142 #autoload -U promptinit
143 #promptinit
144 #
145 #setopt correctall
146 #
147 #
148 zstyle ':completion:*' menu select
149 setopt AUTOLIST
150 setopt AUTOMENU
151
152
153 autoload -U compinit
154 compinit
155
156
157 WORDCHARS='*?_-[]~=&;!#$%^(){}<>'
158
159
160 ################
161 #
162 #http://techanic.net/2012/12/30/my_git_prompt_for_zsh.html
163 # Adapted from code found at <https://gist.github.com/1712320>.
164
165 setopt prompt_subst
166 autoload -U colors && colors # Enable colors in prompt
167
168 # Modify the colors and symbols in these variables as desired.
169 GIT_PROMPT_SYMBOL="%{$fg[blue]%}±"
170 GIT_PROMPT_PREFIX="%{$fg[green]%}[%{$reset_color%}"
171 GIT_PROMPT_SUFFIX="%{$fg[green]%}]%{$reset_color%}"
172 GIT_PROMPT_AHEAD="%{$fg[red]%}ANUM%{$reset_color%}"
173 GIT_PROMPT_BEHIND="%{$fg[cyan]%}BNUM%{$reset_color%}"
174 GIT_PROMPT_MERGING="%{$fg_bold[magenta]%}⚡︎%{$reset_color%}"
175 GIT_PROMPT_UNTRACKED="%{$fg_bold[red]%}•%{$reset_color%}"
176 GIT_PROMPT_MODIFIED="%{$fg_bold[yellow]%}•%{$reset_color%}"
177 GIT_PROMPT_STAGED="%{$fg_bold[green]%}•%{$reset_color%}"
178
179 # Show Git branch/tag, or name-rev if on detached head
180 parse_git_branch() {
181   (git symbolic-ref -q HEAD || git name-rev --name-only --no-undefined --always HEAD) 2> /dev/null
182 }
183
184 # Show different symbols as appropriate for various Git repository states
185 parse_git_state() {
186
187   # Compose this value via multiple conditional appends.
188   local GIT_STATE=""
189
190   local NUM_AHEAD="$(git log --oneline @{u}.. 2> /dev/null | wc -l | tr -d ' ')"
191   if [ "$NUM_AHEAD" -gt 0 ]; then
192     GIT_STATE=$GIT_STATE${GIT_PROMPT_AHEAD//NUM/$NUM_AHEAD}
193   fi
194
195   local NUM_BEHIND="$(git log --oneline ..@{u} 2> /dev/null | wc -l | tr -d ' ')"
196   if [ "$NUM_BEHIND" -gt 0 ]; then
197     GIT_STATE=$GIT_STATE${GIT_PROMPT_BEHIND//NUM/$NUM_BEHIND}
198   fi
199
200   local GIT_DIR="$(git rev-parse --git-dir 2> /dev/null)"
201   if [ -n $GIT_DIR ] && test -r $GIT_DIR/MERGE_HEAD; then
202     GIT_STATE=$GIT_STATE$GIT_PROMPT_MERGING
203   fi
204
205   if [[ -n $(git ls-files --other --exclude-standard 2> /dev/null) ]]; then
206     GIT_STATE=$GIT_STATE$GIT_PROMPT_UNTRACKED
207   fi
208
209   if ! git diff --quiet 2> /dev/null; then
210     GIT_STATE=$GIT_STATE$GIT_PROMPT_MODIFIED
211   fi
212
213   if ! git diff --cached --quiet 2> /dev/null; then
214     GIT_STATE=$GIT_STATE$GIT_PROMPT_STAGED
215   fi
216
217   if [[ -n $GIT_STATE ]]; then
218     echo "$GIT_PROMPT_PREFIX$GIT_STATE$GIT_PROMPT_SUFFIX"
219   fi
220
221 }
222
223 # If inside a Git repository, print its branch and state
224 git_prompt_string() {
225   local git_where="$(parse_git_branch)"
226   [ -n "$git_where" ] && echo "$GIT_PROMPT_SYMBOL$(parse_git_state)$GIT_PROMPT_PREFIX%{$fg[yellow]%}${git_where#(refs/heads/|tags/)}$GIT_PROMPT_SUFFIX"
227 }
228
229 # Set the right-hand prompt
230 RPS1='$(git_prompt_string)'
231
232 #source ~/Private/zsh-syntax-highlighting/zsh-syntax-highlighting.plugin.zsh
233 # [[ -f  "~/.zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" ]] && 
234 source ~/.zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
235 # http://stackoverflow.com/questions/3483604/which-shortcut-in-zsh-does-the-same-as-ctrl-u-in-bash
236 bindkey \^U backward-kill-line
237
238
239 export ZSHHOME="$HOME/.zsh"
240 [[ -f "$ZSHHOME/cpverc" ]] && . "$ZSHHOME/cpverc"
241 [[ -f "$ZSHHOME/eccrc" ]] && . "$ZSHHOME/eccrc"
242
243
244 #` git log --oneline --abbrev-commit --all --graph --decorate --color `
245 `which brew`  &> /dev/null
246 if [ $? -eq 0 ]; then
247         [[ -s `brew --prefix`/etc/autojump.sh ]] && . `brew --prefix`/etc/autojump.sh
248 fi
249
250 alias emacs='TERM=xterm-256color emacs -nw'
251
252 alias tw='open -a /Applications/TextWrangler.app/'
253
254 #
255 #function exists { which $1 &> /dev/null }
256 #
257 #if exists percol; then
258 #    function percol_select_history() {
259 #        local tac
260 #        exists gtac && tac="gtac" || { exists tac && tac="tac" || { tac="tail -r" } }
261 #        BUFFER=$(fc -l -n 1 | eval $tac | percol --query "$LBUFFER")
262 #        CURSOR=$#BUFFER         # move cursor
263 #        zle -R -c               # refresh
264 #    }
265 #
266 #    zle -N percol_select_history
267 #    bindkey '^R' percol_select_history
268 #fi
269 export LANG='en_US.UTF-8'
270 export LC_ALL="en_US.UTF-8"
271
272 lftp-cp() {
273
274     server='cmbu-ftp.cisco.com'
275     user='cmbu'
276     pass='cisco123!@#'
277     home='/FTPServer/users/peli3'
278     dir=`date +%m%d`
279
280     localfile=$1;
281     remotefile=${home}/${dir}/$2;
282
283     echo "copy ${localfile} to ftp://${server}${remotefile}"
284     lftp -e "mkdir ${home}/${dir}; put ${localfile} -o  ${remotefile}; bye" -u $user,$pass $server
285 }
286 merge_logs() {
287     ls -rl jabber.log*  | awk '{print $9}' | Xargs cat >> full.log
288 }
289
290 export MANPATH=$MANPATH:/usr/local/man
291
292 ## vim YouCompleteMe
293 # https://github.com/Valloric/YouCompleteMe/issues/18
294 #export DYLD_FORCE_FLAT_NAMESPACE=1
295
296 export MANPATH=$MANPATH:${HOME}/perl5/man
297 export PATH=$PATH:"${HOME}/scripts"
298 ## hunspell path
299 export DICPATH="/usr/local/share/hunspell/"
300
301
302
303
304 if [[ $OSNAME == 'Darwin' ]]; then
305     export PYTHONSTARTUP="$HOME/.pythonrc"
306     export PYTHONPATH="/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/"
307     export DEV_ENV_ROOT=$HOME/android/
308     export ANDROID_NDK_ROOT="${DEV_ENV_ROOT}/android-ndk-r10d/"
309     export ANDROID_SDK_ROOT="${DEV_ENV_ROOT}/android-sdk-macosx/"
310     #export ANDROID_TOOLCHAIN="${DEV_ENV_ROOT}/standalone-toolchain-api14/"
311     export ANDROID_NDK_HOME="$HOME/android/android-ndk-r10d"
312     export ANDROID_TOOLCHAIN="$ANDROID_NDK_HOME/standalone-toolchain-api14/"
313     export ANDROID_HOME="$HOME/android/android-sdk-macosx/"
314     export PATH=$PATH:"${ANDROID_TOOLCHAIN}/bin/":"${ANDROID_HOME}/platform-tools/"
315     export PATH="$HOME/bin":"/usr/local/bin":"/usr/local/sbin":$PATH
316     
317     
318     ## appended by cpan initializaion
319     PATH="$HOME/perl5/bin${PATH:+:${PATH}}"; export PATH;
320     PERL5LIB="$HOME/perl5/lib/perl5${PERL5LIB:+:${PERL5LIB}}"; export PERL5LIB;
321     PERL_LOCAL_LIB_ROOT="$HOME/perl5${PERL_LOCAL_LIB_ROOT:+:${PERL_LOCAL_LIB_ROOT}}"; export PERL_LOCAL_LIB_ROOT;
322     PERL_MB_OPT="--install_base \"$HOME/perl5\""; export PERL_MB_OPT;
323     PERL_MM_OPT="INSTALL_BASE=$HOME/perl5"; export PERL_MM_OPT;
324 fi
325
326 # add Racket into PATH
327 export PATH="/Applications/Racket v6.6/bin":$PATH;
328 export MANPATH="/Applications/Racket v6.6/man":$MANPATH;
329
330 # export ~/.bin into PATH
331 export PATH="$HOME/.bin":$PATH;
332
333
334 # source Android bin
335 [[ -f "$ZSHHOME/androidrc" ]] && . "$ZSHHOME/androidrc"
336
337 if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
338 export PYENV_ROOT="$HOME/.pyenv"
339 export PATH="$PYENV_ROOT/bin:$PATH"
340
341 # tmux variable
342 export TMUX_IP=`echo $SSH_CONNECTION | awk '{print $3}'`
343 if [[ $OSNAME == 'Darwin' ]]; then
344     ip=`ifconfig | grep "inet .*broadcast" | head -1 | awk '{print $2}'`
345     [[ $TMUX_IP = "" ]] && TMUX_IP=$ip
346 else
347     ip=`ip addr | grep 'inet .*global' | head -1 | sed 's/^.*inet //g; s/\/.*$//g'`
348     [[ $TMUX_IP = "" ]] && TMUX_IP=$ip
349 fi
350