-
Notifications
You must be signed in to change notification settings - Fork 0
/
.zshrc
301 lines (274 loc) · 6.65 KB
/
.zshrc
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
# This function checks whether we have a given program on the system.
_have()
{
command -v $1 &>/dev/null
}
# This function checks if interactive.
_interactive()
{
[[ $- == *i* ]]
}
# Path edit function
_pathedit ()
{
if ! echo "$1" | grep -Eq "(^|:)$2($|:)" &>/dev/null ; then
if [ -n "$1" ] ; then
if [ "$3" = "after" ] ; then
echo "$1:$2"
else
echo "$2:$1"
fi
else
echo "$2"
fi
else
echo "$1"
fi
}
# Set PATH so it includes user's private bin if it exists
[ -d ~/bin ] && PATH=$(_pathedit "$PATH" ~/bin after)
[ -d ~/.local/bin ] && PATH=$(_pathedit "$PATH" ~/.local/bin after)
# More paths
[ -d /usr/local/sbin ] && PATH=$(_pathedit "$PATH" /usr/local/sbin)
[ -d /usr/local/bin ] && PATH=$(_pathedit "$PATH" /usr/local/bin)
[ -d /usr/sbin ] && PATH=$(_pathedit "$PATH" /usr/sbin)
[ -d /usr/bin ] && PATH=$(_pathedit "$PATH" /usr/bin)
[ -d /sbin ] && PATH=$(_pathedit "$PATH" /sbin)
[ -d /bin ] && PATH=$(_pathedit "$PATH" /bin)
# Man paths
_have manpath && export MANPATH=$(manpath 2>/dev/null)
[ -d ~/man ] && MANPATH=$(_pathedit "$MANPATH" ~/man after)
[ -d ~/.local/man ] && MANPATH=$(_pathedit "$MANPATH" ~/.local/man after)
# Set colors
if _interactive && _have tput && (tput sgr0 &>/dev/null || tput me &>/dev/null) ; then
CRESET="%{$(tput sgr0 || tput me)%}" # reset all attributes
C00="%{$(tput setaf 0 || tput AF 0)%}" # black
C01="%{$(tput setaf 1 || tput AF 1)%}" # red
C02="%{$(tput setaf 2 || tput AF 2)%}" # green
C03="%{$(tput setaf 3 || tput AF 3)%}" # yellow
C04="%{$(tput setaf 4 || tput AF 4)%}" # blue
C05="%{$(tput setaf 5 || tput AF 5)%}" # magenta
C06="%{$(tput setaf 6 || tput AF 6)%}" # cyan
C07="%{$(tput setaf 7 || tput AF 7)%}" # white
fi
# Miscellaneous
export PAGER='less'
export TZ='America/New_York'
export LANG='en_US.UTF-8'
export OS_TYPE=$(uname)
bindkey -e
zstyle :compinstall filename '~/.zshrc'
autoload -Uz compinit
compinit
# Zsh history settings
HISTSIZE=2000
SAVEHIST=2000
HISTIGNORE='&:exit:*shutdown*:*reboot*'
HISTCONTROL='ignoreboth'
HISTTIMEFORMAT='%F %T '
HISTFILE=~/.zsh_history
setopt appendhistory
# Mosh alias
if _have mosh ; then
alias mssh='mosh'
fi
# SSH alias with forwarding
alias ssh='ssh -A'
alias xssh='ssh -X'
alias zssh='ssh -C'
alias xzssh='ssh -X -C'
alias issh='ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
# Set LS_COLORS
if _have dircolors ; then
if [ -r ~/.dir_colors ] ; then
eval $(dircolors -b ~/.dir_colors)
else
eval $(dircolors -b)
fi
fi
# Default to vim if vim exists
if _have vim ; then
alias vi='vim'
export EDITOR='vim'
export VISUAL='vim'
else
export EDITOR='vi'
export VISUAL='vi'
fi
# Default web browser
if [ -n "$DISPLAY" ] && _have chromium-browser ; then
export BROWSER='chromium-browser'
elif [ -n "$DISPLAY" ] && _have firefox ; then
export BROWSER='firefox'
elif _have elinks ; then
export BROWSER='elinks'
elif _have links ; then
export BROWSER='links'
fi
# MySQL prompt
if _have mysql ; then
export MYSQL_PS1='[\u@\h:\p \d]> '
fi
# Show grep in color
alias grep='grep --color=auto'
# Set HOST_NAME environment variable
export HOST_NAME=$(uname -n | cut -d . -f 1)
# OS specific settings
case $OS_TYPE in
Linux)
alias ls='ls --color=auto'
;;
CYGWIN*)
alias ls='ls --color=auto'
;;
Darwin)
alias ls='ls -G'
export CLICOLOR=1
;;
FreeBSD)
alias ls='ls -G'
export CLICOLOR=1
;;
esac
# Prompt builder and precmd
precmd()
{
local exitstatus=$?
local userprompt continueprompt title pwdcolor exitcode
# Change prompt if root or sudoed
if [ "$USER" = "root" ] ; then
userprompt="${C01}#"
continueprompt="${C01}>>"
elif [ -z "$SUDO_USER" ] ; then
userprompt="${C04}>"
continueprompt="${C04}>>"
else
userprompt="${C03}$"
continueprompt="${C03}>>"
fi
# Change title to include user if sudoed
if [ -z "$SUDO_USER" ] ; then
title="$HOST_NAME"
else
title="$USER@$HOST_NAME"
fi
# Change working directory color if writable
if [ -w "$PWD" ] ; then
pwdcolor="${C03}"
else
pwdcolor="${C05}"
fi
# Show exit status if not zero
if [ $exitstatus -ne 0 ] ; then
exitcode="${C01}[${exitstatus}]"
else
exitcode=""
fi
PS1="${CRESET}${C03}%D{%Y-%m-%dT%H:%M:%S%z}
${C07}[${C02}%n${C04}@${C02}%m${C04}:${pwdcolor}%~${C07}]${exitcode}${userprompt}${CRESET} "
PS2="${CRESET}${continueprompt}${CRESET} "
# Change title
_set_title $title
# Auto reset of ssh agent
[ -n "$TMUX" ] && reset_ssh_agent
}
# Change screen/tmux window and xterm/rxvt title names
_set_title()
{
if [ -n "$1" ] ; then
case $TERM in
screen*)
echo -ne "\ek$1\e\\"
;;
xterm*|rxvt*)
echo -ne "\e]0;$1\a"
;;
esac
fi
}
# Start SSH agent
start_ssh_agent()
{
if [ -z "$SSH_AUTH_SOCK" ] || [ ! -S "$SSH_AUTH_SOCK" ] ; then
eval $(ssh-agent -s)
trap "kill $SSH_AGENT_PID" 0
ssh-add
fi
}
# Reset SSH agent after detaching and reattaching tmux
reset_ssh_agent()
{
if [ -n "$TMUX" ] && [ ! -S "$SSH_AUTH_SOCK" ] ; then
local new_ssh_auth_sock=$(tmux showenv | grep '^SSH_AUTH_SOCK' | cut -d = -f 2)
if [ -n "$new_ssh_auth_sock" ] && [ -S "$new_ssh_auth_sock" ] ; then
SSH_AUTH_SOCK=$new_ssh_auth_sock
fi
fi
}
# Start GPG agent
start_gpg_agent()
{
if [ -z "$GPG_AGENT_INFO" ] || [ ! -S $(echo $GPG_AGENT_INFO | cut -d: -f 1) ] ; then
export GPG_TTY=$(tty)
eval $(gpg-agent --daemon)
trap "kill $(echo $GPG_AGENT_INFO | cut -d: -f 2)" 0
fi
}
# Function for extracting files
extract() {
if [ -f "$1" ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "Uknown Archive Type for '$1'"; return 2 ;;
esac
else
echo "File Not Found '$1'"
return 1
fi
}
# Shim for sudo to change TITLE and TERM
_have sudo && {
# Set sudo path env variable
[ -z "$SUDO_PATH" ] && readonly SUDO_PATH=$(command -v sudo)
sudo_shim()
{
local params suser oldterm title exitstatus
params=$@
suser=$(echo $params | grep -Eo '\-u [a-z0-9_-]+' | cut -d ' ' -f 2 | head -n 1)
oldterm=$TERM
# Change title to include user if sudoed
if [ -z "$suser" ] ; then
title="root@$HOST_NAME"
else
title="$suser@$HOST_NAME"
fi
# Change title
_set_title $title
# Set TERM to xterm
TERM='xterm'
# Execute sudo
if [ -n "$SUDO_PATH" ] && [ -x "$SUDO_PATH" ] ; then
$SUDO_PATH $params
exitstatus=$?
fi
# Reset TERM to old TERM
TERM=$oldterm
return $exitstatus
}
alias sudo='sudo_shim'
}
# Include .bashrc-env if it exists for environment specific settings
[ -r ~/.bashrc-env ] && source ~/.bashrc-env
unset -f _have
unset -f _interactive
unset -f _pathedit