-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaliases
executable file
·652 lines (540 loc) · 19.6 KB
/
aliases
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
# Common aliases and functions
__aliases_main() {
local SH_SOURCE_FILE SH_SOURCE_DIR SH_SOURCE_FILE_ESCAPED
if [ -n "$ZSH_VERSION" ]; then
SH_SOURCE_FILE=${(%):-%x}
elif [ -n "$BASH_VERSION" ]; then
SH_SOURCE_FILE=${BASH_SOURCE[0]}
fi
while [[ -L "$SH_SOURCE_FILE" ]]; do
SH_SOURCE_FILE=$(readlink "$SH_SOURCE_FILE")
done
SH_SOURCE_DIR=$(dirname "$SH_SOURCE_FILE")
SH_SOURCE_DIR=`cd "$SH_SOURCE_DIR" >/dev/null; pwd`
SH_SOURCE_FILE=$(basename "$SH_SOURCE_FILE")
SH_SOURCE_FILE_ESCAPED=${SH_SOURCE_FILE// /_}
# SH_SOURCE_DIR is a full path to the location of this script
eval "$(cat <<EOF
__get_${SH_SOURCE_FILE_ESCAPED}_dir() {
echo $SH_SOURCE_DIR
}
__get_${SH_SOURCE_FILE_ESCAPED}_file() {
echo $SH_SOURCE_FILE
}
EOF
)"
eval "$(__sh_color_definitions)"
eval "$(__sh_os_definitions)"
# Early load before bashrc main started
alias cls='clear'
alias grep='grep --color=auto '
alias fgrep='fgrep --color=auto '
alias egrep='egrep --color=auto '
alias dir='ls -al '
alias path='echo $PATH | sed s/\:/\\n/g '
alias ps='ps -ef '
alias psu='ps -u ${USER} '
alias avd='android avd & '
# Replace the cd builtin to turn off the annoying
# echo-new-folder behavior when CDPATH is set
# and to automatically use the correct node version
cd() {
if [[ $# -eq 0 ]]; then
builtin cd ~ >/dev/null
else
builtin cd "$@" >/dev/null
fi
[[ -f .nvmrc && -r .nvmrc ]] && nvm use || true
}
ff() {
find . -name "*" -type f -exec grep --color=auto -HinF "$@" {} \;
}
ff2() {
# Double-grep to filter out grep warnings
grep --color=auto -RHinF "$@" * 2>&1 | grep --color=auto -i "$@"
}
fff() {
find . -name "$1" -type f -exec grep --color=auto -HinF "${@:2}" {} \;
}
fff2() {
# Double-grep to filter out grep warnings
grep --color=auto -RHinF "${@:2}" "$1" 2>&1 | grep --color=auto -i "${@:2}"
}
ffs() {
set | grep --color=auto -i "$@"
}
fn() {
find -L . -iname "$@"
}
fp() {
ps | grep --color=auto -i "$@"
}
rmr() {
eval "$(__sh_color_definitions)"
if [[ $# -eq 0 ]]; then
echo -e "Usage: "$COLOR_YELLOW_BOLD"rmr"$COLOR_NONE" "$COLOR_CYAN_BOLD"{pattern}"$COLOR_NONE" "$COLOR_CYAN_BOLD"{rm options}"$COLOR_NONE
echo
echo -e $COLOR_YELLOW_BOLD"Rmr"$COLOR_NONE" will execute recursive rm on any files matching the "$COLOR_CYAN_BOLD"pattern"$COLOR_NONE
echo -e "skipping matching files in hidden subdirectories."
echo
echo -e $COLOR_RED_BOLD"Warning:"$COLOR_NONE" rmr is dangerous!"
echo
else
find . -name "$1" ! -path '*/.*' -exec rm "${@:2}" {} \;
fi
}
rmr2() {
eval "$(__sh_color_definitions)"
if [[ $# -eq 0 ]]; then
echo -e "Usage: "$COLOR_YELLOW_BOLD"rmr"$COLOR_NONE" "$COLOR_CYAN_BOLD"{pattern}"$COLOR_NONE" "$COLOR_CYAN_BOLD"{rm options}"$COLOR_NONE
echo
echo -e $COLOR_YELLOW_BOLD"Rmr"$COLOR_NONE" will execute recursive rm on any files matching the "$COLOR_CYAN_BOLD"pattern"$COLOR_NONE"."
echo
echo -e $COLOR_RED_BOLD"Warning:"$COLOR_NONE" rmr is dangerous!"
echo
else
find . -name "$1" -exec rm "${@:2}" {} \;
fi
}
check_whitespace() {
eval "$(__sh_color_definitions)"
eval "$(__sh_os_definitions)"
echo
echo -e 'Looking for '$COLOR_CYAN_BOLD'trailing space'$COLOR_NONE
# Look for trailing spaces
grep -r -n '[[:space:]]$' *
echo
echo -e 'Looking for '$COLOR_CYAN_BOLD'leading tabs'$COLOR_NONE
# Look for leading tabs
grep -r -n '^\t' *
echo
}
get_ssh_client_ip() {
local SSH_ARGS=($SSH_CLIENT)
echo ${SSH_ARGS[0]}
}
groot() {
local GIT_ROOT=$(git rev-parse --show-toplevel)
cd $GIT_ROOT/"$@"
}
hroot() {
local HG_ROOT=$(hg root)
cd $HG_ROOT/"$@"
}
ssht() {
ssh $* -t 'tmux a || tmux || /usr/bin/env bash'
}
sshz() {
ssh $* -t 'tmux a || tmux || /usr/bin/env zsh'
}
__get_commands_info() {
local COMMAND_LIST COMMAND COMMAND_INSTALLED COMMAND_LOCATION COMMAND_VERSION
COMMAND_LIST=( $* )
for COMMAND in "${COMMAND_LIST[@]}"; do
COMMAND_INSTALLED=`type -t ${COMMAND} 2>/dev/null || whence ${COMMAND} 2>/dev/null`
if [[ -n "$COMMAND_INSTALLED" ]]; then
echo
echo -e $COLOR_GREEN_BOLD${COMMAND}$COLOR_NONE
COMMAND_VERSION=`${COMMAND} --version 2>/dev/null || ${COMMAND} version 2>/dev/null`
if [[ -n "$COMMAND_VERSION" ]]; then
echo -e $COLOR_CYAN_BOLD' Version : '${COMMAND_VERSION}$COLOR_NONE
fi
COMMAND_LOCATION=`type -p ${COMMAND} 2>/dev/null`
if [[ -n "$COMMAND_LOCATION" ]]; then
echo -e $COLOR_CYAN_BOLD' Location : '${COMMAND_LOCATION}$COLOR_NONE
fi
else
echo
echo -e $COLOR_RED_BOLD${COMMAND}$COLOR_NONE
echo -e $COLOR_RED_BOLD' Not found'$COLOR_NONE
fi
done
}
check_env() {
eval "$(__sh_color_definitions)"
eval "$(__sh_os_definitions)"
echo
echo -e 'Looking for '$COLOR_CYAN_BOLD'common'$COLOR_NONE' tools...'
__get_commands_info git gh brew bun nvm node npm yarn python python3 pip pip3 pipx poetry huggingface-cli flutter dart go ruby gem arduino-cli llama-server gcc clang ccache cmake ninja tmux curl convert
if [[ $SH_OS_TYPE == OSX ]]; then
# OS X tools
echo
echo -e 'Looking for '$COLOR_CYAN_BOLD'OS X'$COLOR_NONE' tools...'
__get_commands_info port
fi
echo
}
deps_update() {
if [ ! -f "package.json" ]; then
echo "No package.json found in current directory"
return 1
fi
# Check if npm-check-updates is installed locally
if ! npm list npm-check-updates >/dev/null 2>&1; then
echo "Installing npm-check-updates as dev dependency..."
npm install -D npm-check-updates
echo
fi
echo "Checking for updates..."
# Run ncu and capture its output and exit code
output=$(npx npm-check-updates -u)
exit_code=$?
echo
if [ $exit_code -ne 0 ]; then
echo "Error running npm-check-updates"
return 1
fi
# Check if any packages were updated
if echo "$output" | grep -q "Run npm install"; then
echo "Updates found. Running npm install..."
npm install
echo
# Check for build script
if grep -q "\"build\":" package.json; then
echo "Running build script..."
npm run build
echo
fi
# Check for test script
if grep -q "\"test\":" package.json; then
echo "Running tests..."
npm test
echo
fi
else
echo "All packages are up to date!"
fi
}
# Function to find and activate a Python virtual environment in the current directory
activate() {
eval "$(__sh_color_definitions)"
eval "$(__sh_os_definitions)"
# Check for common venv directory names
if [ -d "venv" ] && [ -f "venv/bin/activate" ]; then
echo -e "Activating virtual environment: "$COLOR_GREEN_BOLD"venv"$COLOR_NONE
source venv/bin/activate
elif [ -d ".venv" ] && [ -f ".venv/bin/activate" ]; then
echo -e "Activating virtual environment: "$COLOR_GREEN_BOLD".venv"$COLOR_NONE
source .venv/bin/activate
elif [ -d "env" ] && [ -f "env/bin/activate" ]; then
echo -e "Activating virtual environment: "$COLOR_GREEN_BOLD"env"$COLOR_NONE
source env/bin/activate
elif [ -d ".env" ] && [ -f ".env/bin/activate" ]; then
echo -e "Activating virtual environment: "$COLOR_GREEN_BOLD".env"$COLOR_NONE
source .env/bin/activate
else
echo -e $COLOR_YELLOW_BOLD"No virtual environment found in the current directory."$COLOR_NONE
fi
}
# OS-specific aliases and functions
if [[ $SH_OS_TYPE == OSX ]]; then
# OS X aliases
alias ls='ls -Gp '
alias www-on='sudo apachectl start'
alias www-off='sudo apachectl stop'
alias atompi='ATOM_HOME=$HOME/.atompi atom'
alias apmpi='ATOM_HOME=$HOME/.atompi apm'
list() {
eval "$(__sh_color_definitions)"
eval "$(__sh_os_definitions)"
echo -e 'Connected devices'
echo
echo -e '--- '$COLOR_CYAN_BOLD'iOS'$COLOR_NONE' ---'
xcrun xctrace list devices 2>&1 | sed -n '/Simulators/q;p' | sed '1d;2d;$d'
echo
echo -e '--- '$COLOR_GREEN_BOLD'Android'$COLOR_NONE' ---'
adb devices | sed '1d;$d'
echo
echo -e '--- '$COLOR_YELLOW_BOLD'Fastboot'$COLOR_NONE' ---'
fastboot devices
echo
}
ij() {
open -b com.jetbrains.intellij.ce "$@"
}
as() {
# open -b com.google.android.studio "$@"
open -a /Applications/Android\ Studio.app "$@"
}
g() {
# Consider hroot if it's a Mercurial repository
groot
./gradlew "$@"
}
set-title() {
eval "$(__sh_color_definitions)"
local TITLE="$@"
echo -e "Setting terminal title to "$COLOR_YELLOW_BOLD"$TITLE"$COLOR_NONE
echo -n -e "\033]0;$TITLE\007"
}
ex() {
if [[ $# -eq 0 ]]; then
open .
else
if [[ -d "$1" ]]; then
open "$@"
else
open -R "$@"
fi
fi
}
rep() {
eval "$(__sh_color_definitions)"
if [[ $# -eq 0 ]]; then
echo -e "Usage: rep {sed command}"
echo
echo -e "Rep will execute the sed command on all files in the current directory"
echo -e "and subdirectories, excluding any hidden files. All commands will be"
echo -e "executed in place using sed's -i option."
echo
echo -e "For example rep s/a/b/ will replace all instances of 'a' in all files with 'b'"
echo
sed --help
echo
else
for x in $(find . -type f -name '*' ! -name '.*' ! -path '*/.*'); do
sed -i "" "$@" $x
done
# This is a bit dangerous, and assumes that there are no legitimate files
# that start with .!. Probably should be a bit more careful and match
# more specifically
find . -name "\.\!*" -exec rm {} \;
fi
}
latest() {
if [[ -z $LATEST_SKIP_BREW ]]; then
if [[ $(which brew 2>/dev/null) ]]; then
brew analytics off
brew update
brew upgrade
brew cleanup
brew prune
fi
fi
if [[ -z $LATEST_SKIP_PORTS ]]; then
if [[ $(which port 2>/dev/null) ]]; then
local PORT_BIN=$(which port)
sudo $PORT_BIN selfupdate
sudo $PORT_BIN upgrade outdated
sudo $PORT_BIN reclaim
fi
fi
if [[ -z $LATEST_SKIP_NPM ]]; then
if [[ $(which npm 2>/dev/null) ]]; then
npm install -g npm@latest
npm update -g
fi
fi
if [[ -z $LATEST_SKIP_YARN ]]; then
if [[ $(which yarn 2>/dev/null) ]]; then
yarn global upgrade --latest --non-interactive
fi
fi
if [[ -z $LATEST_SKIP_PYTHON ]]; then
if [[ $(which pip3 2>/dev/null) ]]; then
# pip3 install --upgrade pip setuptools
# pip3 install --user --upgrade pip setuptools
python3 -m pip install --upgrade pip setuptools
fi
fi
}
mdns_list() {
dns-sd -B _services._dns-sd._udp .
}
fi
if [[ $SH_OS_TYPE == Linux ]]; then
# Linux aliases and functions
alias ls='ls --color=auto '
set-title() {
eval "$(__sh_color_definitions)"
local TITLE="$@"
echo -e "Setting terminal title to "$COLOR_YELLOW_BOLD"$TITLE"$COLOR_NONE
export PROMPT_COMMAND="echo -n -e \"\033]0;$TITLE\007\"";
}
rep() {
eval "$(__sh_color_definitions)"
if [[ $# -eq 0 ]]; then
echo -e "Usage: rep {sed command}"
echo
echo -e "Rep will execute the sed command on all files in the current directory"
echo -e "and subdirectories, excluding any hidden files. All commands will be"
echo -e "executed in place using sed's -i option."
echo
sed --help
echo
else
for x in $(find . -type f -name '*' ! -name '.*' ! -path '*/.*'); do
sed -i "$@" $x
done
# This is a bit dangerous, and assumes that there are no legitimate files
# that start with .!. Probably should be a bit more careful and match
# more specifically
find . -name "\.\!*" -exec rm {} \;
fi
}
if [[ $COLORTERM == gnome-terminal ]] ||
[[ $SESSIONTYPE == gnome-session ]] ||
[[ $XDG_CURRENT_DESKTOP == GNOME ]]; then
ex() {
if [[ $# -eq 0 ]]; then
nautilus . &
else
nautilus "$@" &
fi
}
e() {
gedit "$@" &
}
regedit() {
gconf-editor "$@" &
}
else
e() {
vim "$@"
}
fi
if [[ $SH_OS_DISTRO == CentOS ]]; then
# CentOS aliases and functions
alias __dummy='echo dummy'
# CentOS doesn't have --group-directories-first :-(
# Hence why dir is specific for Ubuntu and Debian
fi
if [[ $SH_OS_DISTRO == Debian ]] ||
[[ $SH_OS_DISTRO == Ubuntu ]] ||
[[ $SH_OS_DISTRO == Raspbian ]]; then
# Debian aliases and functions
alias dir='ls -al --group-directories-first '
if [[ $SH_OS_FLAVOR == WSL ]]; then
ex() {
if [[ $# -eq 0 ]]; then
explorer.exe .
else
explorer.exe `wslpath -w $1`
fi
}
fi
latest() {
sudo apt-get update
# sudo apt-get upgrade -y
sudo apt-get --with-new-pkgs upgrade -y
sudo snap refresh
if [[ -z $LATEST_SKIP_NPM ]]; then
if [[ $(which npm 2>/dev/null) ]]; then
npm install -g npm@latest
npm update -g
fi
fi
if [[ -z $LATEST_SKIP_YARN ]]; then
if [[ $(which yarn 2>/dev/null) ]]; then
yarn global upgrade --latest --non-interactive
fi
fi
}
fi
if [[ $SH_OS_DISTRO == Ubuntu ]]; then
# Ubuntu aliases and functions
# Ubuntu has a weird bug with graphical sudo and DBUS
sudo() {
local _SUDO_SAVE=$DBUS_SESSION_BUS_ADDRESS
unset DBUS_SESSION_BUS_ADDRESS
command sudo -E env PATH="$PATH" "$@"
export DBUS_SESSION_BUS_ADDRESS=$_SUDO_SAVE
}
gksudo() {
local _SUDO_SAVE=$DBUS_SESSION_BUS_ADDRESS
unset DBUS_SESSION_BUS_ADDRESS
command gksudo -k "$@" &
export DBUS_SESSION_BUS_ADDRESS=$_SUDO_SAVE
}
fi
if [[ $SH_OS_DISTRO == Yocto ]]; then
# Yocto grep doesn't support --color=auto
unalias grep
ff() {
find . -name "*" -type f -exec grep -HinF "$@" {} \;
}
ff2() {
# Double-grep to filter out grep warnings
grep -RHinF "$@" * 2>&1 | grep -i "$@"
}
fff() {
find . -name "$1" -type f -exec grep -HinF "${@:2}" {} \;
}
fff2() {
# Double-grep to filter out grep warnings
grep -RHinF "${@:2}" "$1" 2>&1 | grep -i "${@:2}"
}
ffs() {
set | grep -i "$@"
}
fp() {
ps | grep -i "$@"
}
fi
fi
if [[ $SH_OS_TYPE == Windows ]]; then
# Windows aliases
alias ls='ls --color=auto'
e() {
vim "$@"
}
ex() {
if [[ $# -eq 0 ]]; then
start .
else
if [[ -d "$1" ]]; then
start "$@"
else
notepad "$@"
fi
fi
}
latest() {
pacman -Syuu
}
fi
# Late load after bashrc main finished
__aliases_load() {
local ALIASES_LOAD
if [[ $SH_OS_TYPE == OSX ]]; then
# OS X aliases
# Eternal Terminal
if [[ "$LC_TERMINAL" == "iTerm2" ]]; then
if [[ -n `type -t et 2>/dev/null` || -n `whence et 2>/dev/null` ]]; then
ett() {
et -p 8090 -c "tmux -CC a || tmux -CC || /usr/bin/env bash" $*
}
etz() {
et -p 8090 -c "tmux -CC a || tmux -CC || /usr/bin/env zsh" $*
}
fi
fi
# Sublime Edit
if [[ -n `type -t subl 2>/dev/null` || -n `whence subl 2>/dev/null` ]]; then
subl() {
"/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" "$@"
}
fi
# Check if Visual Code is installed
# If not, check if Sublime Edit is installed
# If not, use TextEdit
e() {
local CODE_COMMAND SUBL_COMMAND
CODE_COMMAND=`type -t code 2>/dev/null || whence code 2>/dev/null`
SUBL_COMMAND=`type -t subl 2>/dev/null || whence subl 2>/dev/null`
if [[ -n $CODE_COMMAND ]]; then
# Eval because code command can be alias
`code "$@"`
else
if [[ -n $SUBL_COMMAND ]]; then
subl "$@"
else
open -e "$@"
fi
fi
}
fi
}
}
__aliases_main "$@"
unset -f __aliases_main