-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.zshrc
544 lines (443 loc) · 15 KB
/
.zshrc
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
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
###################################################
#
# Environment variables
#
###################################################
export PATH="$PATH:$HOME/bin:$HOME/local/bin:$HOME/.local/bin"
# Homebrew installed locally due to meta devserver conflicts
export PATH="$PATH:$HOME/homebrew/bin:$HOME/homebrew/sbin"
# Add adb to path
export PATH="$PATH:/Users/$USER/Library/Android/sdk/platform-tools/"
export EDITOR=/usr/bin/vim
export VISUAL=/usr/bin/vim
export HISTSIZE=130000
export HISTFILESIZE=-1
export PYTHONSTARTUP="$HOME/.pythonrc"
export CTRL_SRC="$HOME/fbsource/fbcode/frl/ctrl/src2-main"
###################################################
#
# Util functions
#
###################################################
# Check for the availability of a command.
# Usage: has-command <command_name>
function has-command() {
if [ "$#" -ne 1 ]; then
echo "Usage: ${0} <command_name>"
return
fi
cmd="${1}"
command -v ${cmd} > /dev/null 2>&1
}
# Search and replace using rg and sed. Supports searching by regex.
# Usage: rgr <search_pattern> <replacement_text>
function rgr() {
if [ "$#" -ne 2 ]; then
echo "Usage: ${0} <search_pattern> <replacement_text>"
return
fi
pattern="${1}"
replacement="${2}"
# Search and replace inplace, backing up original files
# with the extension .rgr_backup
BACKUP_EXT=.rgr_backup
rg "${pattern}" -l | xargs sed -i"${BACKUP_EXT}" "s/${pattern}/${replacement}/g"
# Delete backups. We could use fd here to list backup files, but
# sticking to rg for platform compatibility.
rg --files -g "*${BACKUP_EXT}" | xargs rm
}
# Search and delete lines containing regex pattern using rg and sed.
# Usage: rgd <search_pattern>
function rgd() {
if [ "$#" -ne 1 ]; then
echo "Usage: ${0} <search_pattern>"
return
fi
pattern="${1}"
# Search and delete lines with matches inplace, backing up original files
# with the extension .rgr_backup
BACKUP_EXT=.rgd_backup
rg "${pattern}" -l | xargs sed -i"${BACKUP_EXT}" "/${pattern}/d"
# Delete backups. We could use fd here to list backup files, but
# sticking to rg for platform compatibility.
rg --files -g "*${BACKUP_EXT}" | xargs rm
}
# Fix hotspot not working due to carriers throttling tethered traffic.
# Usage: hotspot <on|off>
function hotspot() {
if [ "$#" -ne 1 ]; then
echo "Usage: ${0} <on|off>"
return
fi
on_or_off="${1}"
if [ "${on_or_off}" = "on" ]; then
# Increase TTL from 64 to 65 to add an extra hop. With the default TTL of 64
# packet originating from tethered laptop will have a TTL of 63 when it gets
# to the phone. Carriers use this to distinguish phone traffic from hotspot
# and throttle. Bump up TTL to 65 to get around this.
sudo sysctl net.inet.ip.ttl=65
# Also disable ipv6 because the setting for TTL on IPv6 (known as HLIM)
# is apparently not honored by Mac OS.
sudo networksetup -setv6off "Wi-Fi"
echo "Hotspot mode enabled"
else
# Revert hotspot unfix and turn back ipv6 on when on wifi
sudo sysctl net.inet.ip.ttl=64
sudo networksetup -setv6automatic "Wi-Fi"
echo "Back on wifi mode"
fi
}
# Enable `fwdproxy-config` aliases when on devserver.
# Usage: fwdproxy <on|off> <all|git|curl|wget|...>
function fwdproxy() {
# Only proceed if fwdproxy-config command is found. Otherwise,
# not on devserver and no need for proxy.
if ! has-command fwdproxy-config; then
return
fi
if [ "$#" -ne 2 ]; then
echo "Usage: ${0} <on|off> <command>"
return
fi
on_or_off="${1}"
cmd="${2}"
CMDS=(git hg curl wget ssh java)
if [[ "${cmd}" == "all" ]]; then
cmds=("${CMDS[@]}")
elif [[ "${CMDS[*]} " =~ "${cmd}" ]]; then
cmds=("${cmd}")
else
echo "Unknown command ${cmd}, must be either 'all' or one of [${CMDS}]"
return
fi
if [ "${on_or_off}" = "on" ]; then
# Set aliases with fwdproxy-config
for cmd in "${cmds[@]}"; do
alias "${cmd}=${cmd} $(fwdproxy-config ${cmd})"
done
else
# Unset aliases
for cmd in "${cmds[@]}"; do
unalias "${cmd}"
done
fi
}
# Encrypt or decrypt a file with openssl
OPENSSL_CMD="openssl enc -aes-256-cbc -md sha512 -pbkdf2 -iter 100000"
function enc() {
if [ "$#" -lt 1 ]; then
echo "Usage: ${0} <in_file> [<out_file>]"
return
fi
in_file="${1}"
out_file="${2}"
if [ -z "${out_file}" ]; then
out_file="${in_file}.enc"
fi
cmd="${OPENSSL_CMD} -in ${in_file} -out ${out_file}"
echo "${cmd}"
eval "${cmd}"
}
function dec() {
if [ "$#" -ne 2 ]; then
echo "Usage: ${0} <in_file> <out_file>"
return
fi
in_file="${1}"
out_file="${2}"
cmd="${OPENSSL_CMD} -in ${in_file} -out ${out_file} -d"
echo "${cmd}"
eval "${cmd}"
}
# fzf + git: https://polothy.github.io/post/2019-08-19-fzf-git-checkout/
function in_git_repo() {
git rev-parse HEAD > /dev/null 2>&1
}
function fzf-git-branch() {
# Return early if not in a git repo
in_git_repo || return
git branch --all --color=always |
grep -v HEAD |
fzf --preview 'git log -n 50 --color=always --date=short --pretty="format:%C(auto)%cd %h%d %s" $(sed "s/.* //" <<< {})' |
sed "s/.* //"
}
fzf-git-checkout() {
# Return early if not in a git repo
in_git_repo || return
# If branch name is provided, no need to invoke `fzf-git-branch`
if ! [ $# -eq 0 ]; then
git checkout "$@"
return
fi
local branch
branch=$(fzf-git-branch)
if [[ "$branch" = "" ]]; then
echo "No branch selected"
return
fi
# If branch name starts with 'remotes/' then it is a remote branch. By
# using --track and a remote branch name, it is the same as:
# git checkout -b branchName --track origin/branchName
if [[ "$branch" = 'remotes/'* ]]; then
git checkout --track $branch
else
git checkout $branch;
fi
}
# Build ctrl bento kernel (from https://fburl.com/wiki/jexy3vq9)
function build-bento-kernel () {
# Install aienv first if unavaiable
has-command aienv || feature install aienv --global
buck1 clean
buck2 clean
aienv run -i //frl/ctrl/aienv:arvr_ctrl_integrated -m bento.link -- -i SELF
sudo systemctl restart bento@$USER.service
fixmyserver --yes
}
###################################################
#
# Oh My Zsh
#
###################################################
# Path to your oh-my-zsh installation.
export ZSH="$HOME/.oh-my-zsh"
# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"
# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"
# Uncomment the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"
# Uncomment the following line to automatically update without prompting.
# DISABLE_UPDATE_PROMPT="true"
# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13
# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS=true
# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"
# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"
# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"
# Uncomment the following line to display red dots whilst waiting for completion.
# COMPLETION_WAITING_DOTS="true"
# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"
# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"
# Which plugins would you like to load?
# Standard plugins can be found in ~/.oh-my-zsh/plugins/*
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=() # Ignore, taken over by antigen below
source $ZSH/oh-my-zsh.sh
###################################################
#
# Antigen
#
###################################################
# Enable fwdproxy-config aliases for all commands to connect to the internet
# when on devserver so we can fetch the necessary plugins at startup.
# This is reverted at the end of this file.
fwdproxy on all
source ~/.antigen.zsh
antigen use oh-my-zsh
# Theme
# antigen theme jreese
#
# The following two must be enabled together:
# antigen bundle mafredri/zsh-async
# antigen bundle sindresorhus/pure
#
antigen theme romkatv/powerlevel10k
# Plugins from oh-my-zsh (https://github.com/ohmyzsh/ohmyzsh/wiki/Plugins)
antigen bundle git
antigen bundle colored-man-pages
antigen bundle web-search
antigen bundle vi-mode
# Custom/third-party plugins
antigen bundle zsh-users/zsh-autosuggestions
antigen bundle zsh-users/zsh-syntax-highlighting
# Plugin settings
# zsh-autosuggestions (prefer tab completion suggestions first and then history)
# export ZSH_AUTOSUGGEST_STRATEGY=(completion history)
# Apply
antigen apply
# All done. Unset fwdproxy-config aliases if set.
fwdproxy off all
###################################################
#
# Plugin settings
#
###################################################
# antigen theme romkatv/powerlevel10k.
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[ -f ~/.p10k.zsh ] && source ~/.p10k.zsh
# Disable gitstatusd for powerlevel10k since it causes issues on fb dev
export POWERLEVEL9K_DISABLE_GITSTATUS=true
# fzf
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
# fzf command order of preference based on availability: fd > ripgrep > find (default)
if has-command fd; then
export FZF_DEFAULT_COMMAND='fd --type file --color=always'
elif has-command rg; then
export FZF_DEFAULT_COMMAND='rg --files --color=always'
fi
export FZF_CTRL_T_COMMAND="${FZF_DEFAULT_COMMAND}"
# CTRL+d and CTRL+u for navigating fzf results, color/display/preview settings, etc.
export FZF_DEFAULT_OPTS="
--bind=ctrl-d:down,ctrl-u:up,?:toggle-preview \
--height=40% \
--layout=reverse \
--ansi \
--preview '(
[[ -f {} ]] && (bat --style=numbers --color=always --line-range :500 {} || cat {})) \
|| ([[ -d {} ]] && (tree -C {} | less)) \
|| echo {} 2> /dev/null | head -200' \
--preview-window=right,60% \
"
# bat, if installed, is used by fzf to prettify previews
export BAT_CONFIG_PATH="$HOME/.bat.conf"
###################################################
#
# Aliases
#
###################################################
alias woman='man' # For Silky
alias sudo='sudo '
has-command nvim && alias v='nvim' || alias v='vim'
alias l='ls -lh'
alias la='ls -lha'
alias py='python'
# cd
alias wrk='cd ~/work'
alias lcl='cd ~/local'
alias jk='cd ~/junk'
alias dbx='cd ~/Dropbox\ \(Personal\)'
alias zk='cd ~/Dropbox\ \(Personal\)/zettelkasten'
alias gtd='cd ~/Dropbox\ \(Personal\)/zettelkasten/gtd'
alias docs='cd ~/Dropbox\ \(Personal\)/docs'
alias fbs='cd ~/fbsource'
alias fbc='cd ~/fbsource/fbcode'
alias src='cd ${CTRL_SRC}'
alias nb='cd ~/work/notebooks'
# git
alias gd='git diff'
alias gc='git commit -a -v'
alias ga='git commit -a -v --amend'
alias gaa='git add --all'
alias gaad='git add --all --dry-run'
alias gs='git status'
alias gst='git stash'
alias gsh='git show'
alias gl='git log --graph --decorate --oneline'
alias gpr='git pull --rebase'
alias gpro='git pull --rebase origin'
alias gprom='git pull --rebase origin master'
alias gprum='git pull --rebase upstream master'
alias gf='git fetch'
alias gfo='git fetch origin'
alias gcb='git checkout -b'
alias gcm='git checkout $(git_main_branch)'
has-command fzf && alias gb='fzf-git-branch' || alias gb='git branch -vv'
has-command fzf && alias gco='fzf-git-checkout' || alias gco='git checkout'
# hg
alias hd='hg diff'
alias hc='hg commit'
alias ha='hg commit --amend'
alias hb='hg bookmarks'
alias hsh='hg show'
function hp() {
hg pull
hg update $1
hg rebase -d master
}
# tmux
alias tmx='tmux attach -d || tmux' # Unique view: Attach to existing, detach old view
alias tmxa='tmux attach || tmux' # Multiple views: Attach to existing, keep both views
# conda
alias ca='conda activate'
alias cda='conda deactivate'
alias cenv='conda env'
# buck
alias bb='buck build'
alias bt='buck test'
alias br='buck run'
alias bbmd='bb @mode/mac/dev-release'
alias bbld='bb @mode/linux/dev-release'
alias btmd='bt @mode/mac/dev-release'
alias btld='bt @mode/linux/dev-release'
alias brmd='br @mode/mac/dev-release'
alias brld='br @mode/linux/dev-release'
# aws
alias s3ls='aws s3 ls --summarize --human-readable --recursive'
alias s3cp='aws s3 cp'
alias s3cpr='aws s3 cp --recursive'
alias s3put='aws s3api put-object' # s3put --bucket <bucket> --key <path>
# cluster
alias cl='ctrl-launch'
alias cj='ctrl-jupyter'
alias cjup='cj up --log-level INFO'
alias cjdown='cj down'
alias cjauth='cj auth --overwrite'
alias cldel='cl manage delete --name '
alias kubectl='kubectl --namespace=jobs'
alias kubepods='kubectl get pods'
alias kubelogs='kubectl logs'
alias argo='argo --namespace=jobs'
# Run a command in the container such as `kubeexec <pod_name> -- ps aux`
alias kubeexec='kubectl exec -ti'
# Plugins and extras
has-command bat && alias cat='bat' # s/cat/bat if bat is installed
has-command exa && alias ls='exa' # s/ls/exa if exa is installed
has-command exa && alias lt='exa --tree'
alias goog='google' # zsh web-search plugin
# Alias to mosh into a jumphost and then ssh as mosh doesn't support ProxyJump
alias moshjmp='mosh -6 jmp -n ssh'
# meta dev
alias devc='dev connect --mosh'
alias devl='dev list'
alias devr='dev release'
###################################################
#
# Coda with conda
#
###################################################
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/viswanath/miniconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/Users/viswanath/miniconda3/etc/profile.d/conda.sh" ]; then
. "/Users/viswanath/miniconda3/etc/profile.d/conda.sh"
else
export PATH="/Users/viswanath/miniconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
# Auto-activate ctrldev env after conda init
has-command conda && conda deactivate && conda activate ctrldev
###################################################
#
# Source additional scripts
#
###################################################
# FB-specific stuff
[ -f ~/.fb.zshrc ] && source ~/.fb.zshrc