-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.bashrc.my
522 lines (414 loc) · 11.3 KB
/
.bashrc.my
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# vim: set filetype=sh
eval "$(/opt/homebrew/bin/brew shellenv)"
# Homebrew
BREW_PREFIX=$(/opt/homebrew/bin/brew --prefix)
export PATH="$BREW_PREFIX/opt/coreutils/libexec/gnubin:$PATH"
export PATH="$BREW_PREFIX/opt/findutils/libexec/gnubin:$PATH"
export PATH="$BREW_PREFIX/opt/gnu-sed/libexec/gnubin:$PATH"
export PATH="$BREW_PREFIX/opt/grep/libexec/gnubin:$PATH"
export PATH="$BREW_PREFIX/opt/make/libexec/gnubin:$PATH"
export PATH="$BREW_PREFIX/opt/python/libexec/bin:$PATH"
export PATH="./node_modules/.bin:$PATH"
export PATH="$PATH:/Applications/Docker.app/Contents/Resources/cli-plugins"
export MANPATH="$BREW_PREFIX/opt/coreutils/libexec/gnuman:$MANPATH"
[ "$OSTYPE" = "linux-gnueabi" ] && export PS1=" \[\033[0;33m\]\w\[\033[0;37m\] \[\033[0;32m\]ε\[\033[0;37m\] "
[ "$OSTYPE" = "linux-gnu" ] && export PS1=" \[\033[0;33m\]\w\[\033[0;37m\] \[\033[0;32m\]Ξ\[\033[0;37m\] "
[[ $OSTYPE =~ ^darwin ]] && export PS1=" \[\033[0;33m\]\w\[\033[0;37m\] \[\033[0;35m\]ɀ\[\033[0;37m\] "
[ "`whoami`" = "vgurdiga" ] && export PS1=" \[\033[0;33m\]\w\[\033[0;37m\] \[\033[1;33m\]ω\[\033[0;37m\] "
echo -n -e "\033]0;`whoami`@`hostname -f`\007"
# Cause the status of terminated background jobs to be reported immediately,
# rather than before printing the next primary prompt.
set -b
shopt -s globstar
shopt -s extglob
# keeps newlines in multi-line commands
shopt -s cmdhist
shopt -s lithist
# If set, the history list is appended to the file named by the value of the
# HISTFILE variable when the shell exits, rather than overwriting the file.
shopt -s histappend
export HISTSIZE=100000
export HISTCONTROL=ignoredups
export HISTTIMEFORMAT='%Y-%m-%d %T '
export MAILCHECK=
export LC_CTYPE=en_US.UTF-8
export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced
export EDITOR=vim
export MANWIDTH=80
# per-directory Bash history
function check_for_local_history {
function main {
if found_local_history_file; then
if [ ! "$PWD" == "$HOME" ]; then
echo "Using local Bash history"
fi
use_history_file "$PWD/.bash_history"
else
use_history_file ~/.bash_history
fi
}
function found_local_history_file {
[ -e .bash_history ]
}
function use_history_file {
history -w
history -c
export HISTFILE="$1"
history -r
}
main
}
function changing_directory {
function main {
local last_command=`history 1`
local last_command=${last_command:27} # this depends on HISTTIMEFORMAT
is_cd_nothing || is_cd_something || is_a_cd_alias
}
function is_cd_nothing {
[ ${#last_command} -eq 2 -a "${last_command:0:2}" = "cd" ]
}
function is_cd_something {
[ ${#last_command} -gt 2 -a "${last_command:0:3}" = "cd " ]
}
function is_a_cd_alias {
local cd_aliases=`alias | grep "='cd " | grep -E -o 'alias [a-z]+='`
echo $cd_aliases | grep -F -q "alias $last_command="
}
main
}
function check_for_nvmrc {
local nvm_node_path="$HOME/.nvm/versions/node"
function main {
if [ -e .nvmrc ]; then
local expected_node_version=`get_version_from_nvmrc`
local expected_node_path="$nvm_node_path/$expected_node_version/bin"
local current_node_version=`get_current_node_version`
if [[ -n $current_node_version && $current_node_version =~ $expected_node_version ]]; then
return
fi
if [ -d $expected_node_path ]; then
# Remove previous nvm_node_path from PATH if found
export PATH=$expected_node_path:`remove_path_item_matching $nvm_node_path`
echo "Changed Node version to $(node -v)"
else
export NVM_DIR="$HOME/.nvm"
source "$NVM_DIR/nvm.sh" # This loads nvm
nvm use
fi
fi
}
function get_current_node_version {
if [[ "$(type -t nvm)" == "function" ]]; then
nvm current
return
fi
if [[ "$(type -t node)" == "file" ]]; then
node --version
return
fi
}
function get_version_from_nvmrc {
local version=$(<.nvmrc)
version=${version/v/}
echo v$version
}
function remove_path_item_matching {
local path_item=$1
if grep -F "$path_item" <<<$PATH ; then
escaped_path_item=${path_item//\//\\/} # escape slashes for the awk regexp
awk -v RS=: -v ORS=: "/$escaped_path_item/ {next} {print}" <<<$PATH
else
echo $PATH
fi
}
main
}
function check_for_ruby_version {
if [ -e .ruby-version ]; then
source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
rvm use
fi
}
function check_for_dot_env {
# Unload prev .env if any
if [ ! -z "$OLDPWD" ] && [ -f "$OLDPWD/.env" ]; then
unset `grep -Po '^(export )?\K\w+' "$OLDPWD/.env"`
fi
if [ -e .env ]; then
set -o allexport
source .env
set +o allexport
echo "Loaded .env"
fi
}
function do_my_checks {
if ! changing_directory; then
return
fi
check_for_local_history
check_for_nvmrc
check_for_ruby_version
check_for_dot_env
set_macos_terminal_tab_title
}
function set_macos_terminal_tab_title {
local home_relative_path=$(realpath --relative-to="$HOME" "$PWD")
echo -n -e "\033]0;~/${home_relative_path}\007"
}
function add_prompt_command {
function main {
if ! already_set; then
if [ -z "$PROMPT_COMMAND" ]; then
PROMPT_COMMAND="do_my_checks"
else
PROMPT_COMMAND="${PROMPT_COMMAND}; do_my_checks"
fi
fi
}
function already_set {
grep -q check_for_local_history <<<$PROMPT_COMMAND
}
main
}
add_prompt_command
export RUBY_GC_MALLOC_LIMIT=90000000
export RUBY_GC_HEAP_FREE_SLOTS=200000
if [ `arch` = 'i386' ]; then
echo "Rosetta"
BREW_PREFIX=$(/usr/local/homebrew/bin/brew --prefix)
fi
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
function pull() {
if [ ! -d .git ]; then
echo "Not a Git repo."
return 1
fi
if [ $# -eq 0 ]; then
if [ "`git status --short --branch | head -1 | grep -o '(no branch)'`" = "(no branch)" ]; then
echo "Not on a branch."
return 1
fi
# If there is a tracked branch only pull that one
git status --short --branch |\
head -1 |\
grep -oP '\.{3}\K\S+' |\
sed 's|/| |' |\
xargs git pull
else
git pull "$@"
fi
}
function q() {
local wildcard="$1"
shift
local dirs="$@"
local IGNORE_DIRS=".git"
local DOT_FILE=".qignore"
local EXCLUDE_DIR_CLAUSE=""
if [ -f $DOT_FILE ]; then
IGNORE_DIRS+=" "`cat "$DOT_FILE" | paste -sd " "`
fi
local IGNORE_DIRS_RE=${IGNORE_DIRS// /\|}
for DIR in $IGNORE_DIRS; do
EXCLUDE_DIR_CLAUSE+=" -path ./$DIR -prune -o"
done
find ${dirs:-.} $EXCLUDE_DIR_CLAUSE -name "$wildcard" \
| grep -Pv "^./($IGNORE_DIRS_RE)$"
}
function aac2mp3() {
aac_file=$1
mp3_file=$(basename "$aac_file" .aac).mp3
wav_file=/tmp/aac2mp3.wav
ffmpeg -i "$aac_file" "$wav_file" && \
ffmpeg -i "$wav_file" -acodec libmp3lame "$mp3_file" && \
rm "$wav_file"
}
function f() {
if [ "$#" -eq 0 ]; then
echo 'Find what?'
return 0
fi
local DOT_FILE=".grepignore"
local MAX_LINE_LENGTH=$(($COLUMNS * 3))
local IGNORE_DIRS=".git .svn node_modules bower_components" # GLOB patterns
local IGNORE_FILES=".bash_history .viminfo"
local EXCLUDE_DIR_CLAUSE=""
local EXCLUDE_CLAUSE=""
if [ -f $DOT_FILE ]; then
IGNORE_DIRS+=" $(echo $(grep '/$' $DOT_FILE | sed 's|/$||'))"
IGNORE_FILES+=" $(echo $(grep -v '/$' $DOT_FILE))"
fi
for DIR in $IGNORE_DIRS; do
EXCLUDE_DIR_CLAUSE+=" --exclude-dir=$DIR"
done
for FILE in $IGNORE_FILES; do
EXCLUDE_CLAUSE+=" --exclude=$FILE"
done
local GREP="grep"
if [ -v DEBUG ]; then
GREP="echo grep"
MAX_LINE_LENGTH=$(($COLUMNS * 30))
fi
$GREP \
--color=${GREP_COLOR:-always} \
--line-number \
--binary-files=without-match \
--recursive \
$EXCLUDE_CLAUSE \
$EXCLUDE_DIR_CLAUSE "$@" \
| cut -c -$MAX_LINE_LENGTH
}
function gif() {
if [ "$#" -eq 0 ]; then
echo 'Gif what?'
return 0
fi
local input="$1"
local filename="${input%.*}"
ffmpeg -i "$input" -pix_fmt rgb24 -r 10 -f gif - | gifsicle --loopcount=forever --optimize=3 --delay=5 > "$filename.gif"
}
function inith() {
if [ ! -e .bash_history ]; then
touch .bash_history
else
echo ".bash_history already exists"
fi
}
function initv() {
local viminfo_config="set viminfo+=n.viminfo"
if [ ! -e .vimrc ]; then
echo "$viminfo_config" > .vimrc
else
if grep -F "set viminfo" .vimrc; then
echo ".viminfo already configured"
else
echo "$viminfo_config" >> .vimrc
echo ".viminfo configured"
fi
fi
}
function svgoo() {
if [ "$#" -eq 0 ]; then
echo 'Gimme an SVG file to optimize, please!'
return 1
fi
if ! which -s svgo; then
echo 'I’m not seeing your svgo executable...🤔'
return 2
fi
local file="$1"
svgo --input="$file" --pretty
}
function is_interactive_shell() {
# https://www.gnu.org/software/bash/manual/html_node/Is-this-Shell-Interactive_003f.html
[[ "$-" =~ "i" ]]
}
if is_interactive_shell; then
# fzf git branch name; use like this: git checkout ^g^b
bind '"\C-g\C-b": "$(git branch -a | cut -c 3- | fzf)\e\C-e"'
source ~/.fzf.bash
fi
#fzf
export FZF_CTRL_T_COMMAND="fd --type f"
# yes, or maybe not?
# source /usr/local/etc/bash_completion
# Autocompletion of Make targets BEGIN
function _makefile_targets {
local curr_arg;
local targets;
# Find makefile targets available in the current directory
targets=''
if [[ -e "Makefile" ]]; then
targets=$( \
grep -oE '^[a-zA-Z0-9._-]+:' Makefile \
| sed 's/://' \
| tr '\n' ' ' \
)
fi
# Filter targets based on user input to the bash completion
curr_arg=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -W "${targets[@]}" -- $curr_arg ) );
}
complete -F _makefile_targets make
complete -F _makefile_targets m
# Autocompletion of Make targets END
function hibp() {
local password_hash=`read -s -p "Password: " p && echo -n $p | openssl sha1`
local head=${password_hash:0:5}
local tail=${password_hash:5}
local tempfile=`mktemp`
if curl https://api.pwnedpasswords.com/range/$head --output $tempfile; then
if grep --ignore-case $tail $tempfile; then
echo YEP;
else
echo NOPE
fi
fi
}
function log {
local directory=~/Google\ Drive/log
local filename="`LC_ALL=ro_RO.utf-8 date +%F-%H-%M-%a`.txt"
local variation="$1"
local template="template.txt"
if [ $variation ]; then
template="template-$variation.txt"
fi
cp "$directory/$template" "$directory/$filename"
cd "$directory"
code --new-window . "$filename" && exit
}
function skip {
tail --lines +`expr $1 + 1`
}
function vimp {
file=$1
line_number=`expr "$file" : '.*:\([0-9]\+\)'`
if [[ "$file" =~ :[0-9]*.*$ ]]; then
file=${file/$BASH_REMATCH/}
line_number=$BASH_REMATCH
line_number=${line_number/:/}
line_number=${line_number/[^0-9]*/}
if [[ "$line_number" -ne "" ]]; then
line_number="+$line_number"
fi
vim "$file" $line_number
else
vim "$@"
fi
}
function source_location {
shopt -s extdebug
declare -F $1
shopt -u extdebug
}
function gfc {
local remote_branch_name="$1"
: "${remote_branch_name:?Gimme a remote branch name}"
git fetch origin $remote_branch_name
git checkout $remote_branch_name
git reset --hard origin/$remote_branch_name
}
function s {
local files="$@"
stat -c "%s %n" $files | numfmt --to=iec-i --suffix=B --padding=7
}
function second {
awk '{ print $2; }'
}
# Given a list of files to STDIN, will filter out the ones that contain the grep needle.
function does_not_contain {
local needle=$1
while read file; do
grep "$needle" "$file" > /dev/null || echo "$file"
done
}
export -f does_not_contain
function seconds { date -u +%H:%M:%S -d @$1; }
function wfx { # WorkFlowy export
pbpaste > ~/tmp/wfx.opml
cd ~/Desktop
local filename=`xq -r '.opml.body.outline["@text"] + ".opml"' ~/tmp/wfx.opml`
mv ~/tmp/wfx.opml "$filename"
}