Zsh 配置文件

例 30.1. Zsh 配置文件 .zshrc

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

这是一种效果超炫的提示符,把上面与提示符相关的配置语句注释掉,加入下面代码

  1. #线框型提示符
  2. function precmd {
  3. local TERMWIDTH
  4. (( TERMWIDTH = ${COLUMNS} - 1 ))
  5. ###
  6. # 如果路径太长,截短它
  7. FILLBAR=""
  8. PWDLEN=""
  9. #统计 %~ 双字节字符
  10. local count_db_wth_char="$( echo ${^${(%):-%~}} | sed 's/\(.\)/\1\n/g' | grep -c \[\^\!-\~\] )"
  11. local promptsize=${#${(%):---(%n@%m:%l)---()--}}
  12. local pwdsize=${#${(%):-%~}}+$count_db_wth_char
  13. if [[ "$promptsize + $pwdsize" -gt $TERMWIDTH ]]; then
  14. ((PWDLEN=$TERMWIDTH - $promptsize))
  15. else
  16. FILLBAR="\${(l.(($TERMWIDTH - ($promptsize + $pwdsize)))..${HBAR}.)}"
  17. fi
  18. ###
  19. # Get APM info.
  20. #if which ibam > /dev/null; then
  21. #APM_RESULT=`ibam --percentbattery`
  22. #elif which apm > /dev/null; then
  23. #APM_RESULT=`apm`
  24. #fi
  25. }
  26. setopt extended_glob
  27. preexec () {
  28. if [[ "$TERM" == "screen" ]]; then
  29. local CMD=${1[(wr)^(*=*|sudo|-*)]}
  30. echo -n "\ek$CMD\e\\"
  31. fi
  32. }
  33. setprompt () {
  34. ###
  35. # Need this so the prompt will work.
  36. setopt prompt_subst
  37. ###
  38. # See if we can use colors.
  39. autoload colors zsh/terminfo
  40. if [[ "$terminfo[colors]" -ge 8 ]]; then
  41. colors
  42. fi
  43. for color in RED GREEN YELLOW BLUE MAGENTA CYAN WHITE; do
  44. eval $color='%{$terminfo[bold]$fg[${(L)color}]%}'
  45. eval LIGHT_$color='%{$fg[${(L)color}]%}'
  46. (( count = $count + 1 ))
  47. done
  48. NO_COLOUR="%{$terminfo[sgr0]%}"
  49. ###
  50. # See if we can use extended characters to look nicer.
  51. typeset -A altchar
  52. set -A altchar ${(s..)terminfo[acsc]}
  53. SET_CHARSET="%{$terminfo[enacs]%}"
  54. SHIFT_IN="%{$terminfo[smacs]%}"
  55. SHIFT_OUT="%{$terminfo[rmacs]%}"
  56. HBAR=${altchar[q]:--}
  57. #HBAR=" "
  58. ULCORNER=${altchar[l]:--}
  59. LLCORNER=${altchar[m]:--}
  60. LRCORNER=${altchar[j]:--}
  61. URCORNER=${altchar[k]:--}
  62. ###
  63. # Decide if we need to set titlebar text.
  64. case $TERM in
  65. xterm*)
  66. TITLEBAR=$'%{\e]0;%(!.-=*[ROOT]*=- | .)%n@%m:%~ | ${COLUMNS}x${LINES} | %y\a%}'
  67. ;;
  68. screen)
  69. TITLEBAR=$'%{\e_screen \005 (\005t) | %(!.-=[ROOT]=- | .)%n@%m:%~ | ${COLUMNS}x${LINES} | %y\e\\%}'
  70. ;;
  71. *)
  72. TITLEBAR=''
  73. ;;
  74. esac
  75. ###
  76. # Decide whether to set a screen title
  77. if [[ "$TERM" == "screen" ]]; then
  78. STITLE=$'%{\ekzsh\e\\%}'
  79. else
  80. STITLE=''
  81. fi
  82. ###
  83. # APM detection
  84. #if which ibam > /dev/null; then
  85. #APM='$RED${${APM_RESULT[(f)1]}[(w)-2]}%%(${${APM_RESULT[(f)3]}[(w)-1]})$LIGHT_BLUE:'
  86. #elif which apm > /dev/null; then
  87. #APM='$RED${APM_RESULT[(w)5,(w)6]/\% /%%}$LIGHT_BLUE:'
  88. #else
  89. APM=''
  90. #fi
  91. ###
  92. # Finally, the prompt.
  93. PROMPT='$SET_CHARSET$STITLE${(e)TITLEBAR}\
  94. $CYAN$SHIFT_IN$ULCORNER$BLUE$HBAR$SHIFT_OUT(\
  95. $GREEN%(!.%SROOT%s.%n)$GREEN@%m:%l\
  96. $BLUE)$SHIFT_IN$HBAR$CYAN$HBAR${(e)FILLBAR}$BLUE$HBAR$SHIFT_OUT(\
  97. $MAGENTA%$PWDLEN<...<%~%<<\
  98. $BLUE)$SHIFT_IN$HBAR$CYAN$URCORNER$SHIFT_OUT\
  99. $CYAN$SHIFT_IN$LLCORNER$BLUE$HBAR$SHIFT_OUT(\
  100. %(?..$LIGHT_RED%?$BLUE:)\
  101. ${(e)APM}$YELLOW%D{%H:%M}\
  102. $LIGHT_BLUE:%(!.$RED.$WHITE)%#$BLUE)$SHIFT_IN$HBAR$SHIFT_OUT\
  103. $CYAN$SHIFT_IN$HBAR$SHIFT_OUT\
  104. $NO_COLOUR '
  105. RPROMPT=' $CYAN$SHIFT_IN$HBAR$BLUE$HBAR$SHIFT_OUT\
  106. ($YELLOW%D{%a,%b%d}$BLUE)$SHIFT_IN$HBAR$CYAN$LRCORNER$SHIFT_OUT$NO_COLOUR'
  107. PS2='$CYAN$SHIFT_IN$HBAR$SHIFT_OUT\
  108. $BLUE$SHIFT_IN$HBAR$SHIFT_OUT(\
  109. $LIGHT_GREEN%_$BLUE)$SHIFT_IN$HBAR$SHIFT_OUT\
  110. $CYAN$SHIFT_IN$HBAR$SHIFT_OUT$NO_COLOUR '
  111. }
  112. setprompt