-
Notifications
You must be signed in to change notification settings - Fork 109
/
aliases_functions
297 lines (242 loc) · 9.67 KB
/
aliases_functions
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
# vim: syntax=sh
# since this is sourced from shared.lib in the dotfiles repo, we need
# to source other files
if [[ "$BASH_D" == "" || "$BIN_DIR" == "" ]]; then
printf "('${BASH_SOURCE[0]}'): Missing \$BASH_D and \$BIN_DIR environment variable.\n\n"
return 1
fi
# https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html#Pattern-Matching
#shopt -s extglob;
if ! declare -p t_debug > /dev/null 2> /dev/null; then
source "$BASH_D/core_utils"
fi
t_debug "Reading aliases and functions"
if ! declare -p X_PLATFORM_UTILS > /dev/null 2>&1; then
source "$BIN_DIR/cross-platform-utils.bashlib"
fi
function abspath(){
python -c "import os,sys; print(os.path.abspath(sys.argv[1]))" "$1"
}
function command_exists(){
command -v $@ > /dev/null
}
function function_exists(){
[[ "$(type -t $1)" == "function" ]]
}
function cat(){
if [[ $# > 0 ]]; then
local file="$(readlink -f $1)"
case "$file" in
*.md)
glow "$file"
# recurse on the next files
shift 1
if [[ $# > 0 ]]; then
cat $@
fi
;;
*) ccat $*
;;
esac
else
ccat
fi
}
if ! command_exists ccat || ! command_exists glow; then
printf "Missing core utils for fancy 'cat' command with syntax highlighting and preview. Ensure ccat and glow is installed for OS\n"
unset cat
fi
# Strip junk left from converting utf8<->cp1252
alias strip-non-ascii='python -c "import sys,re;[sys.stdout.write(re.sub('\''[^\x09\x0A\x0D\x20-\x7F]'\'', '\'''\'', line)) for line in sys.stdin]"'
alias strip-empty="egrep -v '^\s*$'"
t_debug Setting up aliases for conversions
alias urldecode='python3 -c "import sys, urllib.parse as p; print(p.unquote_plus(sys.argv[1]))"'
alias urlencode='python3 -c "import sys, urllib.parse as p; print(p.quote_plus(sys.argv[1]))"'
alias htmldecode='python3 -c "import sys, html; print(html.unescape(sys.argv[1]))"'
alias htmlencode='python3 -c "import sys, html; print(html.escape(sys.argv[1]))"'
alias html2text='npx html-to-text'
t_debug "Pascal, Kebab, Camel, ... case conversions"
alias pascal-case='~/bin/change-case --pascal'
alias camel-case='~/bin/change-case --camel'
alias snake-case='~/bin/change-case --snake'
alias kebab-case='~/bin/change-case --kebab'
t_debug Use htop if available
if which htop > /dev/null; then
alias top='htop'
fi
IGNORE=""
if ! is_mac; then
t_debug Add custom ignore pattern for GNU ls
#ignore patterns
for i in '*~' '*.pyc'; do
IGNORE="$IGNORE --ignore=$i"
done
fi
t_debug small utils and aliases
# Color output - when CLICOLOR is defined, --color/-G is unneeded
alias v='ls -l'
alias clean-temp='find -E ~ -type f -and -regex ".*~$|.*-ck.js|.*.tmp" -exec rm {} \;'
# Pretty print json
alias prettify_json='python -mjson.tool'
# Indent text before sending it to Stack Overflow
alias indent4="sed -E 's/(^.*)/ \1/'"
alias remove-indent4="sed -E 's/^ //'"
alias de-indent4=remove-indent4
alias unindent4=remove-indent4
t_debug "aliases: setting up node aliases"
# node commands - important that these are enclosed in single quotes to avoid expansion!
alias npm-exec='PATH=$(npm bin):$PATH'
alias coffee='$(npm bin)/coffee'
alias grunt='$(npm bin)/grunt'
alias gulp='$(npm bin)/gulp'
alias mocha='$(npm bin)/mocha'
alias mochify='$(npm bin)/mochify'
alias karma='$(npm bin)/karma'
alias eslint='$(npm bin)/eslint'
alias prettier='$(npm bin)/prettier'
alias faucet='$(npm bin)/faucet'
t_debug "aliases: finished setting up node aliases"
t_debug webserver aliases
# webdev
# it's so long because I enabled UTF8 support: https://stackoverflow.com/a/24517632/200987
alias webserver='python -m SimpleHTTPServer'
alias servers='sudo lsof -iTCP -sTCP:LISTEN -P -n'
# time
alias epoch=millis # tool that we compile ourselves
# find external ip
alias my-ip='curl -s http://ipinfo.io/ip'
alias my-ip-json='curl -s http://ifconfig.co/json'
# SOCKS proxy
alias socks_proxy='ssh -v -D 22222 [email protected] -N'
alias socks_proxy_all='ssh -v -D :22222 [email protected] -N'
function ltrim ()
{
util.esed 's/^[[:space:]]+//'
}
function rtrim ()
{
util.esed 's/[[:space:]]+$//'
}
function trim ()
{
ltrim | rtrim
}
# Used with the git alias functions; gd, gds, gdw, gdws
alias strip-diff-prefix='sed "s/^\([^-+ ]*\)[-+ ]/\\1/"'
# Used a lot in utility setup scripts
function strip-comments(){
util.esed -e 's/#.*//' $@ | strip-empty
}
function strip-color-codes(){
util.esed -e 's/\[.{0,3}3.m//g'
}
# For stack overflow pasting
alias websafe='util.esed -e "s/(diffia|ptflow)/acme-org/gi" -e "s/$USER/my-user/gi" -e "s/(nimble|ptflow|pete)/foo-project/gi" -e "s/(clinic|people)/app-\1/gi" '
alias stack-overflow-no-copy='websafe | indent4 '
alias stack-overflow='stack-overflow-no-copy | pbcopy; pbpaste; echo -e "\n\nCopied to pastebuffer (use stack-overflow-no-copy to avoid this)!"'
# "reminder aliases" for how to suspend and continue a process
alias processes-suspend='killall -sSTOP '
alias processes-start='killall -sSTART '
function psgrep() { ps -ef | grep -i "$@"; };
# System shortcuts
alias apt='sudo apt'
# Restore the original system path if for some reason some
# command in your ~/bin directory does not work
function restore_path() {
PATH="${ORIGINAL_PATH}"
export PATH
}
# Zach Holman's git aliases converted to functions for more flexibility
# @see https://github.com/holman/dotfiles/commit/2c077a95a610c8fd57da2bd04aa2c85e6fd37b7c#diff-4335824c6d289f1b8b41f7f10bf3a2e7
# Being functions allow them to take arguments, such as additional options or filenames
#
# The -r flag to `less` is uses to make it work with the color escape codes
# @depends on strip-diff-prefix alias
function gd() { git diff --color $@ | strip-diff-prefix | less -r; }
function gds() { git diff --staged --color $@ | strip-diff-prefix | less -r; }
function gdw() { git diff --color --word-diff $@ | less -r; }
function gdws() { git diff --staged --color --word-diff $@ | less -r; }
alias gs='git status -sb' # upgrade your git if -sb breaks for you. it's fun.
alias glg='git lg'
alias gco='git co'
alias gbr='git branch'
function gclone(){
[[ $# == 0 ]] && echo "USAGE: gclone [subdir] org/repo" && return 1
pushd ~/dev/;
if [[ $# == 2 ]]; then
[[ -e $1 ]] || mkdir $1
cd $1
shift
fi
gh repo clone $@;
cd $(basename $@);
}
# utility function that returns myname/repo
function local-repo(){
local url=$(git config remote.origin.url) # --> [email protected]:team/proj
echo $url | util.esed -n -e 's/.*github.com[:/]([/a-zA-Z0-9-]+)(\.git)?/\1/p'
}
# View commits in GitHub web
function gh-commit(){
local repo=$(local-repo)
[[ -n $1 ]] && open "https://github.com/$repo/commit/$1" && return
echo "Usage: gh-commit 779490"
}
function gh-compare(){
local repo=$(local-repo)
[[ -n $2 ]] && open "https://github.com/$repo/compare/$1...$2" \
|| echo "Usage: gh-compare 779490 master";
}
# Maven - fail as soon as a test fails
function mvn() { $(which mvn) -Dsurefire.skipAfterFailureCount=1 $EXTRA_MAVEN_OPTS $@; }
#
# Open JIRA issues: `nim 434` opens NIM-434 in the browser
function nim() { xdg-open http://diffia.atlassian.net/browse/NIM-$1; }
# Nice util for listing all declared functions. You use `type` to print them
alias list-functions='declare | egrep '\''^[[:alpha:]][[:alnum:]_]* ()'\''; echo -e "\nTo print a function definition, issue \`type function-name\` "'
tmux-restore () {
if [[ -n $1 ]]; then
local setup_file="$HOME/.tmux/$1.proj"
local dir_file="$HOME/.tmux/$1.dir"
if [[ -e $setup_file ]]; then
[[ -e $dir_file ]] && cd $(bash -c "echo $(cat $dir_file)") # set CWD
command tmux new-session -s "$1" "tmux source-file $setup_file"
else
printf "\nNo such file \"$setup_file\".\nListing existing files:\n\n"
ls -1 ~/.tmux/*.proj
return 1
fi
else
echo "Usage: tmux-restore my-js-setup"
return 1
fi
}
# allow to use alternative npm clients like PNPM with the same name. Lazy :p
# just use direnv's .envrc to define a path to a different npm, like pnpm
function npm(){ if [[ -z "$NPMBIN" ]]; then command npm $@; else $NPMBIN $@; fi; }
function npx(){ if [[ -z "$NPXBIN" ]]; then command npx $@; else $NPMBIN $@; fi; }
alias hub='printf "DEPRECATED: use the Github CLI \`gh\` instead!\n"'
alias n='printf "DEPRECATED: use ASDF for controlling global versions instead!: \`asdf global nodejs 20.0.0\`\n"'
# SSH recipes for myself
# Auto-open Bittorrent Sync
alias timbuktu_btsync="(sleep 2 && open http://localhost:8888)& ssh -L8888:127.0.0.1:8888 [email protected] -N"
# Reverse SSH
# Connect to timbuktu.kopseng.no to allow timbuktu.kopseng.no:10001 to go directly against my own machine
# client 1: timbuktu_reverse_tunnel
# client 2: ssh -l my-client1-user -p 10001 timbuktu.kopseng.no
# Effect: client2 => timbuktu => client1
alias timbuktu_reverse_tunnel='ssh -v -R 10001:localhost:22 timbuktu.kopseng.no -N -l carl-erik'
# https://stackoverflow.com/a/40949364/200987
function delete-untagged-aws-images(){
aws ecr describe-repositories \
| jq --raw-output .repositories[].repositoryName \
| while read repo; do
imageIds=$(aws ecr list-images --repository-name $repo --filter tagStatus=UNTAGGED --query 'imageIds[*]' --output json | jq -r '[.[].imageDigest] | map("imageDigest="+.) | join (" ")');
if [[ "$imageIds" == "" ]]; then continue; fi
aws ecr batch-delete-image --repository-name $repo --image-ids $imageIds;
done
}
function progress-file-size(){
tail -f -c 1 "$1" | pv > /dev/null
}
t_debug "global aliases and functions finished"