zsh 配置文件

原文:http://forum.ubuntu.org.cn/viewtopic.php?f=95&t=138936

~/.zshrc:

  1 #color{{{
  2 autoload colors zsh/terminfo
  3 if [[ "$terminfo[colors]" -ge 8 ]]; then
  4 colors
  5 fi
  6 for color in RED GREEN YELLOW BLUE MAGENTA CYAN WHITE; do
  7 eval _$color='%{$terminfo[bold]$fg[${(L)color}]%}'
  8 eval $color='%{$fg[${(L)color}]%}'
  9 (( count = $count + 1 ))
 10 done
 11 FINISH="%{$terminfo[sgr0]%}"
 12 #}}}
 13 
 14 #命令提示符 {{{
 15 precmd () {
 16 local count_db_wth_char=${#${${(%):-%/}//[[:ascii:]]/}}
 17 local leftsize=${#${(%):-%M%/}}+$count_db_wth_char
 18 local rightsize=${#${(%):-%D %T }}
 19 HBAR=" -"
 20  
 21 FILLBAR="\${(l.(($COLUMNS - ($leftsize + $rightsize +2)))..${HBAR}.)}"
 22  
 23 RPROMPT=$(echo "%(?..$RED%?$FINISH)")
 24 PROMPT=$(echo "$BLUE%M$GREEN%/ $WHITE${(e)FILLBAR} $MAGENTA%D %T$FINISH
 25 $CYAN%n $_YELLOW>>>$FINISH ")
 26  
 27 #在 Emacs终端 中使用 Zsh 的一些设置
 28 if [[ "$TERM" == "dumb" ]]; then
 29 setopt No_zle
 30 PROMPT='%n@%M %/
 31 >>'
 32 alias ls='ls -F'
 33 fi    
 34 }
 35 #}}}
 36 
 37 #标题栏、任务栏样式{{{
 38 case $TERM in (*xterm*|*rxvt*|(dt|k|E)term)
 39    preexec () { print -Pn "\e]0;%n@%M//%/\ $1\a" }
 40    ;;
 41 esac
 42 #}}}
 43 
 44 #关于历史纪录的配置 {{{
 45 #历史纪录条目数量
 46 export HISTSIZE=10000
 47 #注销后保存的历史纪录条目数量
 48 export SAVEHIST=10000
 49 #历史纪录文件
 50 #export HISTFILE=~/.zhistory
 51 #以附加的方式写入历史纪录
 52 setopt INC_APPEND_HISTORY
 53 #如果连续输入的命令相同,历史纪录中只保留一个
 54 setopt HIST_IGNORE_DUPS      
 55 #为历史纪录中的命令添加时间戳      
 56 setopt EXTENDED_HISTORY      
 57  
 58 #启用 cd 命令的历史纪录,cd -[TAB]进入历史路径
 59 setopt AUTO_PUSHD
 60 #相同的历史路径只保留一个
 61 setopt PUSHD_IGNORE_DUPS
 62  
 63 #在命令前添加空格,不将此命令添加到纪录文件中
 64 #setopt HIST_IGNORE_SPACE      
 65 #}}}
 66 
 67 #每个目录使用独立的历史纪录{{{
 68 HISTDIR="$HOME/.zhistory"
 69     [[ ! -d "$HISTDIR" ]] && mkdir -p "$HISTDIR"
 70 HISTFILE="$HISTDIR/${PWD//\//:}"
 71 chpwd() {
 72 #   fc -W                                       # write current history  file
 73 #   "setopt INC_APPEND_HISTORY"
 74     HISTFILE="$HISTDIR/${PWD//\//:}"            # set new history file
 75     [[ ! -e "$HISTFILE" ]] && touch $HISTFILE
 76     local ohistsize=$HISTSIZE
 77         HISTSIZE=0                              # Discard previous dir's history
 78         HISTSIZE=$ohistsize                     # Prepare for new dir's history
 79     fc -R                                       # read from current histfile
 80 }
 81  
 82 function allhistory { cat $HISTDIR/* }                                   #*/
 83 function convhistory {
 84             sort $1 | sed 's/^:\([ 0-9]*\):[0-9]*;\(.*\)/\1::::::\2/' |
 85             awk -F"::::::" '{ $1=strftime("%Y-%m-%d %T",$1) "|"; print }'  
 86 }
 87 #使用 histall 命令查看全部历史纪录
 88 function histall { convhistory =(allhistory) |
 89             sed '/^.\{20\} *cd/i\\' }
 90 #使用 hist 查看当前目录历史纪录
 91 function hist { convhistory $HISTFILE }
 92  
 93 #全部历史纪录 top55
 94 function top55 { allhistory | awk -F':[ 0-9]*:[0-9]*;' '{ $1="" ; print }' | sed 's/ /\n/g' | sed '/^$/d' | sort | uniq -c | sort -nr | head -n 55 }
 95  
 96 #}}}
 97 
 98 #杂项 {{{
 99 #允许在交互模式中使用注释  例如:
100 #cmd #这是注释
101 setopt INTERACTIVE_COMMENTS      
102  
103 #启用自动 cd,输入目录名回车进入目录
104 #稍微有点混乱,不如 cd 补全实用
105 #setopt AUTO_CD
106       
107 #扩展路径
108 #/v/c/p/p => /var/cache/pacman/pkg
109 setopt complete_in_word
110  
111 #禁用 core dumps
112 limit coredumpsize 0
113  
114 #Emacs风格 键绑定
115 bindkey -e
116 #设置 [DEL]键 为向后删除
117 bindkey "\e[3~" delete-char
118  
119 #以下字符视为单词的一部分
120 WORDCHARS='*?_-[]~=&;!#$%^(){}<>'
121 #}}}
122 
123 #自动补全功能 {{{
124 setopt AUTO_LIST
125 setopt AUTO_MENU
126 #开启此选项,补全时会直接选中菜单项
127 #setopt MENU_COMPLETE
128 
129 autoload -U compinit
130 compinit
131  
132 #自动补全缓存
133 #zstyle ':completion::complete:*' use-cache on
134 #zstyle ':completion::complete:*' cache-path .zcache
135 #zstyle ':completion:*:cd:*' ignore-parents parent pwd
136 
137 #自动补全选项
138 zstyle ':completion:*' verbose yes
139 zstyle ':completion:*' menu select
140 zstyle ':completion:*:*:default' force-list always
141 zstyle ':completion:*' select-prompt '%SSelect:  lines: %L  matches: %M  [%p]'
142  
143 zstyle ':completion:*:match:*' original only
144 zstyle ':completion::prefix-1:*' completer _complete
145 zstyle ':completion:predict:*' completer _complete
146 zstyle ':completion:incremental:*' completer _complete _correct
147 zstyle ':completion:*' completer _complete _prefix _correct _prefix _match _approximate
148  
149 #路径补全
150 zstyle ':completion:*' expand 'yes'
151 zstyle ':completion:*' squeeze-slashes 'yes'
152 zstyle ':completion::complete:*' '\\'
153  
154 #彩色补全菜单 
155 eval $(dircolors -b) 
156 export ZLSCOLORS="${LS_COLORS}"
157 zmodload zsh/complist
158 zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
159 zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31'
160  
161 #修正大小写
162 zstyle ':completion:*' matcher-list '' 'm:{a-zA-Z}={A-Za-z}'
163 #错误校正      
164 zstyle ':completion:*' completer _complete _match _approximate
165 zstyle ':completion:*:match:*' original only
166 zstyle ':completion:*:approximate:*' max-errors 1 numeric
167  
168 #kill 命令补全      
169 compdef pkill=killall
170 zstyle ':completion:*:*:kill:*' menu yes select
171 zstyle ':completion:*:*:*:*:processes' force-list always
172 zstyle ':completion:*:processes' command 'ps -au$USER'
173  
174 #补全类型提示分组 
175 zstyle ':completion:*:matches' group 'yes'
176 zstyle ':completion:*' group-name ''
177 zstyle ':completion:*:options' description 'yes'
178 zstyle ':completion:*:options' auto-description '%d'
179 zstyle ':completion:*:descriptions' format $'\e[01;33m -- %d --\e[0m'
180 zstyle ':completion:*:messages' format $'\e[01;35m -- %d --\e[0m'
181 zstyle ':completion:*:warnings' format $'\e[01;31m -- No Matches Found --\e[0m'
182 zstyle ':completion:*:corrections' format $'\e[01;32m -- %d (errors: %e) --\e[0m'
183  
184 # cd ~ 补全顺序
185 zstyle ':completion:*:-tilde-:*' group-order 'named-directories' 'path-directories' 'users' 'expand'
186 #}}}
187 
188 ##行编辑高亮模式 {{{
189 # Ctrl+@ 设置标记,标记和光标点之间为 region
190 zle_highlight=(region:bg=magenta #选中区域 
191                special:bold      #特殊字符
192                isearch:underline)#搜索时使用的关键字
193 #}}}
194 
195 ##空行(光标在行首)补全 "cd " {{{
196 user-complete(){
197     case $BUFFER in
198         "" )                       # 空行填入 "cd "
199             BUFFER="cd "
200             zle end-of-line
201             zle expand-or-complete
202             ;;
203         "cd  " )                   # TAB + 空格 替换为 "cd ~"
204             BUFFER="cd ~"
205             zle end-of-line
206             zle expand-or-complete
207             ;;
208         " " )
209             BUFFER="!?"
210             zle end-of-line
211             ;;
212         "cd --" )                  # "cd --" 替换为 "cd +"
213             BUFFER="cd +"
214             zle end-of-line
215             zle expand-or-complete
216             ;;
217         "cd +-" )                  # "cd +-" 替换为 "cd -"
218             BUFFER="cd -"
219             zle end-of-line
220             zle expand-or-complete
221             ;;
222         * )
223             zle expand-or-complete
224             ;;
225     esac
226 }
227 zle -N user-complete
228 bindkey "\t" user-complete
229  
230 #显示 path-directories ,避免候选项唯一时直接选中
231 cdpath="/home"
232 #}}}
233 
234 ##在命令前插入 sudo {{{
235 #定义功能 
236 sudo-command-line() {
237     [[ -z $BUFFER ]] && zle up-history
238     [[ $BUFFER != sudo\ * ]] && BUFFER="sudo $BUFFER"
239     zle end-of-line                 #光标移动到行末
240 }
241 zle -N sudo-command-line
242 #定义快捷键为: [Esc] [Esc]
243 bindkey "\e\e" sudo-command-line
244 #}}}
245   
246 #命令别名 {{{
247 alias -g cp='cp -i'
248 alias -g mv='mv -i'
249 alias -g rm='rm -i'
250 alias -g ls='ls -F --color=auto'
251 alias -g ll='ls -l'
252 alias -g grep='grep --color=auto'
253 alias -g ee='emacsclient -t'
254 alias -g history='history -fi'
255  
256 #[Esc][h] man 当前命令时,显示简短说明 
257 alias run-help >&/dev/null && unalias run-help
258 autoload run-help
259  
260 #历史命令 top10
261 alias top10='print -l  ${(o)history%% *} | uniq -c | sort -nr | head -n 10'
262 #}}}
263 
264 #路径别名 {{{
265 #进入相应的路径时只要 cd ~xxx
266 hash -d WWW="/home/lighttpd/html"
267 hash -d ARCH="/mnt/arch"
268 hash -d PKG="/var/cache/pacman/pkg"
269 hash -d E="/etc/env.d"
270 hash -d C="/etc/conf.d"
271 hash -d I="/etc/rc.d"
272 hash -d X="/etc/X11"
273 hash -d BK="/home/r00t/config_bak"
274 hash -d HIST="$HISTDIR"
275 #}}}
276     
277 #{{{自定义补全
278 #补全 ping
279 zstyle ':completion:*:ping:*' hosts 192.168.128.1{38,} http://www.g.cn \
280        192.168.{1,0}.1{{7..9},}
281  
282 #补全 ssh scp sftp 等
283 my_accounts=(
284 {r00t,root}@{192.168.1.1,192.168.0.1}
285 kardinal@linuxtoy.org
286 123@211.148.131.7
287 )
288 zstyle ':completion:*:my-accounts' users-hosts $my_accounts
289  
290 #def pacman-color completion as pacman
291 compdef pacman-color=pacman
292 #}}}
293 
294 #{{{ F1 计算器
295 arith-eval-echo() {
296   LBUFFER="${LBUFFER}echo \$(( "
297   RBUFFER=" ))$RBUFFER"
298 }
299 zle -N arith-eval-echo
300 bindkey "^[[11~" arith-eval-echo
301 #}}}
302 
303 ####{{{
304 function timeconv { date -d @$1 +"%Y-%m-%d %T" }
305  
306 # }}}
307 
308  
309 ## END OF FILE #################################################################
310 # vim:filetype=zsh foldmethod=marker autoindent expandtab shiftwidth=4

 

posted @ 2012-12-28 11:14  wangkangluo1  阅读(2473)  评论(0编辑  收藏  举报