-
Notifications
You must be signed in to change notification settings - Fork 2
/
shell-cfg.sh
448 lines (398 loc) · 11.8 KB
/
shell-cfg.sh
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
440
441
442
443
444
445
446
447
448
#!/usr/bin/env sh
# coding:utf-8 vi:et:ts=2
# Some tools like VSCode tend to spawn subshells like "zsh -c -l". On macOS
# zsh will source /etc/zprofile which runs /usr/libexec/path_helper and
# REORDERS $PATH, moving "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
# in the beginning. For pyenv/rbenv/etc to work correctly they should be
# in $PATH before default path, so we need to also reorder for each
# invokation of zsh and each execution of this script.
start_path_with() {
if ! [ -e $1 ]; then
return
fi
NEW_PATH=""
for PATH_LINE in $(echo $PATH | tr ":" "\n"); do
if [ "$PATH_LINE" != "$1" ]; then
if [ -n "$NEW_PATH" ]; then
NEW_PATH=$NEW_PATH:$PATH_LINE
else
NEW_PATH=$PATH_LINE
fi
fi
done
PATH=$1:$NEW_PATH
unset NEW_PATH
}
## bash/zsh portable way to encode "Escape" character.
PS_ESC=$(printf '\033')
## Don't show git in PS by default, it conflicts with the "g" alias.
PS_GIT_ON=
if [ -n "$ZSH_VERSION" ]; then
## Colors should be escaped for correct length calculation.
PS_K="%{${PS_ESC}[30;47m%}"
PS_W="%{${PS_ESC}[37;49m%}"
PS_R="%{${PS_ESC}[31;49m%}"
PS_G="%{${PS_ESC}[32;49m%}"
PS_B="%{${PS_ESC}[34;49m%}"
PS_Y="%{${PS_ESC}[33;49m%}"
PS_M="%{${PS_ESC}[35;49m%}"
PS_C="%{${PS_ESC}[36;49m%}"
PS_N="%{${PS_ESC}[0m%}"
PS_WORKDIR="%~"
PS_DOLLAR="$"
## Substring re-interpolation.
setopt promptsubst
## Do not display "no matches found" error for blobs
setopt +o nomatch
## Always complete files, even if app-specific completion don't says so
zstyle ':completion:*' completer _complete _ignored _files
elif [ -n "$BASH_VERSION" ]; then
## Colors should be escaped for correct length calculation.
PS_K="\\[${PS_ESC}[30;47m\\]"
PS_W="\\[${PS_ESC}[37;49m\\]"
PS_R="\\[${PS_ESC}[31;49m\\]"
PS_G="\\[${PS_ESC}[32;49m\\]"
PS_B="\\[${PS_ESC}[34;49m\\]"
PS_Y="\\[${PS_ESC}[33;49m\\]"
PS_M="\\[${PS_ESC}[35;49m\\]"
PS_C="\\[${PS_ESC}[36;49m\\]"
PS_N="\\[${PS_ESC}[0m\\]"
PS_WORKDIR="\\W"
PS_DOLLAR="\\\$"
else
echo "Unsupported shell"
fi
## Disable terminal/ssh freeze with C-S:
stty -ixon
## Don't create |.pyc| files while executing python code from console.
export PYTHONDONTWRITEBYTECODE=1
## Rust 'install' places binaries here
start_path_with ~/.cargo/bin
## pipx installs binaries here
start_path_with ~/.local/bin
## For custom nodejs build (ubuntu have old one in repository)
start_path_with ~/.local/nodejs/bin
## git can clone from repos without certificates.
export GIT_SSL_NO_VERIFY=true
## 256-colors in terminal for apps that knows how to use it.
export TERM=xterm-256color
## Used by apps to launch text editor.
export EDITOR=vim
## Required for VIM, otherwise it will start creating dirs names '$TMP'.
if [ -z $TMP ]; then
export TMP=~/tmp
fi
if [ -z $TEMP ]; then
export TEMP=~/tmp
fi
## gnome-ssh-askpass don't work.
unset SSH_ASKPASS
## make less display ASCII colors, quit if one screen and don't clear
## screen after it quits.
export LESS="-R -F -X"
## Don't display lein root warning while using in docker
export LEIN_ROOT=true
## For docker containers where they are not set
export LANG="en_US.UTF-8"
export LANGUAGE="en_US:en"
export LC_ALL="en_US.UTF-8"
## Switchable truecolor for VIM, on by default
export TRUECOLOR="1"
## Always install dependencies in .venv for poetry.
export POETRY_VIRTUALENVS_IN_PROJECT="1"
## Always install dependencies in .venv for pipenv.
export PIPENV_VENV_IN_PROJECT="1"
## Do not lock dependencies (very slow).
export PIPENV_SKIP_LOCK="1"
## Caprover default branch
export CAPROVER_DEFAULT_BRANCH=main
## OSX?
if [ "$(uname)" = "Darwin" ]; then
## Add color to |ls| output
export CLICOLOR=1
## Better 'ls' output colors.
export LSCOLORS=Exfxcxdxbxegedabagacad
## For django 'syncdb' command to work.
export LC_ALL=en_US.UTF-8
## |PYTHONDONTWRITEBYTECODE| don't work on OSX 10.8, default python is
## 64-bit that is not compatible with wxWidgets.
# alias python="arch -i386 /usr/bin/python2.7 -B"
## custom svn
start_path_with /opt/subversion/bin
## MacOS Apple Silicon homebrew installed?
if [ -e /opt/homebrew/bin/brew ]; then
eval $(/opt/homebrew/bin/brew shellenv)
start_path_with "/opt/homebrew/bin"
start_path_with "/opt/homebrew/sbin"
fi
## custom mongo installed?
MONGOAPP=~/Applications/MongoDB.app
MONGOBINDIR=$MONGOAPP/Contents/Resources/Vendor/mongodb
if [ -e $MONGOBINDIR/mongo ]; then
alias mongo=$MONGOBINDIR/mongo
alias mongodump=$MONGOBINDIR/mongodump
alias mongorestore=$MONGOBINDIR/mongorestore
fi
## Will write to stderr if Java is not installed
export JAVA_HOME=$(/usr/libexec/java_home 2>/dev/null)
## brew install android-commandlinetools
export ANDROID_HOME=/opt/homebrew/share/android-commandlinetools
## Installed here by 'brew install michaeldfallen/formula/git-radar'
if [ "$SHELL" = "/bin/zsh" ]; then
export RADAR_CMD='$(git-radar --zsh --fetch)'
else
export RADAR_CMD='$(git-radar --bash --fetch)'
fi
else
## Remap caps lock to backspace.
# gsettings set org.gnome.desktop.input-sources xkb-options "['caps:backspace']"
##FIXME: Seems not persisting on Ubuntu, need to check why.
# setterm -background black -foreground white
## GTK_IM_MODULE is set to 'xim' in ubuntu, lots of GTK errors in chrome.
## Disable disk cache so multiple chrome instances will not kill HDD.
CHROME_BIN=/opt/google/chrome/chrome
alias chrome='GTK_IM_MODULE="" $CHROME_BIN --disk-cache-size=10000000'
## android studio
start_path_with ~/.local/android-studio/bin
if [ -e /usr/java/latest ]; then
## Official SDK symlinks this to lates install.
export JAVA_HOME=/usr/java/latest
fi
export RADAR_CMD='$(~/.git-radar/git-radar --bash --fetch)'
fi
if [ -e ~/.rvm/scripts/rvm ]; then
source ~/.rvm/scripts/rvm
fi
## git aliases
alias g=git
## svn aliases
alias svl='svn log'
alias svs='svn stat'
alias svc='svn commit -m'
svd() {
svn diff --diff-cmd colordiff $@ | less -R
}
## "HOST HOME" for Windows "home" on WSL
if [ -e /proc/sys/fs/binfmt_misc/WSLInterop ]; then
HHOME=$(wslpath $(cmd.exe /C "echo %USERPROFILE%" 2>/dev/null))
## Remove '\r' from the end of the 'cmd.exe' output
export HHOME=$(echo $HHOME | tr -d '\r')
else
export HHOME=~
fi
## cd aliases for wsl-mac-nix consistency
alias cdh="cd ${HOME}"
alias cdd="cd ${HOME}/Documents"
alias cdx="cd ${HOME}/.xi"
alias cdc="cd ${HHOME}/dotfiles"
alias cdhh="cd ${HHOME}"
## Python virtual environment; built-in 'venv' instals old bundled 'pip'.
alias vec="python3 -m virtualenv .venv"
alias vea=". .venv/bin/activate"
alias ved="deactivate"
alias ver="rm -rf .venv"
alias pon="poetry init --no-interaction"
alias poi="poetry install"
alias poa="poetry add"
alias por="poetry run"
alias pop="poetry run python3"
alias pom="poetry run python3 manage.py"
## Rails virtual environment
alias buc="bundle init"
alias bui="bundle install"
alias bua="bundle add"
alias buad="bundle add --group development test"
alias bue="bundle exec"
alias bur="bue ruby"
alias buk="bue rake"
alias bus="bue rails"
busg() {
bus generate $@ --no-helper --no-test-framework --no-template-engine
}
alias busgc="busg controller"
alias busgm="busg model"
# Python virtual environment
alias uvr="uv run"
alias uvp="uv run python"
alias uvc="uv init"
alias uvi="uv sync"
alias uva="uv add"
alias uvs="uvr manage.py runserver"
# Hammerspoon
alias hsr="hs -c 'hs.reload()'"
buss() {
if [ -e ./bin/dev ]; then
./bin/dev
else
bundle exec rails server
fi
}
## docker aliases
dmg() {
if [ -z "$1" ]; then
docker-machine env -u >~/tmp/docker-machine-env
else
docker-machine env $1 >~/tmp/docker-machine-env
fi
eval $(cat ~/tmp/docker-machine-env)
}
di() {
echo -n '{{.Name}}' > ~/tmp/di-format
echo -n ' on {{.OperatingSystem}}' >> ~/tmp/di-format
echo -n ' running docker {{.ServerVersion}}' >> ~/tmp/di-format
docker info -f "$(cat ~/tmp/di-format)"
}
alias dsl='docker service ls'
alias dsi='docker service inspect --pretty'
# Always run tox in quiet mode, it spams a lot of useless info by default.
alias tox='tox -q'
# For windows consistensy
alias rmf='rm -rf'
my_list() {
if [ -e /opt/homebrew/bin/lsd ]; then
/opt/homebrew/bin/lsd "$@"
elif [ -e /usr/local/bin/exa ]; then
/usr/local/bin/exa \
-l \
-a \
--group-directories-first \
--ignore-glob .DS_Store\|desktop.ini\|PowerShell\|My\ Games \
"$@"
else
##! No spaces, '*' should be used instead.
IGNORED=( \
"WindowsPowerShell" \
"PowerShell" \
"desktop.ini" \
".vscode" \
)
CMD_IGNORE=""
IGNORED_COUNT=0
## OSX does not support advanced keys
if [ "$(uname)" != "Darwin" ]; then
for CUR in "${IGNORED[@]}"; do
##! No quotes to match things like "Diablo*"
if [ -e ${CUR} ]; then
((IGNORED_COUNT++))
fi
CMD_IGNORE="${CMD_IGNORE} -I ${CUR}"
done
LS_ARG_COLOR="--color"
LS_ARG_GROUP="--group-directories-first"
fi
## Due to LESS options this will pass-through on one screen.
## -l: One column by default, as in powershell.
#3 -h: Human-readable sizes.
## -A: Show all files except special '.' and '..'.
## -F: Append indicators.
## --color: Color output for linux (CLICOLOR for OSX).
## --group-directories-first: Show dirs first.
## -I: Ignore files.
## "$@": quote args so "ll Visual\ Studio\ 2015" will work.
ls \
-l \
-h \
-A \
-F \
${LS_ARG_COLOR} \
${LS_ARG_GROUP} \
${CMD_IGNORE} \
"$@" \
| less
if ((IGNORED_COUNT > 0)); then
echo -e "${R}${IGNORED_COUNT} items ignored${N}"
fi
fi
}
alias ll=my_list
alias ff=ag
_prompt_command() {
PS_EXIT="$?"
export PS1="${PS_N}${PS_W}${PS_WORKDIR} ${PS_N}"
if [ -n "$PS_GIT_ON" ]; then
if [ -d ~/.git-radar ] || which git-radar >/dev/null; then
#! Spaces before optional changes and before next prompt part.
export GIT_RADAR_FORMAT="git:%{branch}%{local}%{ :changes} "
export PS1="${PS1}${PS_G}${RADAR_CMD}${PS_N}"
fi
fi
if [ -n "${DOCKER_MACHINE_NAME}" ]; then
export PS1="${PS1}${PS_M}{${DOCKER_MACHINE_NAME}} ${PS_N}"
fi
if [ -n "${VIRTUAL_ENV}" ]; then
export PS1="${PS1}🐍 "
fi
export PS1="${PS1}(${PS_C}${PS_EXIT}${PS_N}) "
export PS1="${PS1}${PS_Y}${PS_DOLLAR} ${PS_N}"
}
export PROMPT_COMMAND=_prompt_command
# ZSH alternative to "PROMPT_COMMAND"
precmd() {
eval "$PROMPT_COMMAND"
}
psgiton() {
export PS_GIT_ON=1
psupdate
}
psgitoff() {
export PS_GIT_ON=
psupdate
}
freqdown() {
sudo cpupower frequency-set --min 2.4Ghz --max 2.4ghz
}
## Simple HTTP erver in current dir.
srv() {
if [ _"$1" != _"" ] && [ _"$1" != _"-s" ]; then
if echo $@ | grep -- -s > /dev/null; then
(cd $1 && php -S 0.0.0.0:80 >/dev/null 2>/dev/null)
else
(cd $1 && php -S 0.0.0.0:80)
fi
else
if echo $@ | grep -- -s > /dev/null; then
php -S 0.0.0.0:80 >/dev/null 2>/dev/null
else
php -S 0.0.0.0:80
fi
fi
}
pp() {
ping -i 0.2 1.1.1.1
}
mcd() {
mkdir "$1"
cd "$1"
}
## For tools installed via "go get" to be on path
if which go > /dev/null; then
start_path_with $(go env GOPATH)/bin
fi
## Load eye, if installed
if [ -d $HOME/.rye ]; then
source "$HOME/.rye/env"
fi
## Load rbenv, if installed
if [ -d $HOME/.rbenv ]; then
start_path_with "$HOME/.rbenv/bin"
start_path_with "$HOME/.rbenv/shims"
fi
## Load nodenv, if installed
if [ -d $HOME/.nodenv ]; then
start_path_with "$HOME/.nodenv/bin"
start_path_with "$HOME/.nodenv/shims"
eval "$(nodenv init -)"
fi
## Load phpenv, if installed
if [ -d $HOME/.phpenv ]; then
start_path_with "$HOME/.phpenv/bin"
start_path_with "$HOME/.phpenv/shims"
fi
## Load swiftenv, if installed
if [ -d $HOME/.swiftenv ]; then
start_path_with "$HOME/.swiftenv/bin"
fi
## Load opam, if installed
if [ -d $HOME/.opam ]; then
eval "$(opam env)"
fi