summary refs log tree commit diff stats
path: root/zsh/zshrc
blob: 7d8915bb21ac0877fef5e68977436b9fd62b60a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env zsh
#
# System variables
#

if [[ $defpath == ""  && -d ~/bin ]]
then
	defpath=($path)
	path=(
		$defpath
		~/bin
	)
fi

if [[ ! -d ~/.zsh/cache ]]
then
	mkdir -p ~/.zsh/cache
fi

myfuncs=( ~alan/dotfiles/zsh/functions/*(/) )
fpath=(
	$myfuncs
	~alan/dotfiles/zsh/completion/
	$fpath
)

export PATH
export PAGER="less"
export CLICOLOR=true

#
# zsh variables
#
HISTFILE=~/.zsh/history
HISTSIZE=4000
SAVEHIST=3000

WORDCHARS='*?_-[]~=.&;!#$%^(){}<>'

#If a command takes more than 5 seconds, give statistics
REPORTTIME=5
TIMEFMT="%U user %S system %P cpu %*Es total"

#Check for user logins
watch=notme
WATCHFMT="%n has %a %l from %M at %t"

hosts=(
	server
	prefect
	alanpearce.co.uk
	home.alanpearce.co.uk
	lethalrocks.uk.to
	alphapulsar.uk.to
	st.alphapulsar.uk.to
	a.st.alphapulsar.uk.to
	st
)

users=(alan root toor)

function linkAuthSock () {
	local linkSock=~/.ssh/ssh_auth_sock
	ln -sf $1  ~/.ssh/ssh_auth_sock
	export SSH_AUTH_SOCK=$linkSock
}

if [[ "$SSH_AUTH_SOCK" != "" ]]
then
	if [[ -e ~/.ssh/ssh_auth_sock && ! -L ~/.ssh/ssh_auth_sock ]]
	then
		linkAuthSock $SSH_AUTH_SOCK
	fi
fi

if [[ "$OSTYPE" == *gnu* ]]
then
	eval `dircolors -b` #exports LSCOLORS
	ls_options="-v --group-directories-first --color=auto"
	ls_isodate="--time-style=long-iso"
elif [[ "$OSTYPE" == freebsd* ]]
then
	freebsd=1
	ls_options="-p"
	ls_isodate="-D '%F %k:%M'"
fi

#Ignore suffixes in completion
fignore=(
    svbin
)

#
# Modules
#
autoload -Uz compinit bashcompinit
autoload -U colors complist zrecompile zmv zargs zed
autoload -Uz vcs_info
autoload insert-files
zle -N insert-files
zmodload -i zsh/complist
colors
compinit -d .zsh/cache/compdump
bashcompinit

zstyle :compinstall filename '/home/alan/.zshrc'

autoload -Uz _tmux_pane_words
zle -C tmux-pane-words-prefix   complete-word _generic
zle -C tmux-pane-words-anywhere complete-word _generic
zstyle ':completion:tmux-pane-words-(prefix|anywhere):*' completer _tmux_pane_words
zstyle ':completion:tmux-pane-words-(prefix|anywhere):*' ignore-line current
zstyle ':completion:tmux-pane-words-anywhere:*' matcher-list 'b:=* m:{A-Za-z}={a-zA-Z}'

# Speed up completion by not going further than a full match
zstyle ':completion:*' accept-exact '*(N)'

zstyle ':completion:*:default' use-cache on
zstyle ':completion:*:default' cache-path ~/.zsh/cache/compcache

zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}' '+m:{A-Z}={a-z}'
zstyle ':completion:*' completer _oldlist _expand _force_rehash _complete _match

zstyle ':completion:*'              verbose    yes
zstyle ':completion:*:messages'     format     '%d'
zstyle ':completion:*:warnings'     format     'No   matches  for: %d'
zstyle ':completion:*:descriptions' format     '%U%B%d%b%u'
zstyle ':completion:*:corrections'  format     '%B%d (errors: %e)%b'
zstyle ':completion:*'              group-name ''

# Complete all user processes
zstyle ':completion:*:processes' command 'ps -au$USER'
# Add colour to process lists
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31'
zstyle ':completion:*:*:kill:*'           menu        yes    select
zstyle ':completion:*:kill:*'             force-list  always
zstyle ':completion:*:*:killall:*'        menu        yes    select
zstyle ':completion:*:killall:*'          force-list  always

# Complete unreadable log filenames
#zstyle ':completion::complete:(most|tail)::' fake-files '/var/log/*:current'

# Group manpages by section
zstyle ':completion:*' separate-sections true

# Users
#zstyle '*' users $users

zstyle '*' hosts $hosts

zstyle ':completion:*:functions' ignored-patterns '_*'
zstyle ':completion:*:cd:*'	     ignore-parents parent pwd

#This works, but isn't the same as ls
#zstyle ':completion:*:default' list-colors ''
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}

zstyle ':vcs_info:*' enable git hg

# Filename suffixes to ignore during completion (except after rm command)
zstyle ':completion:*:*:(^rm):*:*files' ignored-patterns '*?.o' '*?.c~' '*?.old' '*?.pro'

## Prompt
if [[ $term = "putty" ]];then
	prompt_char="›"
else
	prompt_char=">"
fi

PS1="%B%~%(!.%{$fg[red]%}.%{$fg[green]%})$prompt_char%f%b "

# Options

# Changing directories
setopt auto_cd

# Completion
setopt auto_list # List multiple choices on first tab
unsetopt list_ambiguous # List ambiguous choices rather than completing common prefixes
unsetopt list_beep # Don't try to beep here
setopt list_types # Indicate file types

# Expansion and globbing
setopt extended_glob # Use #~^ as part of filename patterns
unsetopt nomatch

# History
unsetopt hist_beep
setopt hist_expire_dups_first
setopt hist_fcntl_lock # Use standard locking on histfile
setopt hist_find_no_dups
setopt hist_ignore_space
setopt hist_reduce_blanks
setopt hist_verify
setopt inc_append_history

# Input/Output
unsetopt clobber
unsetopt flow_control
setopt path_dirs # Search PATH even with slashes
setopt print_exit_value

# Job Control
setopt auto_continue
unsetopt nohup
setopt long_list_jobs
unsetopt notify

# Scripts and functions
unsetopt multi_func_def
setopt multios

# Zle
unsetopt beep

############
# Keybinds #
############
#KiTTY
bindkey "OD"    backward-word
bindkey "OC"    forward-word

#General
bindkey "[1~"   beginning-of-line
bindkey "[4~"   end-of-line
bindkey "[5~"   beginning-of-history
bindkey "[6~"   end-of-history
bindkey "[2~"   quoted-insert
bindkey "[3~"   delete-char

bindkey ''      backward-delete-char

bindkey '^I'      expand-or-complete-prefix
bindkey ''    reverse-menu-complete
bindkey ''    up-line-or-search
bindkey ''    down-line-or-search

#urxvt
bindkey '[3;5~' delete-word
bindkey '' backward-word
bindkey '' forward-word

bindkey 'f'	  insert-files
bindkey 'd'     describe-key-briefly

bindkey '\'	  tmux-pane-words-prefix
bindkey '|'	  tmux-pane-words-anywhere

# Aliases before functions
alias getcflags='gcc -\#\#\# -march=native -E /usr/include/stdlib.h 2>&1 | grep "/usr/libexec/gcc/.*cc1"'
alias grep='grep --color=auto'
alias df='df -h'
alias du='du -h'
alias md='mkdir -p'
alias rd='rmdir'
alias .='source'

alias l="\ls ${ls_options} -Bp"
alias l1="\ls ${ls_options} -1"
alias ls="\ls ${ls_options} -hF"
alias la="\ls ${ls_options} -hA"
alias ll="\ls ${ls_options} ${ls_isodate} -hl"
alias lal="ll -A"
alias lla="lal"
alias llr="ll -t"
alias gpp='g++'
alias lsr="ls -tld *(m-2)" # mtime < -2days

alias trans="transmission-remote transmission.home"
alias bitcoin="bitcoind"
alias namecoin="~alan/applications/namecoin/src/namecoind"
alias e="$EDITOR"
alias E="SUDO_EDITOR=\emacsclient -c -a emacs\" sudoedit"
alias aticonfig="DISPLAY=:0 \aticonfig"
alias wprop="xprop | egrep WM_(CLASS|NAME|WINDOW_ROLE|TYPE)"

alias -g ...='../..'

#Suffix aliases
alias -s log=less

if [[ -e =hub ]]; then
	alias git="hub"
fi

# Functions

precmd () {
    [[ -t 1 ]] || return
    case $TERM in
		*xterm*|rxvt|putty|(dt|k|E)term|screen*) print -Pn "\e]2;%~\a"
		;;
	esac
}

pid () {
    local i
    for i in /proc/<->/stat
    [[ "$(< $i)" = *\((${(j:|:)~@})\)* ]] && echo $i:h:t
    return 0
}

_force_rehash() {
    (( CURRENT == 1 )) && rehash
    return 1        # Because we didn't really complete anything
}

portgrep() {
    sudo lsof -i :$1 -nP | egrep '(LISTEN|UDP|COMMAND)'
}

ipgrep() {
    sudo lsof -i @$1 -nP
}

smart_sudo () {
    if [[ -n $1 ]]; then
        \sudo $argv
    else
        #if no parameters were given, then assume we want a root shell
        \sudo -i -u toor
    fi
}

compdef _sudo smart_sudo

smart_compile () {
	local zcompargs
	if [[ $1 = *zshrc* ]]; then
		zcompargs="-R"
	fi

	if [[ $EUID -ne 0 || $1 != *alan* ]]; then
		zrecompile -q -p $zcompargs $1
	fi
	if [[ -n $2 ]]; then
		. $1
	fi
}

host=${HOST%%.*}
if [[ -e ~alan/dotfiles/zsh/zshrc_$host ]]; then
	# Don't recompile as toor|root
	smart_compile ~alan/dotfiles/zsh/zshrc_$host 1
fi
if [[ -e /usr/bin/emerge ]]; then
	smart_compile ~alan/dotfiles/zsh/zshrc_gentoo 1
elif [[ $freebsd -eq 1 ]]; then
	smart_compile ~alan/dotfiles/zsh/zshrc_freebsd 1
fi

for fp in $myfuncs; do
	zrecompile -p $fp $fp/*
done

_FASD_DATA="$HOME/.zsh/fasd-data"
fasd_cache="$HOME/.zsh/cache/fasd-init-zsh"
autoload -U fasd
if [[ ! -s "$fasd_cache" || ~alan/dotfiles/zsh/functions/fasd -nt "$fasd_cache" ]]; then
	fasd --init posix-alias zsh-hook zsh-ccomp zsh-ccomp-install zsh-wcomp zsh-wcomp-install >! "$fasd_cache"
fi
smart_compile "$fasd_cache" 1
unset fasd_cache
alias e="f -e $EDITOR"

smart_compile ~/.zshrc
alias s="smart_sudo "