]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - zsh/rc
Use a local variable in screen_preexec().
[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=1000
38 SAVEHIST=1000
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 # Allow substitutions and expansions in the prompt, necessary for vcs_info.
70 setopt promptsubst
71 # Load vcs_info to display information about version control repositories.
72 autoload -Uz vcs_info
73 # Only look for git and mercurial repositories; the only I use.
74 zstyle ':vcs_info:*' enable git hg
75 # Set style of vcs_info display. The current branch (green) and vcs (blue) is
76 # displayed. If there is an special action going on (merge, rebase) it's also
77 # displayed (red).
78 zstyle ':vcs_info:*' formats \
79 "(%{${fg[green]}%}%b%{${fg[default]}%}:\
80 %{${fg[blue]}%}%s%{${fg[default]}%})"
81 zstyle ':vcs_info:*' actionformats \
82 "(%{${fg[green]}%}%b%{${fg[default]}%}/\
83 %{${fg[red]}%}%a%{${fg[default]}%}:\
84 %{${fg[blue]}%}%s%{${fg[default]}%})"
85 # Call vcs_info as precmd before every prompt.
86 prompt_precmd() {
87     vcs_info
88 }
89 add-zsh-hook precmd prompt_precmd
90
91 # Display the vcs information in the right prompt.
92 RPROMPT='${vcs_info_msg_0_}'
93
94 # When screen is used set the name of the window to the currently running
95 # program.
96 #
97 # When a program is started preexec() sets the window's name to it; when it
98 # stops precmd() resets the windows' name to 'zsh'.
99 if [[ $TERM == screen ]]; then
100     # Set to a non empty value to reset the window name in the next precmd()
101     # call.
102     screen_name_reset=yes
103
104     screen_preexec() {
105         # Get the application name excluding any arguments.
106         local program_name=${1%% *}
107
108         # Ignore often used commands which are only running for a very short
109         # time. This prevents a "blinking" name when it's changed to "cd" for
110         # example and then some milliseconds later back to "zsh".
111         [[ $program_name == (cd*|ls|la|ll|clear) ]] && return
112
113         # Set the window name to the currently running program.
114         print -n "\ek$program_name\e\\"
115
116         # Tell precmd() to reset the window name when the program stops.
117         screen_name_reset=yes
118     }
119
120     screen_precmd() {
121         # Abort if no window name reset is necessary.
122         [[ -z $screen_name_reset ]] && return
123
124         # Reset the window name to 'zsh'.
125         print -n "\ekzsh\e\\"
126
127         # Just reset the name, so no screen reset necessary for the moment.
128         screen_name_reset=
129     }
130
131     # Add the preexec() and precmd() hooks.
132     add-zsh-hook preexec screen_preexec
133     add-zsh-hook precmd screen_precmd
134 fi
135
136
137 # COMPLETION SETTINGS
138
139 # Use new completion system.
140 autoload -U compinit && compinit
141 # Load the complist module which provides additions to completion lists
142 # (coloring, scrollable).
143 zmodload zsh/complist
144 # Make sure the list of possible completions is displayed after pressing <TAB>
145 # the first time.
146 setopt nolistambiguous
147 # Allow completions in the middle of a text, i.e. "/usr/bin/<TAB>whatever"
148 # completes like "/usr/bin/<TAB>". Useful when adding new options to commands.
149 bindkey "^I" expand-or-complete-prefix
150 # Use cache to speed up completions.
151 zstyle ':completion:*' use-cache on
152 zstyle ':completion:*' cache-path ~/.zsh/cache
153 # Try uppercase if the currently typed string doesn't match. This allows
154 # typing in lowercase most of the time and completion fixes the case.
155 zstyle ':completion:*' matcher-list '' 'm:{a-z}={A-Z}'
156 # Ignore completion functions.
157 zstyle ':completion:*:functions' ignored-patterns '_*'
158 # Ignore parent directory.
159 zstyle ':completion:*:(cd|mv|cp):*' ignore-parents parent pwd
160 # Use ls like colors for completions.
161 zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
162 # Make completion lists scrollable so "do you wish to see all n possibilities"
163 # is no longer displayed.
164 zstyle ':completion:*' list-prompt '%p'
165 # When unsetting variables make sure every variable name is only suggested
166 # once.
167 zstyle ':completion:*:unset:*' ignore-line yes
168 # When working with Mercurial and Git don't complete the same file multiple
169 # times. Very useful when completing file names.
170 zstyle ':completion:*:(hg|git)*:*' ignore-line yes
171
172
173 # ALIAS AND FUNCTION SETTINGS
174
175 # Simplify calls to less, automatically redirects all output.
176 alias -g L='2>&1 | less'
177 # Simplify calls to colordiff, output is also piped through less.
178 alias -g D='2>&1 | colordiff L'
179 # Simplify calls to grep.
180 alias -g G='| grep'
181
182 # Automatically use unified diffs.
183 alias diff='diff -u'
184
185 # Display all files and use human readable sizes.
186 alias du='du -sh'
187
188 # Multiple files given to Vim are opened in tabs.
189 alias vim='vim -p'
190 # Shortcuts for Vim.
191 alias v='vim'
192 alias vi='vim'
193
194 # Exit binding like in Vim.
195 alias :q='exit'
196
197 # Edit the mercurial patch queue series file for the current mercurial
198 # repository in Vim. Also change Vim's pwd to the patches directory so other
199 # patches can easily be opened.
200 alias vqs='vim -c "cd $(hg root)/.hg/patches/" "$(hg root)/.hg/patches/series"'
201
202 # Make going up directories simple.
203 alias -g ...='../..'
204 alias -g ....='../../..'
205
206 # Improved ls which displays the files in columns (-C), visualizes directories,
207 # links and other special files (-F) and pages everything through less (L).
208 #
209 # If available use GNU ls with colorized output. If it isn't available use
210 # normal ls which needs CLICOLOR_FORCE so it displays colors when used with a
211 # pager.
212 ls --color &> /dev/null
213 if [[ $? -eq 0 ]]; then
214     alias ls='ls --color'
215 else
216     alias ls='CLICOLOR_FORCE=1 ls -G'
217 fi
218 # Main ls function.
219 function ls() {
220     command ls -C -F $* L
221 }
222 # Helper function to list all files.
223 function la() {
224     ls -a $*
225 }
226 # Helper function to list all files in list format with access rights, etc.
227 function ll() {
228     la -l $*
229 }
230
231 # If ^C is pressed while typing a command, add it to the history so it can be
232 # easily retrieved later and then abort like ^C normally does. This is useful
233 # when I want to abort an command to do something in between and then finish
234 # typing the command.
235 TRAPINT() {
236     # Store the current buffer in the history.
237     zle && print -s $BUFFER
238
239     # Return the default exit code so zsh aborts the current command.
240     return $1
241 }
242
243 # Execute all periodic functions every hour.
244 PERIOD=3600
245 # Display TODOs stored in ~/.todo if this file exists. Run every hour to make
246 # clear they should be completed ;-).
247 todo() {
248     if [[ -f ~/.todo ]]; then
249         cat ~/.todo | $PAGER
250     fi
251 }
252 add-zsh-hook periodic todo
253
254
255 # Load rc file for current OS.
256 source_config ~/.zsh os rc $(uname) nolocal
257 # Load rc file for current hostname (first part before a dot) or rc.local.
258 source_config ~/.zsh host rc ${$(hostname)//.*/}