dot/shell-rc, etc.: Introduce an easily user-definable prompt section.
[profile] / dot / zshrc
1 ### -*-sh-*-
2 ###
3 ### Zsh session things.
4
5 __mdw_shell=zsh
6 case ${INSIDE_EMACS+t},$TERM in t,dumb) unsetopt zle ;; esac
7
8 ###--------------------------------------------------------------------------
9 ### History settings.
10
11 ## Put this here so that `.shellrc-local' can override.
12
13 HISTFILE=~/.zsh-history
14 HISTSIZE=1000
15 SAVEHIST=1000
16
17 ###--------------------------------------------------------------------------
18 ### Hook implementation.
19
20 __mdw_precmd_hook= __mdw_preexec_hook=
21
22 __mdw_run_precmd_hook () { __mdw_runhook __mdw_precmd_hook; }
23 __mdw_run_preexec_hook () { __mdw_runhook __mdw_preexec_hook "$2"; }
24
25 precmd_functions+=(__mdw_run_precmd_hook)
26 preexec_functions+=(__mdw_run_preexec_hook)
27
28 ###--------------------------------------------------------------------------
29 ### Common shell configuration.
30
31 . "$HOME/.shell-rc"
32
33 ## Fixup: `zsh' has sane quoting rules.
34 __mdw_runhook () {
35 local hk=$1 saverc=$? t i; shift
36
37 eval t=\${$hk+t}
38 case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac
39
40 eval t=\$$hk
41 for i in ${=t}; do __mdw_setrc $saverc; "$i" "$@"; done
42 }
43
44 ###--------------------------------------------------------------------------
45 ### Prompt hacking.
46
47 __mdw_set_prompt_hacks () {
48 case $TERM in
49 linux*|screen*|xterm*|putty*|vt100*|eterm*)
50 bold=%B unbold=%b
51 gitcolour=%F{cyan} extracolour=%F{yellow} rccolour=%F{red} uncolour=%f
52 ;;
53 esac
54 host=%m dir=" %(6~!%-1~/.../%4~!%~)"
55 more=%F{green}%_%f
56 }
57
58 if [ -t 0 ]; then
59 __mdw_source_if_exists /usr/lib/git-core/git-sh-prompt
60 __mdw_set_prompt_pieces
61 fi
62
63 ###--------------------------------------------------------------------------
64 ### Line editing.
65
66 case $TERM in dumb) unsetopt zle ;; esac
67
68 bindkey -e
69
70 for w in \
71 forward-word backward-word kill-word backward-kill-word \
72 transpose-words capitalize-word up-case-word down-case-word \
73 delete-whole-word select-word
74 do
75 autoload -U $w-match
76 zle -N $w-bash $w-match
77 zle -N $w-shell $w-match
78 zstyle ':zle:*-bash' word-style standard
79 zstyle ':zle:*-bash' skip-whitespace-first true
80 zstyle ':zle:*-bash' word-chars ""
81 zstyle ':zle:*-shell' word-style shell
82 zstyle ':zle:*-shell' skip-whitespace-first false
83 zstyle ':completion:*' rehash true
84 done
85
86 bindkey "\eb" backward-word-bash
87 bindkey "\e^b" backward-word-shell
88 bindkey "\ef" forward-word-bash
89 bindkey "\e^f" forward-word-shell
90 bindkey "\e^?" backward-kill-word-bash
91 bindkey "^w" backward-kill-word-shell
92 bindkey "\ed" kill-word-bash
93 bindkey "\e^d" kill-word-shell
94 bindkey "\et" transpose-words-bash
95 bindkey "\e^t" transpose-words-shell
96 bindkey "\eu" up-case-word-bash
97 bindkey "\e^u" up-case-word-shell
98 bindkey "\el" down-case-word-bash
99 bindkey "\e^l" down-case-word-shell
100 bindkey "\ec" capitalize-word-bash
101 bindkey "\e^c" capitalize-word-shell
102
103 bindkey "\e[1~" beginning-of-line "\e[4~" end-of-line
104
105 bindkey -s "\eOQ" "/" "\eOR" "*" "\eOS" "-"
106 bindkey -s "\eOw" "7" "\eOx" "8" "\eOy" "9"
107 bindkey -s "\eOt" "4" "\eOu" "5" "\eOv" "6" "\eOk" "+"
108 bindkey -s "\eOq" "1" "\eOr" "2" "\eOs" "3"
109 bindkey -s "\eOp" "0" "\eOn" "."; bindkey "\eOM" accept-line
110
111 bindkey -s "\eOQ" "/" "\eOR" "*" "\eOS" "-"
112 bindkey -s "\eOw" "7" "\eOx" "8" "\eOy" "9"
113 bindkey -s "\eOt" "4" "\eOu" "5" "\eOv" "6" "\eOk" "+"
114 bindkey -s "\eOq" "1" "\eOr" "2" "\eOs" "3"
115 bindkey -s "\eOp" "0" "\eOn" "."; bindkey "\eOM" accept-line
116
117 bindkey "\ep" history-beginning-search-backward
118 bindkey "\en" history-beginning-search-forward
119 bindkey "\e," _history-complete-older
120
121 for i in '!' '$' '@' '/' '~'; do
122 bindkey "\e$i" _bash_complete-word
123 bindkey "^X$i" _bash_list-choices
124 done
125
126 setopt interactive_comments
127 bindkey "\e#" pound-insert
128
129 __mdw_delete_horizontal_space () {
130 LBUFFER=${LBUFFER%%[[:space:]]##}
131 RBUFFER=${RBUFFER##[[:space:]]##}
132 }
133 zle -N delete-horizontal-space __mdw_delete_horizontal_space
134 bindkey "\e\\" delete-horizontal-space
135
136 __mdw_just_one_space () {
137 LBUFFER="${LBUFFER%%[[:space:]]##} "
138 RBUFFER=${RBUFFER##[[:space:]]##}
139 }
140 zle -N just-one-space __mdw_just_one_space
141 bindkey "\e " just-one-space
142
143 ###--------------------------------------------------------------------------
144 ### Completion.
145
146 ## Contexts: :completion:FUNCTION:COMPLETER:COMMAND:ARGUMENT:TAG
147 zstyle ':completion:*' completer _expand _complete _ignored _approximate
148 zstyle ':completion:*' insert-unambiguous false
149 zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
150 zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s
151 zstyle ':completion:*' matcher-list '' '+m:{[:lower:]}={[:upper:]} r:|[._-]=** r:|=**' '+l:|=* r:|=*'
152 zstyle ':completion:*' max-errors 0 numeric
153 zstyle ':completion:*' original true
154 zstyle ':completion:*' verbose false
155 zstyle ':completion:*:*:git*:*' verbose true
156
157 ## Initialize the fancy completion machinery.
158 autoload -Uz compinit
159 compinit
160
161 _r () { words[1]=sudo; _normal; }
162 compdef _r rootly
163 compdef _ssh @
164
165 ###--------------------------------------------------------------------------
166 ### Other shell tweaking.
167
168 unsetopt auto_cd
169 unsetopt auto_menu
170 setopt bang_hist
171 unsetopt bash_auto_list
172 unsetopt beep
173 setopt c_bases octal_zeroes
174 setopt extendedglob
175 unsetopt flow_control
176 unsetopt global_export
177 setopt glob_star_short
178 setopt hist_ignore_all_dups
179 setopt hist_ignore_space
180 unsetopt ksh_glob
181 setopt list_ambiguous
182 setopt list_packed
183 setopt multios
184 unsetopt nomatch
185 unsetopt menu_complete
186 setopt notify
187 setopt rc_expand_param
188 setopt rc_quotes
189 setopt share_history
190
191 hash -d t=$TMPDIR
192
193 ###--------------------------------------------------------------------------
194 ### Finishing touches.
195
196 ## Local tweaks.
197 __mdw_source_if_exists "$HOME/.zshrc-local"
198
199 ###----- That's all, folks --------------------------------------------------