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