-
Notifications
You must be signed in to change notification settings - Fork 5
/
.sh_aliases
313 lines (266 loc) · 9.69 KB
/
.sh_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
#alias sqlplus='env -u NLS_LANG sqlplus'
alias lt="ls -ltra"
if [[ `uname -a` =~ Darwin ]]; then
alias ls="ls -FCG"
else
alias ls="ls -FC --color"
fi
alias ll="ls -l"
alias l='ls -la'
alias rm="rm -i"
alias llast='less `ls -tr1 | tail -1`'
#alias splitmp3="mp3splt -a -t 5.0 -d split_files -o @n_@f"
alias splitmp3="mp3splt -a -t 5.0 -o @f/@n"
# better to use an ackrc
#alias ack="ack --all --follow --ignore-dir data --ignore-dir images"
# Version Control
alias sdl='git diff master --no-prefix | diff_painter.pl | less -R'
#alias gdl='git diff --no-prefix | diff_painter.pl | less -R'
alias gdl='git diff --color-words'
alias gst='git status'
alias grh='git fetch upstream; git reset --hard'
alias gco='git co'
alias gph='git push origin HEAD'
# Show most used commands that might be good candidates for aliases
alias mu='history | cut -d " " -f3 | sort | uniq -c | sort -nr | head -50'
# Compress the cd, ls -l series of commands.
alias lc='cl'
function cl () {
if [ $# = 0 ]; then
cd && ll
else
cd "$*" && ll
fi
}
# Change directory up
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
alias ......='cd ../../../../..'
alias .......='cd ../../../../../..'
alias ........='cd ../../../../../../..'
alias .........='cd ../../../../../../../..'
alias ..........='cd ../../../../../../../../..'
# dir sizes summarized and sorted
alias dus='du -sm * .[a-zA-Z0-9]*/ | sort -n'
# -a archive preserves permissions, times etc -vv double verbose -P partial files okay and progress indicator --remove-soure-files removes files but not empty directories
alias mvrsync='rsync -avvP --remove-source-files'
# newly updated files in the last 5 minutes
alias nf='find . -cmin -5 -ls'
# Most frequent committers
alias gitcommiters="git ls-files | xargs -n1 git blame | ruby -n -e \"$_ =~ /^.*\((.*?)\s[\d]{4}/; puts $1.strip\" | sort -f | uniq -c | sort -n"
# I always forget the damned flags for this
alias untargz="tar zxf"
# Saving test output comes in handy
alias testme='dt=`date +%Y%m%d_%H_%M`; spec --color spec/**/*_spec.rb > spec_results_$dt &'
function tt () # tree top
{
cwd=$(pwd)
path_before_work=${cwd%/work/*}
path_after_work=${cwd#*/work/}
dir_within_work=${path_after_work%%/*}
echo $path_before_work/work/$dir_within_work
}
function cdtt
{
cd $(tt)
}
alias drivespeed='dd if=/dev/zero of=test.file bs=1024k count=128 && rm -f test.file'
alias diary="dt=`date +%Y%m%d_%H_%M`; encfs ~/.encrypted ~/encrypted; vim ~/encrypted/diary_${dt}; fusermount -u ~/encrypted"
function tj () # tmux join-pane vertical
{
tmux join-pane -s ${1}.0 -h
}
function tjh () # tmux join-pane horizontal
{
tmux join-pane -s ${1}.0
}
alias gcot='git-checkout-by-match'
alias gbd='git-delete-ticket-branch'
alias f='find . | grep'
alias remove-trailing-whitespace='perl -p -i -e "s/\s+\n$/\n/g" `find . -type f | grep -v .git`'
alias evilchrome='open -b com.google.chrome --args --disable-web-security'
alias vi='vim'
if command -v nvim >/dev/null 2>&1; then
alias vim="nvim"
fi
# need to forward ssh and gpg for this to work on codspaces
# RemoteForward /home/vscode/.gnupg/S.gpg-agent /Users/mmrobins/.gnupg/S.gpg-agent.extra
# ForwardAgent yes
function zshhistorybackup(){(
set -e
#set -x
# you'd better make sure this dir is backed up :-)
ZSH_BACKUP_DIR="$HOME/backup/zsh_history"
mkdir -p $ZSH_BACKUP_DIR
NAME=zsh_history_(`hostname`)_`date +'%Y%m%d'`.gz.gpg
# would be nice to not hard code the pgp key
gzip -c ~/.zsh_history | gpg --encrypt -r 96A9D440AC1A57C9 > $ZSH_BACKUP_DIR/$NAME
cd $ZSH_BACKUP_DIR
ln -fs $NAME zsh_history_(`hostname`)_latest.gz.gpg
rsync -avz ~/backup [email protected]:~
)}
function zshhistoryrestore(){(
set -e
#set -x
HOSTNAME=${1:=$(hostname)}
rsync -avz [email protected]:~/backup/zsh_history $HOME/tmp/
mkdir -p $HOME/tmp/zsh_history
UNESCAPED_BASENAME=zsh_history_($HOSTNAME)_latest
RESTORE_FILE=$HOME/tmp/zsh_history/$UNESCAPED_BASENAME.restored
gpg --decrypt $HOME/tmp/zsh_history/$UNESCAPED_BASENAME.gz.gpg | gunzip > $RESTORE_FILE
echo "restored to $RESTORE_FILE"
)}
# https://david-kerwick.github.io/2017-01-04-combining-zsh-history-files/
function zshcombinehistories(){(
set -e
#set -x
DEST_COMBINED=$HOME/tmp/zsh_history.combined
echo $1
echo $2
export LC_ALL=C # handle weird characters in history when awk and sort
cat $1 $2 > $DEST_COMBINED.unsorted
cat $DEST_COMBINED.unsorted \
| awk -v date="WILL_NOT_APPEAR$(date +"%s")" '{if (sub(/\\$/,date)) printf "%s", $0; else print $0}' \
| sort \
| awk -v date="WILL_NOT_APPEAR$(date +"%s")" '{gsub('date',"\\\n"); print $0}' \
| awk '!/^[[:space:]]*$/' \
> $DEST_COMBINED
echo "combined to $DEST_COMBINED"
)}
alias be='bundle exec'
function zh(){
ack "$1" ~/.zsh_history
}
# close, but not quite to get dates instead of timestamps
#zh "tree.*build" | sed "s/^: \([0-9]*\):.;\(.*\)$/`date -r \1 +%Y%m%d-%H:%M` \2/"
alias bi='(bundle check || bundle --local --jobs=4 || bundle --jobs=4)'
function vpn() {
if ! ifconfig | grep -q utun0
then
osascript <<-END_SCRIPT
tell application "System Events"
tell current location of network preferences
set VPNservice to service "Techops Tools VPN"
if exists VPNservice then connect VPNservice
end tell
end tell
END_SCRIPT
while ! ifconfig |grep -q utun0
do
sleep 2
done
fi
}
#alias ssh='vpn; command ssh'
alias wip='git add -A && git commit -m "wip"'
alias wtc='git add -A && git commit -m "`curl whatthecommit.com/index.txt`"'
# mavericks and el capitan
alias raccoondance='sudo launchctl stop com.apple.racoon; dscacheutil -flushcache; sudo killall -HUP mDNSResponder; sudo launchctl start com.apple.racoon'
# yosemite
#alias raccoondance='sudo launchctl stop com.apple.racoon; sudo discoveryutil mdnsflushcache; sudo discoveryutil udnsflushcaches; sudo launchctl start com.apple.racoon'
alias dobby='ssh -t [email protected] ssh '
alias dockerclean='docker rm $(docker ps -a -q); docker rmi $(docker images -q --filter "dangling=true")'
alias fixcamera='sudo killall VDCAssistant' # when hangouts just won't see the camera this fixes it
alias ssh-start='eval $(/usr/bin/keychain --eval --agents ssh -Q --quiet ~/.ssh/id_rsa)'
command -v pbcopy > /dev/null 2>&1 || alias pbcopy='xclip -selection clipboard'
command -v pbpaste > /dev/null 2>&1 || alias pbpaste='xclip -selection clipboard -o'
alias clegane='git diff `git merge-base origin/master HEAD` --name-only | xargs -I {} rubocop --auto-correct {}'
# kubernetes
alias k="kubectl"
alias kd="kubectl --context=dev"
alias kp="kubectl --context=prod"
# docker
alias dc="docker compose"
alias dcr="docker-compose run --platform linux/amd64"
# Quickly display / switch Kubernetes contexts
function kcsc
{
local context=${1}
if [[ -z "$context" ]]; then
kubectl config get-contexts
else
kubectl config use-context ${context}
fi
}
# Quickly display / switch Kubernetes namespaces
function kcns
{
local namespace=${1}
if [[ -z "$namespace" ]]; then
kubectl get ns
else
local context=$(kubectl config current-context)
echo "Setting context ${context} to namespace ${namespace}..."
kubectl config set-context ${context} --namespace ${namespace}
fi
}
function kcapacity {
for node in $(kubectl get no --no-headers | awk '$0 !~ /Disabled/ {print $1}'); do
echo -n "Node ${node} - "
kubectl describe no $node \
| grep -A4 'Allocated resources' \
| tail -n1 \
| awk '{print "CPU Requests " $1 " " $2 " Memory Requests: " $5 " " $6}'
done
}
alias dt="docker-compose run tests"
function kdpod
{
kd get pods -l deployment=$1 -o name | head -n 1 | sed -e 's/pods\///'
}
function kppod
{
kp get pods -l deployment=$1 -o name | head -n 1 | sed -e 's/pods\///'
}
alias tfm_remove_all_state="terraform state list | cut -f 1 -d '[' | xargs -L 0 terraform state rm"
alias tfm="terraform"
alias vf='vim $(fzf)'
alias gssh="gh codespace ssh --profile github-codespaces -c"
# send text to clipper like pbcopy
alias clip="socat - UNIX-CLIENT:$HOME/.clipper.sock"
alias rt="bin/rails test"
alias tree="tree --gitignore" # prevents node_modules and other such spammy dirs
alias tmux_colors='for i in {0..255}; do; printf "\x1b[38;5;${i}mcolour${i}\n"; done'
# first arg is purpose, if left off uses last purpose set
function cs_tmux_set_purpose
{
tmux set -g status-right-length 90
local cs_black="#[fg=brightwhite,bg=#363636]"
local cs_bright_black="#[fg=brightwhite,bg=brightblack]"
local cs_name=" $(echo ${CODESPACE_NAME##*-}) "
local cs_created="$(cat ~/.codespace_created_at) "
if [[ ! -z "$1" ]]; then
echo $1 > ~/.codespace_purpose
fi
local cs_purpose="$(cat ~/.codespace_purpose) "
tmux set-option -g status-right " $cs_black $cs_name $cs_bright_black $cs_created_at $cs_black $cs_purpose "
}
# https://stackoverflow.com/questions/5609192/how-to-set-up-tmux-so-that-it-starts-up-with-specified-windows-opened
# maybe use tmuxinator, but that seems overkill
function cs_tmux_start
{
tmux attach ||
tmux new-session \; \
send-keys 'vim' C-m \; \
split-window -h \; \
send-keys 'rg test_string_to_warm_disks' C-m \; \
send-keys 'git fetch' C-m \; \
split-window -v \; \
send-keys 'tmux set -g status-bg green' C-m \; \
send-keys 'cs_tmux_set_purpose' C-m \; \
new-window -n 'srv' \; \
send-keys 'script/server' C-m \; \
split-window -h \; \
send-keys 'bin/rails test test/api/versioning_test.rb' C-m
}
#function git_pr
#{
# pr_commit=$(git log --merges --ancestry-path --oneline $1..master | grep 'pull request' | tail -n1)
# pr_num=$(echo $pr_commit | awk '{print $5}' | cut -c2-)
# gh_pr_regex=':\w+\/\w+\.git'
# repo=$(git remote -v
# open https://github.com/github/github/pull/229148
#}
alias d=docker