-
Notifications
You must be signed in to change notification settings - Fork 0
/
.zshrc
536 lines (442 loc) · 15 KB
/
.zshrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# users generic .zshrc file for zsh(1)
## Environment variable configuration
#
# LANG
# http://curiousabt.blog27.fc2.com/blog-entry-65.html
export LANG=ja_JP.UTF-8
## Online Help
#
unalias run-help > /dev/null 2>&1
autoload run-help
HELPDIR=/usr/local/share/zsh/help
## Backspace key
#
bindkey "^?" backward-delete-char
autoload -Uz colors
# colors
############################
# color
# http://www.unix.com/shell-programming-and-scripting/184735-zsh-prompt-variable-expansion-ansi-color-sequences.html
_reset='%{'$(print -P '\033\[0m')'%}'
fg_white='%{'$(print -P '\033[0;0;37m')'%}'
fg_lightgray='%{'$(print -P '\033[0;0;37m')'%}'
fg_gray='%{'$(print -P '\033[0;0;30m')'%}'
fg_black='%{'$(print -P '\033[0;0;30m')'%}'
fg_red='%{'$(print -P '\033[0;0;31m')'%}'
fg_lightred='%{'$(print -P '\033[0;0;31m')'%}'
fg_green='%{'$(print -P '\033[0;0;32m')'%}'
fg_lightgreen='%{'$(print -P '\033[0;0;32m')'%}'
fg_brown='%{'$(print -P '\033[0;0;33m')'%}'
fg_yellow='%{'$(print -P '\033[0;0;33m')'%}'
fg_blue='%{'$(print -P '\033[0;0;34m')'%}'
fg_lightblue='%{'$(print -P '\033[0;0;34m')'%}'
fg_purple='%{'$(print -P '\033[0;0;35m')'%}'
fg_pink='%{'$(print -P '\033[0;0;35m')'%}'
fg_cyan='%{'$(print -P '\033[0;0;36m')'%}'
fg_lightcyan='%{'$(print -P '\033[0;0;36m')'%}'
bg_black='%{'$(print -P '\033[40m')'%}'
bg_red='%{'$(print -P '\033[41m')'%}'
bg_green='%{'$(print -P '\033[42m')'%}'
bg_yellow='%{'$(print -P '\033[43m')'%}'
bg_blue='%{'$(print -P '\033[44m')'%}'
bg_magenta='%{'$(print -P '\033[45m')'%}'
bg_cyan='%{'$(print -P '\033[46m')'%}'
bg_white='%{'$(print -P '\033[47m')'%}'
## Default shell configuration
#
# set prompt
# setopt prompt_subst
# 改行のない出力をプロンプトで上書きするのを防ぐ
# http://stackoverflow.com/questions/24178153/why-zsh-doesnt-print-string-which-doesnt-contain-new-line
setopt PROMPT_CR
case ${UID} in
0) # root
PROMPT="${fg_red}%/#${_reset}%b "
SPROMPT="${fg_red}%r is correct? [n,y,a,e]:${_reset}%b "
[ -n "${REMOTEHOST}${SSH_CONNECTION}" ] &&
PROMPT="${fg_cyan}$(echo ${HOST%%.*} | tr '[a-z]' '[A-Z]') ${PROMPT}"
;;
*)
unset PROMPT
#
# Prompt
#
# Show git branch when you are in git repository
# http://d.hatena.ne.jp/mollifier/20100906/p1
autoload -Uz add-zsh-hook
autoload -Uz vcs_info
zstyle ':vcs_info:*' enable git svn hg bzr
zstyle ':vcs_info:*' formats '(%s)-[%b]'
zstyle ':vcs_info:*' actionformats '(%s)-[%b|%a]'
zstyle ':vcs_info:(svn|bzr):*' branchformat '%b:r%r'
zstyle ':vcs_info:bzr:*' use-simple true
autoload -Uz is-at-least
if is-at-least 4.3.10; then
zstyle ':vcs_info:git:*' check-for-changes true
zstyle ':vcs_info:git:*' stagedstr "[+]"
zstyle ':vcs_info:git:*' unstagedstr "[!]"
zstyle ':vcs_info:git:*' formats '(%s)-[%b]%c%u'
zstyle ':vcs_info:git:*' actionformats '(%s)-[%b|%a]%c%u'
fi
function _update_vcs_info_msg() {
psvar=()
LANG=en_US.UTF-8 vcs_info
psvar[2]=$(_git_not_pushed)
[[ -n "$vcs_info_msg_0_" ]] && psvar[1]="$vcs_info_msg_0_"
}
add-zsh-hook precmd _update_vcs_info_msg
function _git_not_pushed()
{
if [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]; then
head="$(git rev-parse HEAD)"
for x in $(git rev-parse --remotes)
do
if [ "$head" = "$x" ]; then
return 0
fi
done
echo "[*]"
fi
return 0
}
GIT_PROMPT="%1(v|%F${fg_cyan}%1v%2v%f|)${vcs_info_git_pushed}${_reset}"
function _update_venv_info_msg() {
if [ -n "${VIRTUAL_ENV}" ]; then
VIRTUAL_ENV_PROMPT=$(echo ${VIRTUAL_ENV%/*} | awk -F"/" -v OFS="/" '{print $(NF - 1), $NF}')
psvar[2]="((${VIRTUAL_ENV_PROMPT}))"
fi
}
add-zsh-hook precmd _update_venv_info_msg
;;
esac
#
# Vi入力モードでPROMPTの色を変える
# http://memo.officebrook.net/20090226.html
function zle-line-init zle-keymap-select {
case $KEYMAP in
vicmd)
PROMPT="[${fg_blue}%n@%m${_reset}]"
;;
main|viins)
# PROMPT="[${fg[magenta]}%n@%m${_reset}]"
PROMPT="[${fg_purple}%n@%m${_reset}]"
# PROMPT="[%n@%m}]"
;;
esac
PROMPT+="%(?.${fg_white}.${fg_red})%}(%?)${_reset}" # 直前のコマンドのreturn値
PROMPT+=" ${fg_yellow}%D{%FT%T.%Z}${_reset}" # 日付と時刻
PROMPT+=" ${fg_green}%/${_reset}" # カレントのディレクトリ
PROMPT+=" ${GIT_PROMPT}"
PROMPT+="
$ " # 改行
zle reset-prompt
}
PROMPT2=''
zle -N zle-line-init
zle -N zle-keymap-select
# 指定したコマンド名がなく、ディレクトリ名と一致した場合 cd する
setopt auto_cd
# cd でTabを押すとdir list を表示
setopt auto_pushd
# ディレクトリスタックに同じディレクトリを追加しないようになる
setopt pushd_ignore_dups
# コマンドのスペルチェックをする
setopt correct
# コマンドライン全てのスペルチェックをする
setopt correct_all
# 上書きリダイレクトの禁止
setopt no_clobber
# 補完候補リストを詰めて表示
setopt list_packed
# auto_list の補完候補一覧で、ls -F のようにファイルの種別をマーク表示
setopt list_types
# 補完候補が複数ある時に、一覧表示する
setopt auto_list
# コマンドラインの引数で --prefix=/usr などの = 以降でも補完できる
setopt magic_equal_subst
# カッコの対応などを自動的に補完する
setopt auto_param_keys
# ディレクトリ名の補完で末尾の / を自動的に付加し、次の補完に備える
setopt auto_param_slash
# {a-c} を a b c に展開する機能を使えるようにする
setopt brace_ccl
# シンボリックリンクは実体を追うようになる
#setopt chase_links
# 補完キー(Tab, Ctrl+I) を連打するだけで順に補完候補を自動で補完する
setopt auto_menu
# sudoも補完の対象
zstyle ':completion:*:sudo:*' command-path /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin
# 色付きで補完する
zstyle ':completion:*' list-colors di=34 fi=0
# 補完候補を選択するときにわかりやすくする
zstyle ':completion:*:default' menu select=2
zstyle ':completion:*' verbose yes
zstyle ':completion:*' completer _expand _complete _match _prefix _approximate _list _history
zstyle ':completion:*:messages' format '%F{YELLOW}%d'$DEFAULT
zstyle ':completion:*:warnings' format '%F{RED}No matches for:''%F{YELLOW} %d'$DEFAULT
zstyle ':completion:*:descriptions' format '%F{YELLOW}completing %B%d%b'$DEFAULT
zstyle ':completion:*:options' description 'yes'
zstyle ':completion:*:descriptions' format '%F{yellow}Completing %B%d%b%f'$DEFAULT
# セパレータを設定する
zstyle ':completion:*' list-separator '-->'
zstyle ':completion:*:manuals' separate-sections true
# マッチ種別を別々に表示
zstyle ':completion:*' group-name ''
# 複数のリダイレクトやパイプなど、必要に応じて tee や cat の機能が使われる
setopt multios
# 最後がディレクトリ名で終わっている場合末尾の / を自動的に取り除かない
setopt noautoremoveslash
# beepを鳴らさないようにする
setopt nolistbeep
setopt nobeep
# Match without pattern
# ex. > rm *~398# remove * without a file "398". For test, use "echo *~398"
setopt extended_glob
## Keybind configuration
#
# emacs like keybind (e.x. Ctrl-a goes to head of a line and Ctrl-e goes
# to end of it)
#
bindkey -v
# history command setting
source ~/.zshrc_history_config
autoload history-search-end
zle -N history-beginning-search-backward-end history-search-end
zle -N history-beginning-search-forward-end history-search-end
bindkey "^P" history-beginning-search-backward-end
bindkey "^N" history-beginning-search-forward-end
# Ctrl+S/Ctrl+Q によるフロー制御を使わないようにする
#setopt NO_flow_control
# ファイル名の展開でディレクトリにマッチした場合末尾に / を付加する
setopt mark_dirs
# コマンド名に / が含まれているとき PATH 中のサブディレクトリを探す
setopt path_dirs
# 戻り値が 0 以外の場合終了コードを表示する
# setopt print_exit_value
# コマンドラインがどのように展開され実行されたかを表示するようになる
# setopt xtrace
# ctrl-w, ctrl-bキーで単語移動
bindkey "^W" forward-word
bindkey "^B" backward-word
# back-wordでの単語境界の設定
autoload -Uz select-word-style
select-word-style default
zstyle ':zle:*' word-chars " _-./;@"
zstyle ':zle:*' word-style unspecified
# URLをコピペしたときに自動でエスケープ
autoload -Uz url-quote-magic
zle -N self-insert url-quote-magic
# 勝手にpushd
setopt autopushd
# エラーメッセージ本文出力に色付け
e_normal=`echo -e "¥033[0;30m"`
e_RED=`echo -e "¥033[1;31m"`
e_BLUE=`echo -e "¥033[1;36m"`
function make() {
LANG=C command make "$@" 2>&1 | sed -e "s@[Ee]rror:.*@$e_RED&$e_normal@g" -e "s@cannot¥sfind.*@$e_RED&$e_normal@g" -e "s@[Ww]arning:.*@$e_BLUE&$e_normal@g"
}
function cwaf() {
LANG=C command ./waf "$@" 2>&1 | sed -e "s@[Ee]rror:.*@$e_RED&$e_normal@g" -e "s@cannot¥sfind.*@$e_RED&$e_normal@g" -e "s@[Ww]arning:.*@$e_BLUE&$e_normal@g"
}
## Completion configuration
fpath=(~/.zsh/functions/Completion ${fpath})
autoload -U compinit
compinit
## zsh editor
#
autoload zed
## Prediction configuration
#
autoload predict-on
## Command Line Stack [Esc]-[q]
bindkey -a 'q' push-line
case "${OSTYPE}" in
# MacOSX
darwin*)
export PATH=$PATH:/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources:~/bin
;;
freebsd*)
case ${UID} in
0)
updateports()
{
if [ -f /usr/ports/.portsnap.INDEX ]
then
portsnap fetch update
else
portsnap fetch extract update
fi
(cd /usr/ports/; make index)
portversion -v -l \<
}
alias appsupgrade='pkgdb -F && BATCH=YES NO_CHECKSUM=YES portupgrade -a'
;;
esac
;;
esac
## terminal configuration
# http://journal.mycom.co.jp/column/zsh/009/index.html
unset LSCOLORS
case "${TERM}" in
xterm)
export TERM=xterm-color
;;
kterm)
export TERM=kterm-color
# set BackSpace control character
stty erase
;;
esac
case "${TERM}" in
cons25)
unset LANG
export LSCOLORS=ExFxCxdxBxegedabagacad
# export LS_COLORS='di=01;32:ln=01;35:so=01;32:ex=01;31:bd=46;34:cd=43;34:su=41;30:sg=46;30'
export LS_COLORS='di=34:ln=35:so=32:pi=33:ex=31:bd=46;34:cd=43;34:su=41;30:sg=46;30:tw=42;30:ow=43;30'
# zstyle ':completion:*' list-colors \
# 'di=;36;1' 'ln=;35;1' 'so=;32;1' 'ex=31;1' 'bd=46;34' 'cd=43;34'
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
;;
kterm*|xterm*)
export CLICOLOR=1
export LS_COLORS='di=34:ln=35:so=32:pi=33:ex=31:bd=46;34:cd=43;34:su=41;30:sg=46;30:tw=42;30:ow=43;30'
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
precmd() {
echo -ne "\033]0;${USER}@${HOST}:$(pwd) $(date '+%FT%T.%Z')\007"
}
;;
dumb)
echo "Welcome Emacs Shell"
;;
esac
# コマンドにも色を付ける(install by `brew install zsh-syntax-highlighting`)
source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
export EDITOR=vim
export PATH=$PATH:/sbin:/usr/local/bin
########################
# play framework
########################
export PATH=$PATH:~/local/script/scala/play
export MANPATH=$MANPATH:/usr/local/share/man
########################
# rbenv
########################
export PATH="$HOME/.rbenv/bin:$PATH"
if which rbenv > /dev/null; then
eval "$(rbenv init -)"
fi
expand-to-home-or-insert () {
if [ "$LBUFFER" = "" -o "$LBUFFER[-1]" = " " ]; then
LBUFFER+="~/"
else
zle self-insert
fi
}
# zsh の exntended_glob と HEAD^^^ を共存させる。
# http://subtech.g.hatena.ne.jp/cho45/20080617/1213629154
typeset -A abbreviations
abbreviations=(
"L" "| $PAGER"
"G" "| grep"
"HEAD^" "HEAD\\^"
"HEAD^^" "HEAD\\^\\^"
"HEAD^^^" "HEAD\\^\\^\\^"
"HEAD^^^^" "HEAD\\^\\^\\^\\^\\^"
"HEAD^^^^^" "HEAD\\^\\^\\^\\^\\^"
)
magic-abbrev-expand () {
local MATCH
LBUFFER=${LBUFFER%%(#m)[-_a-zA-Z0-9^]#}
LBUFFER+=${abbreviations[$MATCH]:-$MATCH}
}
magic-abbrev-expand-and-insert () {
magic-abbrev-expand
zle self-insert
}
magic-abbrev-expand-and-accept () {
magic-abbrev-expand
zle accept-line
}
no-magic-abbrev-expand () {
LBUFFER+=' '
}
zle -N magic-abbrev-expand
zle -N magic-abbrev-expand-and-insert
zle -N magic-abbrev-expand-and-accept
zle -N no-magic-abbrev-expand
bindkey "\r" magic-abbrev-expand-and-accept # M-x RET はできなくなる
bindkey "^J" accept-line # no magic
bindkey " " magic-abbrev-expand-and-insert
bindkey "." magic-abbrev-expand-and-insert
bindkey "^x " no-magic-abbrev-expand
function rmf(){
for file in $*
do
__rm_single_file $file
done
}
function __rm_single_file(){
[ -d ~/.Trash/ ] || command /bin/mkdir ~/.Trash
[ $# -eq 1 ] || {
echo "__rm_single_file: 1 argument required but $# passed."
return -1
}
[ -e $1 ] || {
echo "No such file or directory: $file"
return -1
}
NAME=${1##*/}
while [ -e ~/.Trash/$NAME ]
do
NAME="$BASENAME.$(date '+%Y%m%d%H%M%S').$(cat /dev/urandom | LC_CTYPE=C tr -dc '[:alnum:]' | head -c 8 ; echo)"
done
command /bin/mv $1 ~/.Trash/$NAME
}
## alias設定
#
[ -f ~/dotfiles/.zshrc.alias ] && source ~/dotfiles/.zshrc.alias
case "${OSTYPE}" in
# Mac(Unix)
darwin*)
[ -f ~/dotfiles/.zshrc.osx ] && source ~/dotfiles/.zshrc.osx
;;
# Linux
linux*)
[ -f ~/dotfiles/.zshrc.linux ] && source ~/dotfiles/.zshrc.linux
;;
esac
autoload -Uz zmv
# less
# required: GNU Source-highlight
# Mac: brew install, Ubuntu: apt-get install, Fedora: yum install source-highlight
export LESS="-MQRSFXWin -P'?f%f .?ltLine %lt:?pt%pt\%:?btByte %bt:-...'"
export LESSCHARSET=utf-8
which src-hilite-lesspipe.sh > /dev/null || \
export LESSOPEN='| /usr/local/bin/src-hilite-lesspipe.sh %s'
# cdr
if is-at-least 4.3.11; then
autoload -Uz chpwd_recent_dirs cdr add-zsh-hook
add-zsh-hook chpwd chpwd_recent_dirs
zstyle ':chpwd:*' recent-dirs-max 5000
zstyle ':chpwd:*' recent-dirs-default yes
zstyle ':completion:*' recent-dirs-insert both
fi
# zaw
if [ -f ~/.zsh-tools/zaw/zaw.zsh ]; then
source ~/.zsh-tools/zaw/zaw.zsh
bindkey '^R' zaw-history
if is-at-least 4.3.11; then
bindkey '^@' zaw-cdr
fi
zstyle ':filter-select' case-insensitive yes
zstyle ':filter-select:highlight' matched fg=red, standout
zstyle ':filter-select' max-lines 10 # use 10 lines for filter-select
zstyle ':filter-select' rotate-list yes # enable rotation for filter-select
zstyle ':filter-select' case-insensitive yes # enable case-insensitive search
zstyle ':filter-select' extended-search yes
fi
# pyenv
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi
# added by travis gem
[ -f /Users/jp20404/.travis/travis.sh ] && source /Users/jp20404/.travis/travis.sh