-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbash_source_me
More file actions
368 lines (332 loc) · 9.48 KB
/
bash_source_me
File metadata and controls
368 lines (332 loc) · 9.48 KB
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
#!/bin/bash
# Note 1: Add this line `source ~/configurations/bash_source_me` in
# ~/.bash_profile (for MacOSX) or ~/.bashrc (for Linux)
#
# Note 2: Since MacOSX and Linux use ~/.bashrc and ~/.bash_profile in different
# ways, add the following lines to ~/.bash_profile to make sure ~/.bashrc is
# loaded correctly.
# ```
# if [ -f ~/.bashrc ]; then
# source ~/.bashrc
# fi
# ```
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
platform='unknown'
unamestr=`uname`
if [[ "$unamestr" == 'Linux' ]]; then
if grep -q Microsoft /proc/version; then
subPlatform='WSL'
fi
platform='Linux'
elif [[ "$unamestr" == 'Darwin' ]]; then
platform='Darwin'
fi
# START: WSL
if [[ $subPlatform == 'WSL' ]]; then
if [ "$(umask)" == '0000' ]; then
umask 0022
fi
fi
# END: WSL
# START: path
PATH=~/configurations/bin\
:~/bin\
:$PATH
if [[ $platform == 'Darwin' ]]; then
# PATH for Homebrew
PATH=/usr/local/sbin:$PATH
PATH=/opt/homebrew/bin:$PATH
# PATH for python/python3/pip/pip3
PATH=$PATH:~/Library/Python/2.7/bin
fi
# END: path
# START: terminal/ls color
# Use https://geoff.greer.fm/lscolors/ to convert the format.
export CLICOLOR=1
if [[ $platform == 'Linux' ]]; then
export LS_COLORS="\
di=36:\
ln=35:\
so=30:\
pi=33:\
ex=32:\
bd=34;46:\
cd=34;43:\
su=30;41:\
sg=30;46:\
tw=30;42:\
ow=30;43"
elif [[ $platform == 'Darwin' ]]; then
export LSCOLORS=gxfxaxdxcxegedabagacad
fi
# END: terminal/ls color
# START: default language
if [[ $platform == 'Linux' ]]; then
export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
fi
# END: default language
# START: bash-completion
# Use homebrew's bash completion script:
if [ -x "`which brew`" ]; then
GIT_COMPLETION_BASH=$(brew --prefix)/etc/bash_completion.d/git-completion.bash
if [ -f ${GIT_COMPLETION_BASH} ]; then
. ${GIT_COMPLETION_BASH}
fi
AG_COMPLETION_BASH=$(brew --prefix)/etc/bash_completion.d/ag.bashcomp.sh
if [ -f ${AG_COMPLETION_BASH} ]; then
. ${AG_COMPLETION_BASH}
fi
TIG_COMPLETION_BASH=$(brew --prefix)/etc/bash_completion.d/tig-completion.bash
if [ -f ${TIG_COMPLETION_BASH} ]; then
. ${TIG_COMPLETION_BASH}
fi
fi
AG_COMPLETION_BASH_NIX=~/.nix-profile/share/bash-completion/completions/ag.bashcomp.sh
if [ -f ${AG_COMPLETION_BASH_NIX} ]; then
. ${AG_COMPLETION_BASH_NIX}
fi
ARIA2C_COMPLETION_BASH_NIX=~/.nix-profile/share/bash-completion/completions/aria2c
if [ -f ${ARIA2C_COMPLETION_BASH_NIX} ]; then
. ${ARIA2C_COMPLETION_BASH_NIX}
fi
if [[ $SHELL == */bash ]]; then
GIT_COMPLETION_BASH_NIX=~/.nix-profile/share/bash-completion/completions/git
if [ -f ${GIT_COMPLETION_BASH_NIX} ]; then
. ${GIT_COMPLETION_BASH_NIX}
fi
TIG_COMPLETION_BASH_NIX=~/.nix-profile/share/bash-completion/completions/tig
if [ -f ${TIG_COMPLETION_BASH_NIX} ]; then
. ${TIG_COMPLETION_BASH_NIX}
fi
fi
TMUX_COMPLETION_BASH_NIX=~/.nix-profile/share/bash-completion/completions/tmux
if [ -f ${TMUX_COMPLETION_BASH_NIX} ]; then
. ${TMUX_COMPLETION_BASH_NIX}
fi
# END: git-completion
# START: powerline-shell for bash
if [[ $SHELL == */bash ]]; then
function _update_ps1() {
PS1=$(powerline-shell $?)
}
if [[ -x "`which powerline-shell`" && $TERM != linux && ! $PROMPT_COMMAND =~ _update_ps1 ]]; then
PROMPT_COMMAND="_update_ps1; $PROMPT_COMMAND"
fi
fi
# END: powerline-shell for bash
# START: bash auto-complete simulation for zsh
if [[ $SHELL == */zsh ]]; then
setopt complete_in_word
bindkey '^I' expand-or-complete-prefix
fi
# END: bash auto-complete simulation for zsh
# START: powerline-shell for zsh
if [[ $SHELL == */zsh ]]; then
function powerline_precmd() {
PS1="$(powerline-shell --shell zsh $?)"
}
function install_powerline_precmd() {
for s in "${precmd_functions[@]}"; do
if [ "$s" = "powerline_precmd" ]; then
return
fi
done
precmd_functions+=(powerline_precmd)
}
if [ "$TERM" != "linux" -a -x "$(command -v powerline-shell)" ]; then
install_powerline_precmd
fi
fi
# END: powerline-shell for zsh
# START: CUDA
if [[ $platform == 'Linux' ]]; then
export CUDA_HOME=/usr/local/cuda
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:${CUDA_HOME}/lib64:${CUDA_HOME}/extras/CUPTI/lib64"
export PATH="$CUDA_HOME/bin:$PATH"
elif [[ $platform == 'Darwin' ]]; then
export CUDA_HOME=/usr/local/cuda
export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:$CUDA_HOME/lib"
export PATH="$CUDA_HOME/bin:$PATH"
fi
# END: CUDA
# START: rust
# rust package management
if [ -f ~/.cargo/env ]; then
source ~/.cargo/env
fi
# END: rust
# START: helper
stack() {
op=$1
name=$2
value=$3
file=/tmp/bash_stack_$name.$$
case $op in
push)
echo $value >> $file
;;
pop)
if [ -f $file ]; then
tail -1 $file
sed -i DME -e '$d' $file
rm ${file}DME
fi
;;
esac
}
mygrepall() { grep -rn ./ -e $1;}
# END: helper
# START: fzf
if command -v fd &> /dev/null; then
export FZF_DEFAULT_COMMAND="fd --exclude={.git,.idea,.vscode,.sass-cache,node_modules,build,tmp} --type f"
fi
# END
# START: td
# Use `td` to show a notification in OS. It can be used with `;` to combine it
# with other commands, e.g. `git remote update; td`
# Use `tdbg` to show a notification after the background job applied. It can
# be used with `[ctrl-z]` to put the job in the background and show a
# notification after it's finished.
alias td="tnot \$?"
tdbg() {
OUTPUT=`jobs -l | grep '^\[[0-9]*\]+' | sed -n 's/^\[\([0-9]*\)\]+/\1/p'`
JOB_ID=`echo ${OUTPUT} | awk '{print $1}'`
PID=`echo ${OUTPUT} | awk '{print $2}'`
fg $JOB_ID
wait $PID
td
}
# END: td
# START: Bash as default
# See https://support.apple.com/en-au/HT208050
export BASH_SILENCE_DEPRECATION_WARNING=1
# END
# START: alias
alias kUdFuse="kill `ps aux | grep udFuse | grep -v grep | awk '{print $2}'`"
alias mydu="du -sch .[!.]* * |sort -h"
# Force tmux to assume the terminal supports 256 colours.
if [[ $platform == 'Linux' ]]; then
alias tmux="tmux -2"
fi
# alias du_cc='du -sh'
# alias du_sf='du -d1 -h'
if [[ $platform == 'Linux' ]]; then
alias pbcopy="xclip -selection clipboard"
alias pbpaste="xclip -selection clipboard -o"
fi
alias gl="git log --no-merges --first-parent"
alias gltig="tig --no-merges --first-parent"
# END: alias
kDS_Store() {
find . -name '*.DS_Store' -type f -delete
}
lDS_Store() {
find . -name '*.DS_Store' -type f
}
kDot_() {
find . -name '._*' -type f -delete
}
lDot_() {
find . -name '._*' -type f
}
kDD() {
kDS_Store
kDot_
}
lDD() {
lDS_Store
lDot_
}
kWatchmanCookie() {
find . -name '.watchman-cookie*' -type f -delete
}
lWatchmanCookie() {
find . -name '.watchman-cookie*' -type f
}
# START: stopwatch
alias stopwatch='set -f;_stopwatch_'
_stopwatch_() { command ~/configurations/bin/stopwatch "$@";set +f; }
# END: stopwatch
# START: synergy
if [[ $platform == 'Linux' ]]; then
alias synergy-stop="systemctl stop synergy"
alias synergy-start="systemctl start synergy"
elif [[ $platform == 'Darwin' ]]; then
alias synergy-stop="launchctl unload /Library/LaunchAgents/com.symless.synergy.synergy-service.plist"
alias synergy-start="launchctl load /Library/LaunchAgents/com.symless.synergy.synergy-service.plist"
fi
# END: synergy
# START: rtfm
# Reference:
# https://www.commandlinefu.com/commands/view/7581/rtfm-function
rtfm() { help $@ || man $@ || open "http://www.google.com/search?q=$@"; }
# END: rtfm
# START: weather
# Reference:
# https://www.commandlinefu.com/commands/view/17709/nice-weather-forecast-on-your-shell
weather() { curl -s wttr.in/$1; }
# END: weather
# START: showWhosUsingNet
# Reference:
# https://www.commandlinefu.com/commands/view/3542/show-apps-that-use-internet-connection-at-the-moment.-multi-language
showWhosUsingNet() { lsof -P -i -n; }
# END: showWhosUsingNet
dict() { ydict.js $1 | less -R; }
# START: esp-idf
PATH=$PATH:$HOME/Projects/esp32_esp-idf/xtensa-esp32-elf/bin
export IDF_PATH=~/Projects/esp32_esp-idf/esp-idf
# END: esp-idf
# START: dasht
export PATH=$PATH:~/Projects/tools/dasht/bin
export MANPATH=~/Projects/tools/dasht/man:$MANPATH
# END: dasht
# START: clear-console
ccc() {
clear
tmux clear-history
}
# END: clear-console
# START: movtogif
# Reference:
# https://gist.github.com/dergachev/4627207#gistcomment-1407037
movtogif(){
ffmpeg -i "$1" -vf scale=800:-1 -r 10 -f image2pipe -vcodec ppm - |\
convert -delay 5 -layers Optimize -loop 0 - "$2"
}
# END: movtogif
# START: canibuydomain
# Reference:
# https://www.commandlinefu.com/commands/view/31767/check-if-a-domain-is-available-for-purchase
function canibuydomain {
whois "$1" 2>/dev/null | grep -q 'Registrant' && echo "taken" || echo "available"
}
# END: canibuydomain
# START: audiomixer
# PulseAudio basically sits atop ALSA, and use it internally. ALSA is unable by
# itself to be used by multiple applications, so PulseAudio provides this
# functionality among others.
#
# Linux Audio System Hierarchy:
#
# [ Application Layer - media players, web browsers, etc. ]
#
# [ PulseAudio Layer - a software proxy providing additional featues ]
# [ (mixing, equalizer) between your application and the ALSA/OSS ]
# [ subsystem. ]
#
# [ ALSA (driver) - dealing with the hardware, basically owning it. ]
#
# [ Hardware - Sound hardware and network adapter for other pulse ]
# [ servers or applications listening and broadcasting over RTP. ]
#
# Reference:
# https://askubuntu.com/questions/581128/what-is-the-relation-between-alsa-and-pulseaudio-sound-architecture
if [[ $platform == 'Linux' ]]; then
audiomixer() {
# alsamixer is for controlling lower layer.
pavucontrol
}
fi
# END: audiomixer