zshrc - Fix prompt
[home/.git] / .zshrc
1 PATH=~/bin${PATH:+:}$PATH
2
3 SAVEHIST=5000
4 HISTSIZE=8000
5 HISTFILE=~/.zshhistory
6 setopt SHARE_HISTORY
7 setopt EXTENDED_HISTORY
8 setopt HIST_EXPIRE_DUPS_FIRST
9 #setopt HIST_FIND_NO_DUPS
10 setopt HIST_REDUCE_BLANKS
11 setopt PUSHD_IGNORE_DUPS
12
13 autoload -U colors; colors
14
15 typeset -A title
16 title[start]="\e]0;"
17 title[end]="\a"
18
19
20 if [[ $TERM == (xterm|screen)* && $oldterm != $TERM$WINDOWID ]]; then
21         SHLVL=1
22         export oldterm=$TERM$WINDOWID
23 fi
24
25 REPORTTIME=1
26 TIMEFMT="%E=%U+%S"
27
28 #PS1="%(?..%{${fg_bold[red]}%}%?%{$reset_color%} )%(2L.%{${fg_bold[yellow]}%}<%L>%{$reset_color%} .)%B%(#.%{${bg[red]}%}.)%m %(#..%{$fg[green]%})%#%{$reset_color%}%b "
29 #RPS1=" %{${fg_bold[green]}%}%~%{${fg_bold[black]}%}|%{${fg_bold[blue]}%}%(t.DING!.%*)%{$reset_color%}"
30
31 setopt PROMPT_PERCENT
32 # Substitute vars.  Notice that we're in "", so $vars here are
33 # replaced already here.  All dynamic content needs to go
34 # in escaped \$vars.
35 #setopt PROMPT_SUBST
36 function prompt_generate {
37         PS1=""
38         # time
39         prompt_time
40         # switch to red background when root
41         #PS1="$PS1%(#.%{${bg[red]}%}.)"
42         # hostname
43         PS1="$PS1%B%m%b:"
44         # path
45         PS1="$PS1%{${fg_bold[black]}%}%~%b "
46         # git dynamic content, i.e. branch name
47         prompt_git
48         # history number
49         #PS1="$PS1%{${fg[magenta]}%}!%!$reset_color "
50         # jobs display
51         prompt_jobs
52         # shell nesting
53         PS1="$PS1%(2L.%{${fg_bold[yellow]}%}<%L>%{$reset_color%} .)"
54         # tty name
55         #PS1="$PS1%l "
56         # start second line
57         PS1="$PS1
58 "
59         # If we're in paste mode, forget all the fancyness and only do
60         # the basic prompt.
61         #if [[ -n $paste_mode ]] {
62         #       PS1=""
63         #}
64         # return value
65         PS1="$PS1%(?..%{${fg_bold[red]}%}%?%{$reset_color%} )"
66         # prompt!
67         #PS1="$PS1%(#.%{${fg[green]%}}%B.%%%{$reset_color%} "
68         #PS1="$PS1%(..%{$fg[green]%}%%) %{$reset_color%}"
69         #PS1="$PS1%(#.%{${fg[red]%}%B.%{$fg[green]%})%# %{$reset_color%}"
70          PS1="$PS1%(!.%{$fg_bold[red]%}.%{$fg[green]%})%# %{$reset_color%}"
71 }
72
73 typeset -g lastding
74 function prompt_time {
75         local t
76         local -a curtime
77         local hour
78         local minute
79
80         curtime=(${(@s,:,)$(print -Pn "%D{%H:%M}")})
81         hour=$curtime[0]
82         minute=$curtime[2]
83
84         t="%*"
85         if [[ $minute -eq 0 && $lastding -ne $hour ]]; then
86                 lastding=$hour
87                 # not using ^G here, because then cat'ing
88                 # this file will beep.
89                 t="DING!$(printf '\a')   "
90         fi
91         PS1="$PS1%{${fg_bold[blue]}%}$t%{$reset_color%} "
92 }
93
94 function prompt_git {
95         local ref
96
97         ref=$(git symbolic-ref HEAD 2>/dev/null)
98         ref=${ref#refs/heads/}
99         if [[ -n $ref ]] {
100                 # real symbolic ref
101                 PS1="$PS1%{${fg[cyan]}%}$ref%{$reset_color%} "
102                 return
103         }
104
105         # maybe detached?
106         ref=$(git rev-parse HEAD 2>/dev/null)
107         ref=${ref[0,10]}
108         if [[ -n $ref ]] {
109                 # detached head, print commitid
110                 PS1="$PS1%{${bg[cyan]}${fg[black]}%}$ref%{$reset_color%} "
111                 return
112         }
113 }
114
115 function prompt_jobs {
116         # from http://www.miek.nl/blog/archives/2008/02/20/my_zsh_prompt_setup/index.html
117         local js
118         local jobno
119
120         js=()
121         for jobno (${(k)jobstates}) {
122                 local fullstate=$jobstates[$jobno]
123                 local state="${${(@s,:,)fullstate}[2]}"
124                 js+=($jobno${state//[^+-]/})
125         }
126         if [[ $#js -gt 0 ]]; then
127                 PS1="$PS1%{${fg[yellow]}%}[${(j:,:)js}]%{$reset_color%} "
128         fi
129 }
130
131 function precmd {
132         title_generate
133         prompt_generate
134 }
135
136 function title_generate {}
137
138 if [[ $TERM == (xterm|screen)* ]]; then
139         function title_generate {
140                 print -Pn "${title[start]}%n@%m:%~${title[end]}"
141         }
142
143         function preexec {
144                 emulate -L zsh
145                 local -a cmd; cmd=(${(z)1})
146                 local -a checkjobs
147
148                 case $cmd[1] in
149                 fg|wait)
150                         if (( $#cmd == 1 ))
151                         then
152                                 checkjobs=%+
153                         else
154                                 checkjobs=$cmd[2]
155                         fi
156                         ;;
157                 %*)
158                         checkjobs=$cmd[1]
159                         ;;
160                 esac
161
162                 print -n "${title[start]}"
163
164                 if [[ -n "$checkjobs" ]]
165                 then
166                         # from: http://www.zsh.org/mla/workers/2000/msg03990.html
167                         local -A jt; jt=(${(kv)jobtexts})       # Copy jobtexts for subshell
168                         builtin jobs -l $checkjobs 2>/dev/null >>(read num rest
169                                 cmd=(${(z)${(e):-\$jt$num}})
170                                 print -nr "$cmd")
171                 else
172                         print -nr "$cmd"
173                 fi
174
175                 print -Pn " | %* | "
176                 print -Pn "%n@%m:%~"
177                 print -n "${title[end]}"
178         }
179 fi
180
181 if which todo >/dev/null 2>&1; then
182         function chpwd {
183                 # only print stuff in the interactive case
184                 [[ ! -o interactive ]] && return
185
186                 todo
187         }
188 fi
189
190 if [[ -r ~/.aliasrc ]]; then
191         source ~/.aliasrc
192 fi
193
194 umask 22
195
196 if which vim >/dev/null 2>&1; then
197         export EDITOR=`which vim`
198 fi
199 export PAGER=less
200 export BLOCKSIZE=K
201 lesspipe=$(which lesspipe.sh 2>/dev/null) || \
202 lesspipe=$(which lesspipe 2>/dev/null)
203 if test -n "$lesspipe"; then
204         export LESSOPEN="|$lesspipe %s"
205 fi
206
207 if which keychain >/dev/null 2>&1; then
208         [ -f ~/.ssh/id_rsa ] && keychain -q id_rsa --nogui
209         [ -f ~/.ssh/id_dsa ] && keychain -q id_dsa --nogui
210         source ~/.keychain/$HOST-sh
211 fi
212
213 bindkey -e
214 bindkey '\e[H' beginning-of-line
215 bindkey '\e[F' end-of-line
216 bindkey '\eOH' beginning-of-line
217 bindkey '\eOF' end-of-line
218 bindkey '\e[1~' beginning-of-line
219 bindkey '\e[4~' end-of-line
220 autoload -U down-line-or-beginning-search
221 autoload -U up-line-or-beginning-search
222 zle -N down-line-or-beginning-search
223 zle -N up-line-or-beginning-search
224 bindkey '\e[B' down-line-or-beginning-search
225 bindkey '\e[A' up-line-or-beginning-search
226 autoload run-help
227 bindkey '\eOP' run-help
228 bindkey '\e[M' run-help
229 bindkey '\e[1;5D' backward-word
230 bindkey '\e[1;5C' forward-word
231 bindkey '\e[3~' delete-char
232 #WORDCHARS=${WORDCHARS//[\/&.;=]}
233 autoload -U select-word-style
234 select-word-style bash
235 zstyle ':zle:transpose-words' word-style shell
236 setopt NO_FLOW_CONTROL
237
238 autoload -U compinit; compinit
239
240 if [[ -r ~/.zshrc.local ]]; then
241         source ~/.zshrc.local
242 fi
243
244 cd .