]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - zsh/rc
Use completion dumpfile in ~/.zsh/cache.
[config/dotfiles.git] / zsh / rc
1 # Zsh configuration file.
2
3
4 # MISCELLANEOUS SETTINGS
5
6 # Use Vi(m) style key bindings.
7 bindkey -v
8
9 # Be paranoid, new files are readable/writable by me only.
10 umask 077
11
12 # Make sure core dumps are created.
13 ulimit -c unlimited
14
15 # Prevent overwriting existing files with '> filename', use '>| filename'
16 # (or >!) instead.
17 setopt noclobber
18
19 # Entering the name of a directory (if it's not a command) will automatically
20 # cd to that directory.
21 setopt autocd
22
23 # When entering a nonexistent command name automatically try to find a similar
24 # one.
25 setopt correct
26
27 # Enable zsh's extended glob abilities.
28 setopt extendedglob
29
30 # Don't exit if <C-d> is pressed.
31 setopt ignoreeof
32
33
34 # HISTORY SETTINGS
35
36 # Use history and store it in ~/.zsh/history.
37 HISTSIZE=5000
38 SAVEHIST=5000
39 HISTFILE=~/.zsh/history
40 # Append to the history file instead of overwriting it and do it immediately
41 # when a command is executed.
42 setopt appendhistory
43 setopt incappendhistory
44 # If the same command is run multiple times store it only once in the history.
45 setopt histignoredups
46 # Vim like completions of previous executed commands.
47 bindkey "^P" history-beginning-search-backward
48 bindkey "^N" history-beginning-search-forward
49
50
51 # PROMPT SETTINGS
52
53 # Autoload add-zsh-hook to add/remove zsh hook functions easily.
54 autoload -Uz add-zsh-hook
55
56 # Use colorized output, necessary for prompts and completions.
57 autoload -U colors && colors
58
59 # Set the default prompt. The current host and working directory is displayed,
60 # the exit code of the last command if it wasn't 0, the number of running jobs
61 # if not 0 and a + if this shell is running inside another shell.
62 # The prompt is in green and blue to make easily detectable, the error exit
63 # code in red and bold and the job count in yellow.
64 PROMPT="%{${fg[green]}%}%B%m%b%{${fg[default]}%}:\
65 %{${fg[blue]}%}%B%~%b%{${fg[default]}%} \
66 %(1j.%{${fg[yellow]}%}%j%{${fg[default]}%}.)%(2L.+.)%# \
67 %(?..(%{${fg[red]}%}%B%?%b%{${fg[default]}%}%) )"
68
69 # VCS_Info was added in 4.3.9.
70 if [[ $ZSH_VERSION == (4.3.<9->|4.<4->*|<5->*) ]]; then
71     # Allow substitutions and expansions in the prompt, necessary for
72     # vcs_info.
73     setopt promptsubst
74     # Load vcs_info to display information about version control repositories.
75     autoload -Uz vcs_info
76     # Only look for git and mercurial repositories; the only I use.
77     zstyle ':vcs_info:*' enable git hg
78     # Set style of vcs_info display. The current branch (green) and vcs (blue)
79     # is displayed. If there is an special action going on (merge, rebase)
80     # it's also displayed (red).
81     zstyle ':vcs_info:*' formats \
82     "(%{${fg[green]}%}%b%{${fg[default]}%}:\
83 %{${fg[blue]}%}%s%{${fg[default]}%})"
84     zstyle ':vcs_info:*' actionformats \
85     "(%{${fg[green]}%}%b%{${fg[default]}%}/\
86 %{${fg[red]}%}%a%{${fg[default]}%}:\
87 %{${fg[blue]}%}%s%{${fg[default]}%})"
88     # Call vcs_info as precmd before every prompt.
89     prompt_precmd() {
90         vcs_info
91     }
92     add-zsh-hook precmd prompt_precmd
93
94     # Display the vcs information in the right prompt.
95     RPROMPT='${vcs_info_msg_0_}'
96 fi
97
98 # When screen is used set the name of the window to the currently running
99 # program.
100 #
101 # When a program is started preexec() sets the window's name to it; when it
102 # stops precmd() resets the windows' name to 'zsh'.
103 if [[ $TERM == screen ]]; then
104     # Set to a non empty value to reset the window name in the next precmd()
105     # call.
106     screen_name_reset=yes
107
108     screen_preexec() {
109         # Get the program name with its arguments.
110         local program_name=$1
111         # When sudo is used use real program name instead.
112         if [[ $program_name == sudo* ]]; then
113             program_name=${program_name#sudo }
114         fi
115         # Remove all arguments from the program name.
116         program_name=${program_name%% *}
117
118         # Ignore often used commands which are only running for a very short
119         # time. This prevents a "blinking" name when it's changed to "cd" for
120         # example and then some milliseconds later back to "zsh".
121         [[ $program_name == (cd*|ls|la|ll|clear) ]] && return
122
123         # Change my shortcuts so the real name of the program is displayed.
124         case $program_name in
125             e)
126                 program_name=elinks
127                 ;;
128             g)
129                 program_name=git
130                 ;;
131             m)
132                 program_name=mutt
133                 ;;
134             v|vi)
135                 program_name=vim
136                 ;;
137         esac
138
139         # Set the window name to the currently running program.
140         print -n "\ek$program_name\e\\"
141
142         # Tell precmd() to reset the window name when the program stops.
143         screen_name_reset=yes
144     }
145
146     screen_precmd() {
147         # Abort if no window name reset is necessary.
148         [[ -z $screen_name_reset ]] && return
149
150         # Reset the window name to 'zsh'.
151         print -n "\ekzsh\e\\"
152
153         # Just reset the name, so no screen reset necessary for the moment.
154         screen_name_reset=
155     }
156
157     # Add the preexec() and precmd() hooks.
158     add-zsh-hook preexec screen_preexec
159     add-zsh-hook precmd screen_precmd
160 fi
161
162
163 # COMPLETION SETTINGS
164
165 # Load the complist module which provides additions to completion lists
166 # (coloring, scrollable).
167 zmodload zsh/complist
168 # Use new completion system, store dumpfile in ~/.zsh/cache to prevent
169 # cluttering of ~/.
170 autoload -U compinit && compinit -d ~/.zsh/cache/zcompdump
171 # Make sure the list of possible completions is displayed after pressing <TAB>
172 # the first time.
173 setopt nolistambiguous
174 # Allow completions in the middle of a text, i.e. "/usr/bin/<TAB>whatever"
175 # completes like "/usr/bin/<TAB>". Useful when adding new options to commands.
176 bindkey "^I" expand-or-complete-prefix
177 # Use cache to speed up completions.
178 zstyle ':completion:*' use-cache on
179 zstyle ':completion:*' cache-path ~/.zsh/cache
180 # Try uppercase if the currently typed string doesn't match. This allows
181 # typing in lowercase most of the time and completion fixes the case.
182 zstyle ':completion:*' matcher-list '' 'm:{a-z}={A-Z}'
183 # Ignore completion functions.
184 zstyle ':completion:*:functions' ignored-patterns '_*'
185 # Ignore parent directory.
186 zstyle ':completion:*:(cd|mv|cp):*' ignore-parents parent pwd
187 # Use ls like colors for completions.
188 zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
189 # Make completion lists scrollable so "do you wish to see all n possibilities"
190 # is no longer displayed.
191 zstyle ':completion:*' list-prompt '%p'
192 # When unsetting variables make sure every variable name is only suggested
193 # once.
194 zstyle ':completion:*:unset:*' ignore-line yes
195 # When working with Mercurial and Git don't complete the same file multiple
196 # times. Very useful when completing file names.
197 zstyle ':completion:*:(hg|git)*:*' ignore-line yes
198
199
200 # ALIAS AND FUNCTION SETTINGS
201
202 # Simplify calls to less, automatically redirects all output.
203 alias -g L='2>&1 | less'
204 # Simplify calls to colordiff, output is also piped through less.
205 alias -g D='2>&1 | colordiff L'
206 # Simplify calls to grep.
207 alias -g G='| grep'
208
209 # Automatically use unified diffs.
210 alias diff='diff -u'
211
212 # Display all files and use human readable sizes.
213 alias du='du -sh'
214
215 # Multiple files given to Vim are opened in tabs, supported since Vim 7.
216 if [[ ${${${(f)"$(vim --version)"}[1]#VIM - Vi IMproved }%% *} == 7* ]]; then
217     alias vim='vim -p'
218 fi
219
220 # Shortcuts for often used programs.
221 alias e='elinks'
222 alias g='git'
223 alias m='mutt'
224 alias v='vim'
225 alias vi='vim'
226
227 # Exit binding like in Vim; I sometimes confuse editor and shell.
228 alias :q='exit'
229
230 # Edit the mercurial patch queue series file for the current mercurial
231 # repository in Vim. Also change Vim's pwd to the patches directory so other
232 # patches can easily be opened.
233 alias vqs='vim -c "cd $(hg root)/.hg/patches/" "$(hg root)/.hg/patches/series"'
234
235 # Make going up directories simple.
236 alias -g ...='../..'
237 alias -g ....='../../..'
238 alias -g .....='../../../..'
239
240 # Improved ls which displays the files in columns (-C), visualizes directories,
241 # links and other special files (-F) and pages everything through less (L).
242 #
243 # If available use GNU ls with colorized output. If it isn't available use
244 # normal ls which needs CLICOLOR_FORCE so it displays colors when used with a
245 # pager.
246 ls --color &> /dev/null
247 if [[ $? -eq 0 ]]; then
248     alias ls='ls --color'
249 else
250     alias ls='CLICOLOR_FORCE=1 ls -G'
251 fi
252 # Main ls function.
253 function ls() {
254     command ls -C -F $* L
255 }
256 # Helper function to list all files.
257 function la() {
258     ls -a $*
259 }
260 # Helper function to list all files in list format with access rights, etc.
261 function ll() {
262     la -l $*
263 }
264
265 # Set correct fpath to allow loading my functions (including completion
266 # functions).
267 fpath=(~/.zsh/functions $fpath)
268 # Autoload my functions (except completion functions). Thanks to caphuso from
269 # the Zsh example files for this idea.
270 autoload ${fpath[1]}/^_*(:t)
271
272 # If ^C is pressed while typing a command, add it to the history so it can be
273 # easily retrieved later and then abort like ^C normally does. This is useful
274 # when I want to abort an command to do something in between and then finish
275 # typing the command.
276 TRAPINT() {
277     # Store the current buffer in the history.
278     zle && print -s $BUFFER
279
280     # Return the default exit code so zsh aborts the current command.
281     return $1
282 }
283
284 # Display TODOs stored in ~/.todo if this file exists.
285 todo() {
286     if [[ -f ~/.todo ]]; then
287         cat ~/.todo | $PAGER
288     fi
289 }
290
291
292 # Load rc file for current OS.
293 source_config ~/.zsh os rc $(uname) nolocal
294 # Load rc file for current hostname (first part before a dot) or rc.local.
295 source_config ~/.zsh host rc ${$(hostname)//.*/}