-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path.bashrc
371 lines (317 loc) · 9.56 KB
/
.bashrc
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
# Check for an interactive session
[[ -z "$PS1" ]] && return
BASH_MAJOR_VERSION=${BASH_VERSINFO[0]}
# create an archive from a directory
mktar() { tar cvf "${1%%/}.tar" "${1%%/}/"; }
mktgz() { tar cvzf "${1%%/}.tar.gz" "${1%%/}/"; }
mktbz() { tar cvjf "${1%%/}.tar.bz2" "${1%%/}/"; }
[[ -f ~/.p ]] && . .p
# ex - extract an archive
# usage: ex <file>
ex() {
if [[ -f "$1" ]]; then
case "$1" in
*.tar.bz2) tar xjvf "$1" ;;
*.tar.gz) tar xzvf "$1" ;;
*.tar.xz) tar xJvf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.rar) unrar x "$1" ;;
*.gz) gunzip "$1" ;;
*.tar) tar xvf "$1" ;;
*.tbz2) tar xjvf "$1" ;;
*.tgz) tar xzvf "$1" ;;
*.zip) unzip "$1" ;;
*.par) unzip "$1" ;;
*.jar) unzip "$1" ;;
*.Z) uncompress "$1" ;;
*.7z) 7z x "$1" ;;
*) echo -e "'$1' can't be uncompressed with ex()" ;;
esac
else
echo -e "\n'$1' is an invalid file"
fi
}
list() {
if [[ -f "$1" ]]; then
case "$1" in
*.tar.bz2) tar tjf "$1" ;;
*.tar.gz) tar tzf "$1" ;;
*.tar.xz) tar tJf "$1" ;;
*.tar) tar tf "$1" ;;
*.tbz2) tar tjf "$1" ;;
*.tgz) tar tzf "$1" ;;
*.zip) unzip -l "$1" ;;
*.par) unzip -l "$1" ;;
*.jar) jar tf "$1" ;;
*) echo -e "'$1' can't be listed with list()" ;;
esac
else
echo -e "\n'$1' is an invalid file"
fi
}
# takes a bunch of text and quote it like in markdown
cite() {
if [[ -t 0 ]]
then
pbpaste | sed 's/^/> /'
else
sed 's/^/> /'
fi
}
origin() {
tr / . | sed 's/.java$//'
}
throttling-proxy() {
ssh -D 1080 -N -o ProxyCommand="
pv -B8K -trbcN upload -L${2:-100K} |
/usr/bin/nc %h %p |
pv -B8K -trbcN download -L${1:-100K}" localhost
}
# Less Colors for Man Pages
export LESS_TERMCAP_mb=$'\E[01;31m' # begin blinking
export LESS_TERMCAP_md=$'\E[01;38;5;74m' # begin bold
export LESS_TERMCAP_me=$'\E[0m' # end mode
export LESS_TERMCAP_se=$'\E[0m' # end standout-mode
export LESS_TERMCAP_so=$'\E[1;31m' # begin standout-mode - info box
export LESS_TERMCAP_ue=$'\E[0m' # end underline
export LESS_TERMCAP_us=$'\E[04;38;5;146m' # begin underline
root() {
cd "$(git rev-parse --show-toplevel)"
}
gw() {
(cd "$(git rev-parse --show-toplevel)" && "$(git rev-parse --show-toplevel)"/gradlew --daemon --no-scan "$@")
}
# http://stackoverflow.com/a/16178979/2813687
color() (set -o pipefail; "$@" 2>&1>&3 | sed $'s,.*,\e[31m&\e[m,' >&2)3>&1
idea() {
open -b com.jetbrains.intellij.ce .
}
goland() {
open -b com.jetbrains.goland .
}
tmp() {
if [[ $# == 1 ]]
then
cd "$(mktemp -td "$1-XXX")"
else
cd "$(mktemp -d)"
fi
}
pb() {
pbpaste | vim -
}
clean() {
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker volume rm $(docker volume ls -qf dangling=true)
docker network rm $(docker network ls -q)
}
_xargsh () {
local f=$1
export -f "$f"
shift
xargs "$@" bash -c "$f \"\$@\"" _
}
alias ....='cd ../../..'
alias ...='cd ../..'
alias ..='cd ..'
alias .='PS1= builtin .'
alias 720p='yt-dlp -f "bv[height<=720]+ba"'
alias csv="awk -vFPAT='([^,]+)|(\"[^\"]+\")'"
alias gg='./godelw'
alias grep='grep -E --color=auto -I'
alias fgrep='fgrep --color=auto -I'
alias ipy='ipython --colors=linux --no-confirm-exit'
alias k=kubectl && complete -o nospace -F __start_kubectl k
# Causes less to automatically exit if the entire file can be displayed on the
# first screen. + display colors
alias less='less -F -X -R'
alias ls='ls --color=auto'
alias pwgen='pwgen -s 32 1'
alias py=python
alias reload='history -n'
alias tree='tree -C'
alias vi=vim
alias vimcognito='vim -u NONE "+set noswapfile nobackup nowritebackup viminfo="'
# tz are hard!
alias denver='TZ=America/Denver date'
alias fr='TZ=Europe/Paris date'
alias jp='TZ=Asia/Tokyo date'
alias ny='TZ=America/New_York date'
alias nyc='TZ=America/New_York date'
alias sf='TZ=US/Pacific date'
alias sg='TZ=Asia/Singapore date'
alias uk='TZ=Europe/London date'
alias utc='TZ=UTC date'
shopt -s checkwinsize
shopt -s cdspell
(( BASH_MAJOR_VERSION > 3 )) && shopt -s globstar
shopt -s histappend
export EDITOR=$HOME/.editor.sh
export GRADLE_OPTS=-Xmx2g
export HISTCONTROL=ignorespace
export HISTTIMEFORMAT='%F %T - '
export HISTFILESIZE=100000
export HISTSIZE=100000
export HOMEBREW_NO_ANALYTICS=1
if [[ -x /opt/homebrew/opt/openjdk/bin/java ]]
then
export JAVA_HOME=/opt/homebrew/opt/openjdk/
export PATH="/opt/homebrew/opt/openjdk/bin/:$PATH"
elif [[ -x /usr/libexec/java_home ]]
then
export JAVA_HOME=$(/usr/libexec/java_home -v '17')
fi
# https://stackoverflow.com/a/42265848/2813687
export GPG_TTY=$(tty)
# avoid anonymous encryption like so:
# gpg: encrypted with RSA key, ID 0x00000000
# gpg: anonymous recipient; trying secret key 0xF82937A7 ...
# gpg: okay, we are the anonymous recipient.
export PASSWORD_STORE_GPG_OPTS='--no-throw-keyids'
# use gnu coreutils on Mac (and use the right man pages)
export MANPATH="/opt/homebrew/opt/coreutils/libexec/gnuman:$MANPATH"
export MANPATH="/opt/homebrew/opt/gnu-sed/libexec/gnuman:$MANPATH"
export MANPATH="/opt/homebrew/opt/grep/libexec/gnuman/:$MANPATH"
export MANPATH="/opt/homebrew/opt/findutils/libexec/gnuman/:$MANPATH"
# pipx
export PATH="$HOME/.local/bin:$PATH"
export PATH="$HOME/bin:$PATH"
export PATH="/sbin:$PATH"
export PATH="/usr/sbin:$PATH"
export PATH="/Applications/Docker.app/Contents/Resources/bin:$PATH"
export PATH="/opt/homebrew/opt/findutils/libexec/gnubin:$PATH"
export PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:$PATH"
export PATH="/opt/homebrew/opt/gnu-sed/libexec/gnubin:$PATH"
export PATH="/opt/homebrew/opt/grep/libexec/gnubin/:$PATH"
export PATH="/opt/homebrew/bin:$PATH"
if [[ "$PROMPT_COMMAND" != *"history -a;"* ]]
then
export PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
fi
export PYTHONSTARTUP=~/.pythonrc.py
export XDG_CONFIG_HOME=~/.dotfiles/.config
if ((BASH_MAJOR_VERSION > 3)) && [[ -f /etc/bash_completion ]]; then
. /etc/bash_completion
fi
if hash brew 2>/dev/null; then
# for MacOS
BREW_PREFIX=$(brew --prefix)
BASH_COMPLETION="$BREW_PREFIX/share/bash-completion/bash_completion"
if ((BASH_MAJOR_VERSION > 3)) && [[ -f "$BASH_COMPLETION" ]]; then
. "$BASH_COMPLETION"
fi
Z="$BREW_PREFIX/etc/profile.d/z.sh"
[[ -f "$Z" ]] && . "$Z"
fi
if hash kubectl 2>/dev/null; then
source <(kubectl completion bash)
fi
if hash fzf 2>/dev/null; then
eval "$(fzf --bash)"
fi
if command -v z &>/dev/null
then
Z="$HOME/bin/z.sh"
[[ -f "$Z" ]] && . "$Z"
fi
# for __git_ps1, this actually loads fast
[[ -f /usr/local/etc/bash_completion.d/git-prompt.sh ]] &&
. /usr/local/etc/bash_completion.d/git-prompt.sh
[[ -f /usr/share/git/git-prompt.sh ]] &&
. /usr/share/git/git-prompt.sh
[[ -f ~/.cargo/env ]] && . ~/.cargo/env
# http://superuser.com/a/418112
stty stop '' # disable ^S
too-long() {
local pfad=${PWD/#$HOME/\~}
if [[ ${#pfad} -lt 30 ]]; then
echo -n "${pfad}"
else
echo -n ".../${pfad##*/}"
fi
}
find-up () {
path=$(pwd)
while [[ "$path" != "" && ! -e "$path/$1" ]]; do
path=${path%/*}
done
[[ -n "$path" ]]
}
git-branch() {
# used instead of git rev-parse --show-toplevel as it's faster (noticeable
# when running in an x86 QEMU VM running on ARM)
if ! find-up .git; then
return
fi
# VM
if [[ "$HOSTNAME" == arch ]]; then
echo -n ' (???)'
return
fi
local plusminus=+-
((BASH_MAJOR_VERSION > 3)) && plusminus=$'\u00b1'
local dirty=$(
[[ -n "$(git status --porcelain 2>/dev/null | head -c 1)" ]] &&
echo -n " $plusminus")
__git_ps1 " (%s$dirty)" 2>/dev/null
}
export PYENV_VIRTUALENV_DISABLE_PROMPT=1
mark=+
((BASH_MAJOR_VERSION > 3)) && mark=$'\u2713'
cross=-
((BASH_MAJOR_VERSION > 3)) && cross=$'\u2717'
check="\[\033[01;37m\]\$(if [[ \$? == 0 ]]; then echo \"\[\033[01;32m\]\"$mark;
else echo \"\[\033[01;31m\]\"$cross; fi)\[\e[0m\]"
if [[ -n "$SSH_CLIENT" ]]
then
ssh="\[\e[1;41m\]\[\e[1;97m\]SSH\[\e[0m\] "
fi
time="\\A"
user="\[\e[1;37m\]\u\[\e[0m\]"
host="\[\e[1;34m\]\h\[\e[0m\]"
dir="\[\e[1;32m\]\$(too-long)\[\e[0m\]"
branch="\[\e[1;36m\]\$(git-branch)\[\e[0m\]"
root="\\$"
# TODO(alexis): refactor PS1 logic at some point.
# e.g. ✓ 16:33 alexis @ alexis in ~/.dotfiles (master ±) $
PS1="$check $ssh$time $user @ $host in $dir$branch $root "
stitle() {
echo -ne "\033]1;${1:-${HOSTNAME%%.*}}\033\\"
}
_venvname() {
if [[ -n "$1" ]]
then
echo -n "$1"
return
fi
local mayberepo=$(git rev-parse --show-toplevel 2>/dev/null)
if [[ -n "$mayberepo" ]]
then
basename "$mayberepo"
return
fi
basename "$PWD"
}
venv() {
[[ ! -d ~/.venvs ]] && mkdir ~/.venvs
local venv=$(_venvname "$1")
[[ ! -d ~/.venvs/"$venv" ]] && python3 -m venv ~/.venvs/"$venv"
workon "$venv"
}
venvs() {
ls --format=single-column ~/.venvs
}
workon() {
. ~/.venvs/"$(_venvname "$1")"/bin/activate
}
if [[ -f ~/.rye/env ]]
then
. ~/.rye/env
eval "$(rye self completion)"
fi
# only for ssh/non-iTerm
[[ "$TERM_PROGRAM" != "iTerm.app" ]] && PROMPT_COMMAND="stitle; $PROMPT_COMMAND"
((BASH_MAJOR_VERSION < 4)) && (
echo -n $'\nBash < 4.x; some features '
echo $'(e.g. advanced autocompletion) won\'t be available.\n') || true