-
Notifications
You must be signed in to change notification settings - Fork 4
/
.shrc
439 lines (359 loc) · 10.4 KB
/
.shrc
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# ~/.shrc - bourne shell startup file
#
# https://github.com/agkozak/dotfiles
#
# This file will be used if the shell is invoked for interactive use and
# the environment variable ENV is set to this file. It is sourced at the
# beginnning of .zshrc and .bashrc
#
# shellcheck shell=sh disable=SC1090,SC1091,SC1094,SC2016,SC2034,SC2153
# Only run this script when shell is interactive {{{1
case $- in
*i*) ;;
*) exit ;;
esac
# }}}1
# AGKDOT_SYSTEMINFO {{{1
export AGKDOT_SYSTEMINFO
: "${AGKDOT_SYSTEMINFO:=$(uname -a)}"
# }}}1
# Source system-wide files {{{1
case $AGKDOT_SYSTEMINFO in
NetBSD*) [ -f /etc/shrc ] && . /etc/shrc ;;
esac
case $KSH_VERSION in
*'LEGACY KSH'*|*'MIRBSD KSH'*) [ -s /etc/mkshrc ] && . /etc/mkshrc ;;
esac
# }}}1
# Begin .shrc benchmark {{{1
if [ "${AGKDOT_BENCHMARKS:-0}" -eq 1 ] &&
[ "${AGKDOT_ZSHRC_LOADING:-0}" -ne 1 ] &&
# BusyBox date doesn't work in nanoseconds
[ ! -h /bin/date ]; then
case $AGKDOT_SYSTEMINFO in
# Neither does BSD date
*BSD*|DragonFly*) ;;
*) AGKDOT_SHRC_START="$(/bin/date +%s%N)" ;;
esac
fi
# }}}1
# Raspberry Pi LXTerminal {{{1
# It's important to set this up early so that dircolors uses the right number of
# colors
if [ "$XDG_CURRENT_DESKTOP" = 'LXDE' ] && [ "$TERM" = 'xterm' ]; then
TERM='xterm-256color' # LXTerminal can't set TERM
fi
# }}}1
# Aliases {{{1
# enable color support of ls and also add handy aliases {{{2
# .zshrc sets AGKDOT_TERM_COLORS from terminfo[colors]
if [ -z "$ZSH_VERSION" ]; then
if [ "$TERM" != "$AGKDOT_ORIG_TERM" ] || [ -z "$AGKDOT_TERM_COLORS" ]; then
export AGKDOT_TERM_COLORS
case $TERM in
*-256color) AGKDOT_TERM_COLORS=256 ;;
vt100|vt220|dumb) AGKDOT_TERM_COLORS=-1 ;;
*)
if type tput > /dev/null 2>&1; then
case ${AGKDOT_SYSTEMINFO} in
FreeBSD*|DragonFly*) AGKDOT_TERM_COLORS="$(tput Co)" ;;
UWIN*) AGKDOT_TERM_COLORS="$(tput cols)" ;;
*) AGKDOT_TERM_COLORS="$(tput colors)" ;;
esac
fi
;;
esac
fi
fi
# if /bin/ls is a symlink, then it's BusyBox ls
if [ -h /bin/ls ]; then
AGKDOT_IS_BUSYBOX_LS=1
fi
if [ "${AGKDOT_IS_BUSYBOX_LS:-0}" -eq 0 ]; then
case $AGKDOT_SYSTEMINFO in
NetBSD*|OpenBSD*|UWIN*) ;;
SunOS*|FreeBSD*) LS_OPTIONS='--color' ;;
Darwin*|*DragonFly*)
if [ "${AGKDOT_TERM_COLORS:-0}" -ge 8 ]; then
CLICLOR=1
LSCOLORS='ExfxcxdxBxegedAbagacad'
LS_OPTIONS='-G'
fi
;;
# GNU ls
*)
LS_OPTIONS='--group-directories-first -F -h -T 0'
[ "${AGKDOT_TERM_COLORS:-0}" -ge 8 ] && LS_OPTIONS="${LS_OPTIONS} --color"
case $AGKDOT_SYSTEMINFO in
*Msys|*Cygwin|*[Mm]icrosoft*)
LS_OPTIONS="${LS_OPTIONS} --hide=NTUSER* --hide=ntuser*"
;;
esac
;;
esac
else
[ "${AGKDOT_TERM_COLORS:-0}" -ge 8 ] && LS_OPTIONS=--color
fi
alias ls='/bin/ls ${LS_OPTIONS}'
# shellcheck disable=SC2153
if [ -n "$INSIDE_EMACS" ]; then
unset LS_COLORS
# If dircolors is available, install.sh will have installed ~/.dircolors
# Try not to run dircolors more than once unless TERM has changed
elif [ -f "${HOME}/.dircolors" ]; then
if [ "$TERM" != "$AGKDOT_ORIG_TERM" ] || [ -z "$LS_COLORS" ]; then
case $AGKDOT_SYSTEMINFO in
SunOS*) eval "$(dircolors -b)" ;;
*)
if [ -f "${HOME}/.dircolors" ] && [ "${AGKDOT_TERM_COLORS:-0}" -ge 256 ]; then
eval "$(dircolors -b "${HOME}/.dircolors")"
elif [ "${AGKDOT_TERM_COLORS:-0}" -ge 8 ]; then
eval "$(dircolors -b)"
fi
;;
esac
fi
fi
export LS_COLORS AGKDOT_ORIG_TERM
AGKDOT_ORIG_TERM=$TERM
case $AGKDOT_SYSTEMINFO in
*ish*|UWIN*) ;;
*solaris*)
alias dir='dir --color'
alias vdir='vdir --color'
case $AGKDOT_SYSTEMINFO in
*joyent*)
alias grep='grep --color'
alias fgrep='fgrep --color'
alias egrep='egrep --color'
;;
esac
;;
*)
if [ "${AGKDOT_IS_BUSYBOX_LS:-0}" -eq 0 ] &&
[ "${AGKDOT_TERM_COLORS:-0}" -ge 8 ]; then
alias dir='dir --color'
alias vdir='vdir --color'
alias grep='grep --color'
alias fgrep='fgrep --color'
alias egrep='egrep --color'
fi
;;
esac
# Zenburn-like colors for GNU grep
export GREP_COLORS
GREP_COLORS='sl=49;39:cx=49;39:mt=48;5;22;38;5;230;1:fn=49;38;5;224:ln=49;38;5;78:bn=49;38;5;78:se=49;38;5;116';
unset AGKDOT_BUSYBOX_LS
# }}}2
# some more ls aliases {{{2
alias l='ls -CF'
alias la='ls -A'
alias lart='ls -1Fcart'
alias ldot='ls -ld .*'
alias ll='ls -alF'
alias lr='ls -tRFh' # sorted by date, recursive, show type, human-readable
alias lrt='ls -1Fcrt'
alias lt='ls -ltFh' # long list, sorted by date, show type, human-readable
alias lS='ls -1FSsh'
# }}}2
# BusyBox less doesn't pay attention to $LESS
[ -h /bin/less ] && alias less='less -IMR'
# _agkdot_is_uwin() {
# case $AGKDOT_SYSTEMINFO in
# UWIN*) return 0 ;;
# *) return 1 ;;
# esac
# }
case $KSH_VERSION in
*R55*) ;; # mksh version R55 erroneously does not allow these aliases
*)
alias ..='cd ..'
alias ...='cd ../..'
;;
esac
# if command -v composer.phar > /dev/null 2>&1 &&
# ! command -v composer > /dev/null 2>&1; then
# alias composer='composer.phar'
# fi
# command -v dos2unix > /dev/null 2>&1 || command -v perl > /dev/null 2>&1 &&
# alias dos2unix='perl -p -i -e "s/\\r\\n/\\n/g"'
############################################################
# If a given command exists, make an alias for it
#
# Arguments
# $1 The command
# $2 The alias
############################################################
# _agkdot_cond_aliases() {
# until [ $# = 0 ]; do
# command -v "$1" > /dev/null 2>&1 && alias "$2"
# shift
# shift
# done
# }
# command -v can be slow
alias github-stats='github-stats --token ${GITHUB_STATS_TOKEN}'
alias bri='hue set all -b'
[ "$EDITOR" = vim ] && alias vi='vim'
alias yta='youtube-dl --audio-format best -x '
alias bat='batcat'
# unset -f _agkdot_cond_aliases
# System-specific aliases {{{2
if [ -x /usr/bin/winpty ] || [ -x /usr/local/bin/winpty ]; then
AGKDOT_WINPTY=1
fi
case $AGKDOT_SYSTEMINFO in
*Msys|*Cygwin)
if [ "${AGKDOT_WINPTY:-0}" -eq 1 ]; then
alias node='winpty node'
alias php='winpty php'
# Workaround for https://github.com/msys2/MSYS2-packages/issues/1568#issuecomment-847004378
alias choco='temp= tmp= winpty choco'
alias cinst='temp= tmp= winpty cinst'
alias cuninst='temp= tmp= winpty cuninst'
alias cup='temp= tmp= winpty cup'
alias eslint='winpty ${APPDATA}/npm/eslint.cmd'
fi
alias ghci='ghcii.sh'
;;
esac
case $AGKDOT_SYSTEMINFO in
*Msys)
if [ "${AGKDOT_WINPTY:-0}" -eq 1 ]; then
alias npm='winpty /c/Program\ Files/nodejs/npm.cmd'
alias wp='winpty php $HOME/bin/wp-cli.phar --no-color'
fi
alias gvim='/c/Windows/gvim.bat'
;;
*Cygwin)
alias composer='php $(cygpath -w "${HOME}/bin/composer.phar")'
if [ "${AGKDOT_WINPTY:-0}" -eq 1 ]; then
alias npm='winpty /cygdrive/c/Program\ Files/nodejs/npm.cmd'
fi
alias gvim='/cygdrive/c/Windows/gvim.bat'
;;
*CYGWIN_NT-10.0-WOW*|*[Mm]icrosoft*)
tmux='tmux -S "${HOME}/.tmsock"'
;;
*raspberrypi*)
alias temp='/opt/vc/bin/vcgencmd measure_temp | egrep "[0-9.]{4,}" -o'
;;
esac
# WSL aliases {{{3
case $AGKDOT_SYSTEMINFO in
*[Mm]icrosoft*)
alias explorer='explorer.exe'
alias subl='subl.exe'
alias wsl='wsl.exe'
;;
esac
# }}}3
# }}}2
# }}}1
# Prompt {{{1
# If possible, load the pan-POSIX Polyglot Prompt
# But not in Zsh -- it uses agkozak-zsh-prompt
############################################################
# Are we using FreeBSD sh?
############################################################
_agkdot_is_bsd_sh() {
case $AGKDOT_SYSTEMINFO in
FreeBSD*)
case ${0#-} in
sh) return 0 ;;
*) return 1 ;;
esac
;;
*) return 1 ;;
esac
}
if [ -z "$ZSH_VERSION" ] ||
[ "$0" = 'bash' ] ||
[ "$0" = 'ksh' ] ||
[ "$0" = 'sh' ] && ! _agkdot_is_bsd_sh; then
if [ -f "${HOME}/dotfiles/prompts/polyglot/polyglot.sh" ]; then
# shellcheck source=/dev/null
. "${HOME}/dotfiles/prompts/polyglot/polyglot.sh"
fi
fi
# }}}1
# Miscellaneous {{{1
# search path for cd
# CDPATH=:$HOME
# For ksh93/mksh
# vi-style line editing
# Zsh handles this itself
[ -z "$ZSH_VERSION" ] && set -o vi
case $KSH_VERSION in
*93*)
# shellcheck disable=SC3040
set -o globstar
# Search path for autoloadable functions
FPATH='/usr/share/ksh/functions:~/.func'
;;
*'PD KSH'*)
# shellcheck disable=SC3040
set -o vi-tabcomplete
;;
esac
HISTSIZE=10000
# If it's not really zsh emulating ksh
if [ -z "$ZSH_VERSION" ]; then
case $KSH_VERSION in
*'PD KSH'*) HISTFILE="${HOME}/.pdksh_history" ;;
*'LEGACY KSH'*|*'MIRBSD KSH'*) HISTFILE="${HOME}/.mksh_history" ;;
*) HISTFILE="${HOME}/.ksh_history" ;;
esac
fi
# }}}1
# find shorthand {{{1
f() {
find . -name "$1" 2>&1 | grep -v 'Permission denied'
}
# }}}1
# Pull in updates from the dotfiles repo and install them {{{1
update_dotfiles() {
[ -n "$ZSH_VERSION" ] && setopt NO_WARN_CREATE_GLOBAL \
NO_WARN_NESTED_VAR > /dev/null 2>&1
CUR_DIR="$PWD"
printf '%s' 'Updating dotfiles: '
cd "${HOME}/dotfiles" || exit
git pull && ./install.sh
# shellcheck disable=SC2044
for i in $(find . -name '.git'); do
i=${i#\./}
[ "$i" = '.git' ] && continue
i=${i%/\.git}
printf 'Updating %s: ' "${i##*/}"
cd "$i" || exit
cd ..
git pull
cd "${HOME}/dotfiles" || exit
done
cd "$CUR_DIR" || exit
unset CUR_DIR i
}
# }}}1
# Disable flow control -- allows use of Ctrl-Q as tmux/screen prefix {{{1
# zsh might be running in ksh emulation mode
if [ "$0" = 'ksh' ] || [ -z "$ZSH_VERSION" ]; then
stty -ixon
fi
# }}}1
# Source ~/.shrc.local {{{1
if [ -f "${HOME}/.shrc.local" ]; then
. "${HOME}/.shrc.local" && AGKDOT_SHRC_LOCAL=1
fi
# }}}1
# End .shrc benchmark {{{1
if [ -n "$AGKDOT_SHRC_START" ]; then
AGKDOT_SHRC_FINISH="$(/bin/date +%s%N)"
AGKDOT_DIFFERENCE=$(( AGKDOT_SHRC_FINISH - AGKDOT_SHRC_START ))
printf '%s' '.shrc '
[ -n "$AGKDOT_SHRC_LOCAL" ] && printf '%s' 'and .shrc.local '
printf '%s\n' "loaded in $((AGKDOT_DIFFERENCE/1000000))ms."
fi
unset AGKDOT_SHRC_START AGKDOT_SHRC_FINISH AGKDOT_DIFFERENCE AGKDOT_SHRC_LOCAL
unset -f _agkdot_is_bsd_sh # _agkdot_winpty_alias _agkdot_is_uwin
# }}}1
# vim: ft=sh:fdm=marker:ts=2:et:sw=2:ai:sts=2