-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.aliases
585 lines (561 loc) · 33 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
#!/usr/bin/env bash
# -------------------------------------------------------------------
# Config (set your own in $HOME/.aliases_local)
# -------------------------------------------------------------------
GIT_USERNAME=${GIT_USERNAME:-Festum Qin}
GIT_USEREMAIL=${GIT_USEREMAIL:-festum@g.pl}
GIT_LOG_FORMAT=${GIT_LOG_FORMAT:-"format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset'"}
# -------------------------------------------------------------------
# Setup
# -------------------------------------------------------------------
safe_source() { [ -f $1 ] && [ -s $1 ] && [ -r $1 ] && source "$1"; }
is_runnable() { command -v $1 >/dev/null 2>&1; }
app_alias() { is_runnable $1 && ! [ -f ~/.local/bin/$2 ] && mkdir -p ~/.local/bin/ && ln -s $(which $1) ~/.local/bin/$2; }
git_install() { [ ! -d $2/.git ] && git clone --depth 1 $1 $2; }
bare_run() {
local TARGET_BIN=$(echo "$@" | awk '{print $1}')
alias "$TARGET_BIN" &>/dev/null && {
OLD_ALIAS=$(alias "$TARGET_BIN")
unalias "$TARGET_BIN"
eval "$@"
alias "$OLD_ALIAS"
} || eval "$@"
}
safe_source $HOME/.aliases_local
safe_source $HOME/.aliases.local
safe_source $HOME/configs/custom_aliases
[ -f $HOME/.remote/.aliases ] && source $HOME/.remote/.aliases && export BASH_IT_THEME=candy
sshz() {
[ "$(uname)" = "Darwin" ] && break_arg='-b 0' || break_arg='-w0'
[ -f $HOME/.remote/.aliases ] && (
RC_DATA=$(cat $HOME/.remote/.bashrc | base64 $break_arg)
AL_DATA=$(cat $HOME/.remote/.aliases | base64 $break_arg)
) || (
RC_DATA=$(cat $HOME/.bashrc | base64 $break_arg)
AL_DATA=$(cat $HOME/.aliases | base64 $break_arg)
)
ssh -t $@ "mkdir -p $HOME/.remote;echo \"${RC_DATA}\" | base64 --decode > $HOME/.remote/.bashrc;echo \"${AL_DATA}\" | base64 --decode > $HOME/.remote/.aliases;bash --rcfile $HOME/.remote/.bashrc"
}
exit_and_rm() { rm -rf $HOME/.fe0/ && exit; }
alias sshr='ssh -R 52698:localhost:52698'
alias exit=exit_and_rm
alias bye=exit
ttmux() { [ -z $TMUX ] && source $HOME/.bash_it/.bashrc && tmux new-session -A -s main; }
# -------------------------------------------------------------------
# Aliases
# -------------------------------------------------------------------
# General
alias c="clear && printf '\e[3J'"
alias cls="echo -ne '\033c'"
alias wget='wget -c' # with resume download
alias cic='set completion-ignore-case On' # cic: Make tab-completion case-insensitive
alias src='unalias -a && source $HOME/.aliases' # src: Reload .bashrc file
alias tcn='mv --force -t $HOME/.local/share/Trash '
alias h='history | tail'
alias hg='history | grep'
alias hrmdup='nl ~/.bash_history | sort -k 2 -k 1,1nr| uniq -f 1 | sort -n | cut -f 2 | sed -e "/^#d*/d" > unduped_history && cp -f unduped_history ~/.bash_history'
alias g='git'
alias {ack,ak}='ack-grep'
alias uig='f(){ grep -i --color $@ /etc/group; unset -f f >/dev/null 2>&1; }; f' # uig: List file groups
# Directory Listing
alias path='echo -e ${PATH//:/\\n}' # path: Echo all executable Paths
alias dir='is_runnable eza && eza -hF --classify || ls -hFx'
alias ll='is_runnable eza && eza -bghHlS || ls -GFhl'
alias l.='is_runnable eza && eza -A --hidden --long || ls -d .* --color=tty' # short listing, only hidden files - .*
alias l='is_runnable eza && eza -la --time modified -h --classify || ls -lathF' # long, sort by newest to oldest
alias L='is_runnable eza && eza -la --time modified -h --classify --reverse || ls -latrhF' # long, sort by oldest to newest
alias la='is_runnable eza && eza -lA || ls -lA' # show hidden files
alias lc='is_runnable eza && eza -lA --sort changed --reverse || ls -lcr' # sort by change time
alias lh='is_runnable eza && eza -lA --sort size || ls -lSrh' # sort by size human readable
alias lm='is_runnable eza && eza -A --long | more || ls -al | more' # pipe through 'more'
alias lo='is_runnable eza && eza -lA --sort size --reverse -h --classify || ls -laSFh' # sort by size largest to smallest
alias lr='is_runnable eza && eza --tree || ls -lR' # recursive ls
alias lt='is_runnable eza && eza -l --time modified --reverse || ls -ltr' # sort by last modified time
alias lu='is_runnable eza && eza -l --sort accessed --reverse || ls -lur' # sort by last access time
alias left='is_runnable eza && eza --sort modified -1 || ls -t -1' # sort by modified for 1 column
alias lr='ls -R | grep ":$" | sed -e '\''s/:$//'\'' -e '\''s/[^-][^\/]*\//--/g'\'' -e '\''s/^/ /'\'' -e '\''s/-/|/'\'' | less' # Full Recursive Directory Listing
alias less='less -FSRXc'
alias dud='du -d 1 -h' # Short and human-readable file listing
alias duf='du -sh *' # Short and human-readable directory listing
# Permission
alias sui='sudo -s'
alias sudid='sudo !!'
alias owned='sudo chown $(id -u -n)'
alias perm='stat --printf "%a %n \n "' # perm: Show permission of target in number
alias 000='chmod 000' # ---------- (nobody)
alias 640='chmod 640' # -rw-r----- (user: rw, group: r, other: -)
alias 644='chmod 644' # -rw-r--r-- (user: rw, group: r, other: -)
alias 755='chmod 755' # -rwxr-xr-x (user: rwx, group: rx, other: x)
alias 775='chmod 775' # -rwxrwxr-x (user: rwx, group: rwx, other: rx)
alias mx='chmod a+x' # ---x--x--x (user: --x, group: --x, other: --x)
alias ux='chmod u+x' # ---x------ (user: --x, group: -, other: -)
alias 644f='find . -type f -exec chmod 644 {} \;'
alias 755d='find . -type d -exec chmod 755 {} \;'
alias chm='644f && 755d' # normalize permission
# gpin: Join group
alias gpls='cat /etc/group | cut -d: -f1' # gpls: List groups
alias vboxin='usermod -aG vboxsf $USER' # vboxin: Allow me to have access shared folder
# File and folder
alias rd='rm -rf'
alias rd.='TMP=`pwd -P` && cd "`dirname $TMP`" && rm -rf "./`basename $TMP`" && unset TMP'
alias rg="rg --hidden --glob '!.git' --no-follow --column -F"
alias rmi='rm -i'
alias rm0='find . -size 0 -print0 |xargs -0 rm --' # Remove zero size files
alias cpv='rsync -ah --info=progress2' # CP continuously
alias mv='mv -iv'
alias mkdir='mkdir -pv'
# alias md='f(){ mkdir -p $1 && cd $1; unset -f f >/dev/null 2>&1; }; f' # Create folder and goto
alias ln='ln -v'
alias ~='cd ~'
alias -- -='cd ~-'
alias d='cd $HOME/Downloads/'
alias numFiles='echo $(ls -1 | wc -l)' # numFiles: Count of non-hidden files in current dir
alias make1mb='truncate -s 1m ./1MB.dat' # make1mb: Creates a file of 1mb size (all zeros)
alias make5mb='truncate -s 5m ./5MB.dat' # make5mb: Creates a file of 5mb size (all zeros)
alias make10mb='truncate -s 10m ./10MB.dat' # make10mb: Creates a file of 10mb size (all zeros)
if [ "$(echo $0)" = "bash" ]; then
alias ffind='f(){ find . -type f -iname '*'$@'*' -ls | grep -v "Permission denied"; unset -f f >/dev/null 2>&1; }; f' # Find a file with a pattern in name:
alias efind='f(){ find . -type f -iname '*'$@'*' -exec "${2:-file}" {} \; unset -f f >/dev/null 2>&1; }; f' # Find a file with pattern $1 in name and Execute $2 on it:
alias qfind="find . -name " # qfind: Quickly search for file
fi
alias count='f(){ cat $@ |wc -l; unset -f f >/dev/null 2>&1; }; f' # count: Get the number of files in this folder
alias rep='find . -type f -print0 | xargs -0 sed -i' # rep: Find file by string
# Process
alias free='free -mt'
alias hogmt='top -l 1 -o rsize | head -20' # hogmt: Find memory hogs by top
alias hogmp='ps wwaxm -o pid,stat,vsize,rss,time,command | head -10' # hogmp: Find memory hogs by ps
alias hogcp='ps wwaxr -o pid,stat,%cpu,time,command | head -10' # hogcp: Find CPU hogs
alias ktop='top -l 9999999 -s 10 -o cpu' # ktop: top listing every 10s
alias ttop="top -R -F -s 10 -o rsize" # ttop: invocation to minimize resources
alias psls='ps auxf'
alias psa='ps -Ao pid,comm,pcpu,pmem --sort=-pcpu | head -11' # psa: List processes with minimal info
alias psc='ps -ax -opid,lstart,pcpu,cputime,comm --sort=-%cpu,-cputime | head -11' # psc: List processes by CPU
alias psm='ps -ax -opid,lstart,pmem,rss,comm --sort=-pmem,-rss | head -11' # psm: List processes by memory
alias psp='f(){ sudo netstat -nlp | grep $@; unset -f f >/dev/null 2>&1; }; f' # psp: Get process by port
alias psg='ps aux | grep -v grep | grep -i -e VSZ -e' # psg: Search process
alias daemon='f(){ $@ > /dev/null 2>&1 &; unset -f f >/dev/null 2>&1; }; f' # daemon: Run in background
alias daemono='f(){ $@ >> ./out 2>&1 &; unset -f f >/dev/null 2>&1; }; f' # daemono: Run in background with output
alias fu='sudo kill -9' # fu: Force kill
alias fuu='sudo killall' # fuu: Kill them all
alias fuuu='sudo killall -9' # fuuu: Force kill them all
alias setcur='f(){ echo -e -n "\x1b[\x3${1:-3} q"; unset -f f >/dev/null 2>&1; }; f' # setcur: set cursor 0-6
# Networking
alias netCons='lsof -i' # netCons: Show all open TCP/IP sockets
alias lsock='sudo /usr/sbin/lsof -i -P' # lsock: Display open sockets
alias lsockU='sudo /usr/sbin/lsof -nP | grep UDP' # lsockU: Display only open UDP sockets
alias lsockT='sudo /usr/sbin/lsof -nP | grep TCP' # lsockT: Display only open TCP sockets
alias ipInfo0='ifconfig getpacket en0' # ipInfo0: Get info on connections for en0
alias ipInfo1='ifconfig getpacket en1' # ipInfo1: Get info on connections for en1
alias openPorts='sudo lsof -i | grep LISTEN' # openPorts: All listening connections
alias showBlocked='sudo ipfw list' # showBlocked: All ipfw rules inc/ blocked IPs
alias myip='curl ifconfig.me' # myip: Show my external IP
alias myips="ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'"
alias newip='sudo ipconfig set en0 BOOTP ; ipconfig set en0 DHCP' # Request a new DHCP lease for a new IP address
alias portf='f(){ sudo lsof -ti :$1 | xargs sudo kill -15; unset -f f >/dev/null 2>&1; }; f' # portf: Free occupied process for certain port
alias portt='f(){ sudo lsof -ti :$1 | xargs sudo kill -9; unset -f f >/dev/null 2>&1; }; f' # portt: Kill occupied process for certain port
alias dl='f(){ curl -OJ $@; unset -f f >/dev/null 2>&1; }; f' # dl: Download
alias dlb='f(){ sudo curl -o "/usr/local/bin/${1##*/}" $1 && sudo sudo chmod +x "/usr/local/bin/${1##*/}"; unset -f f >/dev/null 2>&1; }; f' # dlb: Download binary and move as executable
alias transfer='f(){ curl --progress-bar --upload-file $1 https://transfer.sh/$(basename $1) | tee /dev/null; unset -f f >/dev/null 2>&1; }; f' # transfer: Upload to public
alias serveo='f(){ ssh -R 80:$1 serveo.net; unset -f f >/dev/null 2>&1; }; f' # dl: Expose local port
alias serveop='f(){ autossh -M 0 -R 80:$1 serveo.net; unset -f f >/dev/null 2>&1; }; f' # dl: Expose local port as daemon
# Date
alias bdate="date '+%a, %b %d %Y %T %Z'"
alias cal3='cal -3'
alias da='date "+%Y-%m-%d %A %T %Z"'
alias daysleft='echo "There are $(($(date +%j -d"Dec 31, $(date +%Y)")-$(date +%j))) left in year $(date +%Y)."'
alias epoch='date +%s'
alias secconvert='date -d@1234567890'
alias stamp='date "+%Y%m%d%a%H%M"'
alias timestamp='date "+%Y%m%dT%H%M%S"'
alias today='date +"%A, %B %-d, %Y"'
alias weeknum='date +%V'
# Encryption
alias gpgl='gpg --list-options show-unusable-uids --list-keys'
alias gpga='gpg --default-new-key-algo "ed25519/cert,sign+cv25519/encr" --quick-generate-key'
alias gpges='gpg --armor --export-secret-key'
alias gpgep='gpg --armor --export'
alias gpgek='gpg --edit-key'
alias gpgd='f(){ gpg --delete-secret-key $1; gpg --delete-key $1 ; unset -f f >/dev/null 2>&1; }; f'
alias gpgtu='f(){ gpg --edit-key $1 trust 6; unset -f f >/dev/null 2>&1; }; f'
# -------------------------------------------------------------------
# Tmux
# -------------------------------------------------------------------
alias tma='tmux attach -t'
alias tmn='tmux new -s'
alias tml='tmux ls'
alias tmd='tmux detach'
alias tmk='tmux kill-session -t'
alias tmkk='tmux kill-server'
alias tms='tmux switch -t'
alias tmwn='tmux new-window'
alias tmwl='tmux list-window'
alias tmws='tmux select-window -t' # :0-9
alias tmwr='tmux rename-window'
alias tmpw='tmux split-window'
alias tmps='tmux swap-window' # -[UDLR]
alias tmpp='tmux select-pane' # -[UDLR]
alias tmno='tmuxinator open'
alias tmns='tmuxinator start'
# -------------------------------------------------------------------
# Coding
# -------------------------------------------------------------------
# Go
alias goi='go install'
alias gobin='sudo GO111MODULE=on GOBIN=/usr/local/bin go install'
alias got='go test ./... -tags test'
alias gor='f() { arg=${@:-.}; go run $arg; unset -f f >/dev/null 2>&1; }; f'
alias gom='go mod'
alias gog='go get -u'
alias gogg='GO111MODULE=off go get -u'
alias gogd='go get -d ./...'
alias gol='golangci-lint --color always'
alias gopi='echo export GOPATH=$(pwd) > .envrc'
# Node
alias node='node --harmony'
# Kotlin
alias ktl="ktlint -F '**/*.kt'"
# Python
alias pipi='pip3 install -U'
alias pipr='pip3 install -U -r requirements.txt'
alias pipu='pip3 install -U $(pip3 freeze | cut -d '=' -f 1)'
alias pips='f() { package_name=$1; requirements_file=${2:-./requirements.txt}; pip install "$package_name" && pip freeze | grep -i "$package_name" >> "$requirements_file"; unset -f f >/dev/null 2>&1; }; f'
alias venv='python3 -m venv .venv && source .venv/bin/activate'
# -------------------------------------------------------------------
# Git
# -------------------------------------------------------------------
alias undo='git reset --hard && git clean -fd'
alias save='git stash push -u'
alias stash='git stash'
alias staa='git stash apply'
alias stab='git stash branch'
alias stac='git stash clear'
alias stad='git stash drop -q'
alias stal='git stash list'
alias stap='git stash pop'
alias stas='git stash show -p'
alias diff='git diff --no-index --ignore-blank-lines --color'
alias git-main-branch='git symbolic-ref refs/remotes/origin/HEAD | cut -d"/" -f4'
alias pname='basename `git rev-parse --show-toplevel`'
alias ga='git add'
alias gaa='git add .'
alias gb='git branch'
alias gbs='f() { git checkout --recurse-submodules $1 2>/dev/null || git checkout -b $1; unset -f f >/dev/null 2>&1; }; f'
alias gbl='git branch-select -l'
alias gbr='git rev-parse --abbrev-ref --symbolic-full-name @{u}'
alias gbd='git branch -D'
alias gbdr='git push -d origin'
alias gbdd="git fetch --prune && git branch -vv | grep ': gone]' | awk '{print \$1}' | xargs -r git branch -D"
alias gbdc='git branch | grep -E -v "(^\*|master|main|dev|develop)" | xargs git branch -D'
alias gbo='git for-each-ref --sort=committerdate refs/heads/ --format="%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))"'
alias gc='git checkout --recurse-submodules'
alias gcat='git cat-file'
alias gcfg='$VISUAL .git/config'
alias gcm='git checkout --recurse-submodules $(git-main-branch)'
alias gcp='git cherry-pick'
alias gc1='git clone --depth=1'
alias gcl='f() { [ -n "$2" ] && git clone "$1" "$2" && cd "$2" || (git clone "$1" && cd "$(basename "$1" .git)"); unset -f f >/dev/null 2>&1; }; f'
alias gcln='f() { git clone --filter=blob:none --no-checkout "$1" && cd "$(basename "$1" .git)"; unset -f f >/dev/null 2>&1; }; f'
alias gd='git diff --no-renames -b -w --ignore-blank-lines --color'
alias gda='git diff --shortstat $(git-main-branch) | sed -E "s/([0-9]+) file.* ([0-9]+) insertion.* ([0-9]+) deletion.*/#\1 +\2 -\3/"'
alias gdc='git diff --cached --ignore-blank-lines --color'
alias gdcs='git diff --cached --shortstat --ignore-blank-lines --color'
alias gdev='git checkout $(git-main-branch) && git branch -D dev && git pull && git checkout -b dev'
alias gdm='git diff --cached $(git-main-branch)...'
alias gdvs='git difftool --tool=vscode'
alias gds='gd --stat'
alias gf='git fetch -p'
alias gfa='gf --progress --all' # Fetch all
alias gfo='gf --progress origin' # Fetch origin
alias gm='git commit -m '
alias gm0='git commit -C HEAD@{1}' # Reuse last commit message and timestamp to create a commit
alias gma='git commit -am' # Commit message with auto stage all files
alias gmm='git commit --amend -m' # Amend message on last commit
alias gms='git commit --amend --no-edit' # Stage other files on last commit
alias gmu='git -c user.name="${GIT_USERNAME}" -c user.email="${GIT_USEREMAIL}" commit -m '
alias gmg='git merge'
alias gmgp='git merge @{-1}'
alias gmgm='git merge $(git-main-branch)'
alias gmv='git mv'
alias gp='git pull'
alias gpa='git pull --all'
alias gpf='git pull --all --rebase --autostash'
alias gpm='git pull --rebase --autostash origin $(git-main-branch)'
alias gpu='git fetch upstream && git rebase upstream/$(git-main-branch)'
alias gpush=gu
alias gl='git log --date=iso --name-status'
alias glp='git log --pretty="${GIT_LOG_FORMAT}" --abbrev-commit --no-merges --date=iso'
alias glg='git log --graph --topo-order --decorate --oneline --all'
alias glc='git log --pretty=format:'%s' | cut -d " " -f 1 | sort | uniq -c | sort -nr' # get counts
alias gld='rm -f .git/index.lock'
alias grp='git rev-parse'
alias grb='git rebase'
alias grba='git rebase --abort'
alias unlockref='git remote prune origin'
alias grbc='git rebase --continue'
alias grbs='git rebase --skip'
alias grbi='git rebase --interactive' # merge
alias grbm='git rebase -i --autosquash origin/$(git-main-branch) --no-verify'
alias grbmb='save && gc $(git-main-branch) && gp && gc - && grbm'
alias grc='git ls-files --deleted -z | xargs -0 git rm' # clean
alias grf='git reflog'
alias grm='git rm --ignore-unmatch -f -r --cached'
alias grmt='git remote'
alias grma='git remote add'
alias grmau='git remote add upstream'
alias grmsh='git remote set-head origin --auto'
alias grmsu='git remote set-url --add --push origin'
alias grmrm='git remote rm'
alias checkCommitter='export IAM_COMMITTER=false && [ $(git log -1 --pretty=format:"%ae") = $(git config user.email) ] && export IAM_COMMITTER=true'
alias grs='git reset'
alias grs0='checkCommitter && [ $IAM_COMMITTER = true ] && git add . && git commit --amend --no-edit'
alias grs1='checkCommitter && [ $IAM_COMMITTER = true ] && grs --soft HEAD^1'
alias grh1='checkCommitter && [ $IAM_COMMITTER = true ] && grs --hard HEAD^1'
alias grhh='git update-ref -d HEAD' # Git reset first commit
alias gs='git status -sb'
alias gmod="git status --porcelain | sed -ne 's/^ M //p'"
alias gsm='git submodule'
alias gsmi='git submodule update --init --recursive'
alias gsmu='git submodule update --remote --merge --recursive'
alias gsmp='git submodule foreach git pull'
alias gsmr='git submodule foreach --recursive git reset --hard'
alias gsho='git show --color'
alias gslc="git shortlog | grep -E '^[ ]+\w+' | wc -l"
alias gslu="git shortlog | grep -E '^[^ ]'"
alias gt='git tag'
alias gdt='git describe --tags --abbrev=0'
alias ghusky='rm -rf .git/hooks/ && npm i -D husky' # https://github.com/typicode/husky/issues/333
alias gu='git push --force-with-lease'
alias gus='git push --force-with-lease -o ci.skip'
alias gub='git push -u origin $(git rev-parse --abbrev-ref HEAD)' # git branch --show-current for git 2.22
alias gubs='git push --force-with-lease -o ci.skip -u origin $(git rev-parse --abbrev-ref HEAD)'
alias gum='git push -o merge_request.create -o merge_request.target=$(git-main-branch) -o merge_request.merge_when_pipeline_succeeds'
alias guo='git push --force-with-lease origin'
alias ggpg='git config user.signingkey $(gpg --list-secret-keys --keyid-format LONG | grep -B 1 $(git config user.email) | head -n 1 | awk "{print \$1}")'
gta2() { [ -z $1 ] && echo -e "use it like that: $gt \$tagname\nit will do: git tag -am\"$(date +%Y-%m-%d) \$tagname\" \$tagname\nexample: $gt v0.0.1" || printf "---- git tag -f -am\"$(date +%Y-%m-%d) $1\" $1\n" && git tag -f -am"$(date +%Y-%m-%d) $1" $1 && printf "---- done\n"; }
mr() {
local remote_url=$(git config --get remote.origin.url)
local current_branch=$(git rev-parse --abbrev-ref HEAD)
local default_branch=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')
[[ $remote_url == *"github.com"* ]] && open "$(echo "$remote_url" | sed 's/git@github.com:/https:\/\/github.com\//' | sed 's/\.git$//')/compare/$default_branch...$current_branch" || [[ $remote_url == *"bitbucket.org"* ]] && open "$(echo "$remote_url" | sed 's/git@bitbucket.org:/https:\/\/bitbucket.org\//' | sed 's/\.git$//')/pull-requests/new?source=$current_branch&t=1" || (
local base_url=$(echo "$remote_url" | sed 's/git@\(.*\)\:\(.*\)\.git/https:\/\/\1\/\2/')
open "$base_url/merge_requests/new?merge_request%5Bsource_branch%5D=$current_branch"
)
}
pr() { open "$(git ls-remote --get-url $(git config --get branch.$(git rev-parse --abbrev-ref HEAD).remote) | sed 's|git@github.com:\(.*\)$|https://github.com/\1|' | sed 's|\.git$||')/compare/$(git config --get branch.$(git rev-parse --abbrev-ref HEAD).merge | cut -d '/' -f 3-)?expand=1"; }
ltag() { curl --silent "https://github.com/$1/releases/latest" | sed 's#.*tag/\(.*\)\".*#\1#' && echo ''; }
# ------------------------------------
# Docker related
# ------------------------------------
alias dk='docker'
alias dkx='docker exec -it'
alias dkm='docker commit'
alias dkl='docker logs'
alias dpush='docker push'
alias dkb='docker buildx'
alias dkr='docker run -it -d -P'
alias dkup='f(){ dockerfile=${1:-Dockerfile}; docker run -it "$(dkb build -q -f $dockerfile .) $2"; unset -f f >/dev/null 2>&1; }; f'
alias dkup1='f(){ dockerfile=${1:-Dockerfile}; docker run -it --rm "$(dkb build -q -f $dockerfile .) $2"; unset -f f >/dev/null 2>&1; }; f'
alias dkp='docker ps -a'
alias dkpl='docker ps -l -q'
alias dkpf='docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Ports}}\t{{.Command}}\t{{.Status}}\t"'
alias dkpip="docker ps -q | xargs -n 1 docker inspect --format '{{ .Name }} {{range .NetworkSettings.Networks}} {{.IPAddress}}{{end}}' | sed 's#^/##';"
alias dks='docker stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"'
alias dkr='f(){ docker ps -ga -f name="$@" | xargs --no-run-if-empty docker rm -f; unset -f f >/dev/null 2>&1; }; f'
alias dkrm='f(){ docker rm $(docker ps -aq -f name="$@"); unset -f f >/dev/null 2>&1; }; f'
alias dkrma='docker rm -f $(docker ps -aq)'
alias dkrme='docker rm $(docker ps -aq -f status=exited)'
alias dkrmi='docker rmi'
alias dkrmin='docker rmi $(docker images | grep "^<none>" | awk "{ print $3 }")'
alias dkra='docker ps -qa | xargs --no-run-if-empty docker rm -f'
alias dki='docker images'
alias dkip='docker image prune'
alias dsh='f(){ docker exec -it "$@" /bin/bash; unset -f f >/dev/null 2>&1; }; f'
alias dc='docker compose --compatibility'
alias dcb='dc build'
alias dcd='dc down'
alias dcu='dc up --remove-orphans'
alias dcub='dcu --build'
alias dcubf='dcub --force-recreate'
alias dcubn='dcub --no-deps'
alias dcrs='dcd && dc up -d'
alias dcl='dc logs'
alias dclt='dcl --tail="all"'
alias dcx='dc exec'
alias k8='kubectl'
alias k8a='kubectl apply -f'
alias k8c='kubectl config'
alias k8cc='k8c current-context'
alias k8s='k8c use-context'
alias k8cv='kubectl view'
alias k8d='kubectl describe'
alias k8ns='f(){ kubectl config set-context --current --namespace=$1; unset -f f >/dev/null 2>&1; }; f'
alias k8g='kubectl get'
alias k8gpip='f(){ kubectl get pod $1 -o custom-columns=NAME:metadata.name,IP:status.podIP; unset -f f >/dev/null 2>&1; }; f'
alias k8ga='kubectl get --all-namespaces'
alias k8gd='k8g deploy,svc,pods,pvc,rc,rs'
alias k8l='kubectl logs --all-containers'
alias k8t='k8l --tail=1'
alias k8r='kubectl delete'
alias k8rp='k8r --grace-period=1 po'
alias k8x='kubectl exec -it'
alias kc='kompose'
alias kcc='kc convert -f'
alias mk='minikube'
dmp() {
PORT=$(python -c 'import socket; s = socket.socket(); s.bind(("127.0.0.1", 0)); print s.getsockname()[1]; s.close();')
ssh -f -o ExitOnForwardFailure=yes -L "127.0.0.1:$PORT:127.0.0.1:2375" "$PRODSERVER"
sleep 5
DOCKER_HOST="127.0.0.1:$PORT" "$@"
}
# ------------------------------------
# Serverless and AWS
# ------------------------------------
alias sls='serverless'
alias sld='serverless deploy'
alias sli='serverless invoke --function'
alias slil='serverless invoke local --function'
alias fnc='aws lambda create-function --memory-size 512 --timeout 300 --function-name'
alias login_ecr='$(aws ecr get-login --region eu-central-1 --no-include-email)'
alias login_ghcr='echo $CR_PAT | docker login ghcr.io -u $(git config user.email) --password-stdin'
alias tf='terraform'
alias tfr='terraformer'
alias tco='PYTHONUNBUFFERED=1 LOG_LEVEL=ERROR checkov --quiet --skip-check CKV_TF_1 -d .'
# -------------------------------------------------------------------
# Converters
# -------------------------------------------------------------------
alias 7za='7z a -t7z -m0=LZMA2:27 -mx=9 -ms=on -mfb=273 -mtm=- -mmt -mmtf -md=1536m -mmf=bt3 -mmc=10000 -mpb=0 -mlc=0 -mhe=on'
alias i2='convert -density 300 -quality 100'
# -------------------------------------------------------------------
# Commands mapping
# -------------------------------------------------------------------
! is_runnable code && is_runnable code-oss && alias code=code-oss
! is_runnable code && is_runnable codium && alias code=codium
is_runnable code && alias ed='f() { file=${1:-.}; [ "$file" != "." ] && file="-r ${file}"; code $file 2>/dev/null; unset -f f >/dev/null 2>&1; }; f'
is_runnable code && alias vc=ed
is_runnable nvim && alias nv=nvim
! is_runnable pm && is_runnable homebrew && alias pm='sudo homebrew'
! is_runnable pm && is_runnable cave && alias pm='sudo cave'
! is_runnable pm && is_runnable pkgng && alias pm='sudo pkgng'
! is_runnable pm && is_runnable pkg_tools && alias pm='sudo pkg_tools'
! is_runnable pm && is_runnable sun_tools && alias pm='sudo sun_tools'
! is_runnable pm && is_runnable tazpkg && alias pm='sudo tazpkg'
! is_runnable pm && is_runnable swupd && alias pm='sudo swupd'
! is_runnable pm && is_runnable tlmgr && alias pm='sudo tlmgr'
! is_runnable pm && is_runnable conda && alias pm='sudo conda'
! is_runnable pm && is_runnable portage && alias pm='sudo portage'
! is_runnable pm && is_runnable dnf && alias pm='sudo dnf'
! is_runnable pm && is_runnable pacman && alias pm='sudo pacman'
! is_runnable pm && is_runnable yum && alias pm='sudo yum'
! is_runnable pm && is_runnable eopkg && alias pm='sudo eopkg'
! is_runnable pm && is_runnable apk && alias pm='sudo apk'
! is_runnable pm && is_runnable apt-get && alias pm='sudo apt-get'
! is_runnable pm && is_runnable pkg && alias pm='sudo pkg'
is_runnable hstr && alias hh=hstr
is_runnable http && alias https='http --default-scheme=https --verify=no'
is_runnable serverless && alias sls=serverless
is_runnable bw && alias bws='bw list items --search'
is_runnable direnv && alias de=direnv
is_runnable zerotier-one.zerotier-cli && alias zt='sudo zerotier-one.zerotier-cli'
is_runnable surfshark-vpn && alias surf='sudo surfshark-vpn' && alias surfa='sudo surfshark-vpn attack' && alias surfd='sudo surfshark-vpn down'
is_runnable kitty && alias icat='kitty +kitten icat'
#app_alias eza ls
# -------------------------------------------------------------------
# UTILITIES
# -------------------------------------------------------------------
# alert - long running commands
# Usage: sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
swap() { local TMPFILE=tmp.$$ && mv "$1" $TMPFILE && mv "$2" "$1" && mv $TMPFILE $2; }
authadd() { cat $HOME/.ssh/id_rsa.pub | ssh $1 'umask 0077; mkdir -p $HOME/.ssh; cat >> .ssh/authorized_keys && chmod 700 $HOME/.ssh && chmod 600 $HOME/.ssh/authorized_keys && echo "Key copied"'; }
# ex - archive extractor
# Usage: ex <file>.
ex() { if [ -f $1 ]; then case $1 in *.tar.bz2) tar xjf $1 ;; *.tar.gz) tar xzf $1 ;; *.bz2) bunzip2 $1 ;; *.rar) unrar e $1 ;; *.gz) gunzip $1 ;; *.tar) tar xf $1 ;; *.tbz2) tar xjf $1 ;; *.tgz) tar xzf $1 ;; *.zip) unzip $1 ;; *.Z) uncompress $1 ;; *.7z) 7z x $1 ;; *) echo "'$1' cannot be extracted via ex()" ;; esac else echo "'$1' is not a valid file"; fi; }
# ex - archiver
ar() {
local BN=$(basename ${1})
tar -czf ${BN}.tar.gz ${1}
}
# ex - downloaded archive installer
# Usage: din <app_name> <installation_parent_path> <archive_strip_top_level>
din() {
local TARGET_PATH='/usr/local/'
[ -n "${2}" ] && TARGET_PATH=${2}
local EX_TYPE=''
[ -n "${3}" ] && EX_TYPE='--strip-components 1'
echo "Extract to ${TARGET_PATH}/${1} ..."
sudo rm -rf ${TARGET_PATH}/${1} && sudo mkdir -p $_ && sudo tar -xzf ~/Downloads/${1}*.tar.gz ${EX_TYPE} -C $_
}
# translate
translate() {
if ! is_runnable trans; then sudo curl -o /usr/local/bin/trans https://git.io/trans && sudo chmod +x /usr/local/bin/trans; fi
trans $@
}
# pdfa - pdf signing
pdfa() { bare_run "gs -dPDFA=1 -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=\"${1%.*}_signed.pdf\" \"$1\""; }
# pdfc - pdf compression
pdfc() {
local target=${1:-.}
mkdir -p ./compressed
find "$target" -name "*.pdf" -exec sh -c 'gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dBATCH -dColorImageResolution=150 -sOutputFile="compressed/$(basename "$1")" "$1"' sh {} \;
}
# pdf2i - convert pdf to 24-bit with alpha png
pdf2i() { sh -c "gs -sDEVICE=png16m -r300 -o \"${1%.*}_%d.png\" \"$1\""; }
# 2pdf - convert images to pdf
i2pdf() { sh -c "gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -o converted_from_images.pdf \"$@\"" _ "$@"; }
# TODO: pdfocr - first install ocrmypdf tesseract-lang
# https://ocrmypdf.readthedocs.io/en/latest/cookbook.html
# ocrmypdf --deskew -l ger+eng -o2 input.pdf output.pdf
# enable color support of ls and also add handy aliases
if is_runnable dircolors; then
test -r $HOME/.dircolors && eval "$(dircolors -b $HOME/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
alias dir='is_runnable eza && eza -hF --classify || dir --color=auto'
# alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='grep -E --color=auto'
fi
if [[ "$(echo $0)" = "bash" && "$(uname)" = "Darwin" ]]; then
alias ll='is_runnable eza && eza -bghHlS || ls -alF'
alias la='is_runnable eza && eza -lA || ls -A'
alias l='is_runnable eza && eza -la --time modified -h --classify || ls -CF'
fi
getstate() { echo "CPU $(LC_ALL=C top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}')% RAM $(free -m | awk '/Mem:/ { printf("%3.1f%%", $3/$2*100) }') HDD $(df -h / | awk '/\// {print $(NF-1)}')"; }
update_terminal_cwd() {
# Identify the directory using a "file:" scheme URL,
# including the host name to disambiguate local vs.
# remote connections. Percent-escape spaces.
local SEARCH=' '
local REPLACE='%20'
local PWD_URL="file://$HOSTNAME${PWD//$SEARCH/$REPLACE}"
printf '\e]7;%s\a' "$PWD_URL"
}
welcome() {
# Basic info
HOSTNAME=$(uname -n)
ROOT=$(df -h 2>/dev/null | awk '$NF=="/"{printf "%s", $5}')
DF='df -h --total 2>/dev/null'
if [ -f /etc/os-release ]; then
DISTO1=$(awk -F'=' '/PRETTY_NAME/ {print $2}' /etc/os-release)
else
DISTO1='Unknown'
DF='df -ha 2>/dev/null'
fi
# System load
MEMORY1=$(free 2>/dev/null | grep Total | awk '{print $3" MB";}')
MEMORY2=$(free 2>/dev/null | grep "Mem" | awk '{print $2" MB";}')
DISK_TOTAL=$(eval $DF | awk 'END {print $2}')
DISK_USED=$(eval $DF | awk 'END {print $3}')
DISK_USAGE=$(eval $DF | awk 'END {print $5}')
LOAD1=$(cat /proc/loadavg 2>/dev/null | awk {'print $1'})
LOAD5=$(cat /proc/loadavg 2>/dev/null | awk {'print $2'})
LOAD15=$(cat /proc/loadavg 2>/dev/null | awk END{'print $3'})
echo "===============================================
- Hostname............: $HOSTNAME
- Linux Distribution..: $DISTO1
- Disk Space..........: $DISK_USED/$DISK_TOTAL ($DISK_USAGE) used
- CPU usage...........: $LOAD1, $LOAD5, $LOAD15 (1, 5, 15 min)
- Memory used.........: $MEMORY1 / $MEMORY2
- Swap in use.........: $(free 2>/dev/null | tail -n 1 | awk '{print $3}') MB
==============================================="
}