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