-
Notifications
You must be signed in to change notification settings - Fork 2
/
.zshrc
504 lines (412 loc) · 16.4 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
#!/usr/bin/env zsh
# Above hashbang set for shellcheck. This file should be sourced.
# Zsh profiler
if [[ "$ZPROF" = true ]]; then
zmodload zsh/zprof
fi
# If not in tmux, start it
if [[ -z ${TMUX+X}${ZSH_SCRIPT+X}${ZSH_EXECUTION_STRING+X} ]]; then
exec tmux
fi
# 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
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
# Path to your oh-my-zsh installation.
export ZSH=~/.oh-my-zsh
# Set name of the theme to load. Optionally, if you set this to "random"
# it'll load a random theme each time that oh-my-zsh is loaded.
# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
ZSH_THEME="powerlevel10k/powerlevel10k"
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
# Some user-specific settings selected before loading the config
# Perform git branch name substitution
P10K_BRANCH_SUBST_STRING=npendleton
P10K_BRANCH_SUBST_REPLACE=👴
[[ -f ~/.p10k.zsh ]] && source ~/.p10k.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 change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13
# 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.
# The optional three formats: "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# HIST_STAMPS="mm/dd/yyyy"
# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder
# Disable checking for insecure completions, to speed up shell load
ZSH_DISABLE_COMPFIX=true
# Load completions
fpath+=~/.zfunc
# History configuration
# set an explicit history file, to prevent it getting clobbered. if the
# synced history file is present, use it, otherwise use the local history file.
if [ -f ~/dev/github/zsh-history/.zsh_history.noah ]; then
HISTFILE=~/dev/github/zsh-history/.zsh_history.noah
else
HISTFILE=~/.zsh_history.noah
fi
HISTSIZE=10000000 # large history? unlimited doesn't seem to work correctly
SAVEHIST=$HISTSIZE
setopt HIST_IGNORE_DUPS # Do not record an entry that was just recorded again.
setopt HIST_IGNORE_ALL_DUPS # Delete old recorded entry if new entry is a duplicate.
# setopt HIST_FIND_NO_DUPS # Do not display a line previously found.
setopt HIST_IGNORE_SPACE # Do not record an entry starting with a space.
setopt HIST_SAVE_NO_DUPS # Do not write duplicate entries in the history file.
setopt HIST_REDUCE_BLANKS # Remove superfluous blanks before recording entry.
# Antigen managed plugins
# see https://github.com/zsh-users/antigen/wiki/Commands
source ~/antigen.zsh
antigen use oh-my-zsh
# oh-my-zsh included bundles
antigen bundle rust
antigen bundle colored-man-pages
antigen bundle common-aliases
antigen bundle git
antigen bundle wd
# bundles outside of oh-my-zsh
# antigen only supports specifying tag or branch, not sha. so hope this doesn't
# break >_<
# shas listed just in case i need to manually touch it up
antigen bundle agkozak/zsh-z #99f6ee91187ad6469b2cc3858f5a60c68286a1a5
antigen bundle zsh-users/zsh-autosuggestions #d43c309f888153d6c46d8b6a3a0186f4148680fd
antigen theme romkatv/powerlevel10k #1777bd41a68c99963e009c55a0f500396e485d07
antigen apply
# Disable auto-escaping urls in wget/curl. This actually doesn't work to disable
# that behavior, unfortunately 😩. Leaving it for reference
zstyle ':urlglobber' url-other-schema
# Autoload Zsh functions. Just enabling zcalc for now, the others may cause
# overreliance on zsh!
# autoload -Uz age
# autoload -Uz zargs
autoload -Uz zcalc
# autoload -Uz zmv
# Some extra completion settings. Disabled for now until I test for performance
# impact
# this group from https://github.com/ehynds/dotfiles/blob/master/.zshrc
# zstyle ':completion:*' accept-exact '*(N)'
# zstyle ':completion:*' use-cache on
# zstyle ':completion:*' cache-path ~/.zsh/cache
# next group from https://unix.stackexchange.com/a/214699
# # Do menu-driven completion.
# zstyle ':completion:*' menu select
# Color completion for some things; match ls colors
# http://linuxshellaccount.blogspot.com/2008/12/color-completion-using-zsh-modules-on.html
eval "$(dircolors -b)"
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
# # formatting and messages
# # http://www.masterzen.fr/2009/04/19/in-love-with-zsh-part-one/
# zstyle ':completion:*' verbose yes
# zstyle ':completion:*:descriptions' format "$fg[yellow]%B--- %d%b"
# zstyle ':completion:*:messages' format '%d'
# zstyle ':completion:*:warnings' format "$fg[red]No matches for:$reset_color %d"
# zstyle ':completion:*:corrections' format '%B%d (errors: %e)%b'
# zstyle ':completion:*' group-name ''
# export MANPATH="/usr/local/man:$MANPATH"
# You may need to manually set your language environment
# export LANG=en_US.UTF-8
# Compilation flags
# export ARCHFLAGS="-arch x86_64"
# ssh
# export SSH_KEY_PATH="~/.ssh/rsa_id"
# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
# Git aliases
alias gsc='git clean -dxf && git submodule foreach --recursive "git clean -dxf"'
alias gscf='git clean -dxff && git submodule foreach --recursive "git clean -dxff"'
alias gcln='git clean -dxf'
alias gsu='git submodule update --jobs 6 --init --recursive'
alias gsd='git submodule deinit -f .'
alias git-nuke='rm -rf * && git reset --hard HEAD && gsu'
alias gaf='git commit -a --amend --no-edit'
alias glp='git log --pretty=format:"%C(yellow)%h %Cblue%>(12)%ai %Cgreen%<(7)%aN %Cred%D %Creset%s"'
alias dec2hex='printf "%x\n"'
alias hex2dec='printf "%u\n"'
alias gddir='git difftool --dir-diff'
alias gdm='git diff $(git merge-base HEAD origin/master)'
alias gdd='git diff $(git merge-base HEAD origin/develop)'
alias gddm='git difftool --dir-diff $(git merge-base HEAD origin/master)'
alias gddd='git difftool --dir-diff $(git merge-base HEAD origin/develop)'
alias gst='git status -sb'
alias gac='git add -A && git commit -v'
alias gt='git tree' # https://github.com/knugie/git-status-tree#install
alias gdd='git diff @{u}'
alias gddo='git difftool --dir-diff @{u}'
alias gro='git reset @{u}'
alias gnp='git --no-pager'
# 'git reset date'... set date of top commit to current date. use a single variable
# in a subshell to avoid committer/author date not exactly matching
alias grd='(GIT_AUTHOR_DATE="$(date -R)"; GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"; git commit --amend --date "$GIT_COMMITTER_DATE" --no-edit)'
alias gbn='git rev-parse --abbrev-ref HEAD'
alias gsha='git rev-parse HEAD'
alias gct='git checkout trunk'
alias gcmn='git checkout main'
alias ghprdesc='gh pr edit --title "$(git log -n1 --format="%s")" --body "$(git log -n1 --format="%b")"'
# run 'gh pr create --fill'
alias ghprfill='gh pr create --fill'
# get the gh pr url for the current branch
alias ghprurl='gh pr view --json url | jq -r .url'
# open pr in browser
alias ghpropen='gh pr view --web'
# Other aliases
# tar args are too hard to remember, cheat
alias targz="tar -czvf"
# requires pigz, but man is it faster
alias tarpigz="tar -I pigz -cvf"
# always use system clipboard with xclip yolo
alias xclip="xclip -selection c"
# print ansi 256 colors
alias show-colors='for i in {0..255}; do printf "\x1b[38;5;${i}mcolor%-5i\x1b[0m" $i ; if ! (( ($i + 1 ) % 8 )); then echo ; fi ; done'
# emoji picker, uses https://github.com/noahp/emoji-fzf
alias emoj="emoji-fzf preview | fzf --preview 'emoji-fzf get --name {1}' | cut -d \" \" -f 1 | emoji-fzf get | xclip"
# no safe cp for me
unalias rm cp mv
# pipe data to gnuplot
alias gplot='gnuplot -e "set terminal dumb; plot '"'"'-'"'"' notitle"'
alias gplotp='gnuplot -e "plot '"'"'-'"'"' notitle" -persist'
# lazgit shortcut
alias lg=lazygit
# quick serial port
alias sp='tio $(fd . /dev/serial/by-id/ | head -n 1)'
# Custom functions
# Profile zsh startup
function profzsh() {
shell=${1-$SHELL}
ZPROF=true $shell -i -c exit
}
# git plot lines-of-code
function git-plot-loc() {
# plot the lines of code in a file per commit
git log --reverse --oneline -- $1 | cut -d " " -f 1 | xargs -I {} sh -c "git show {}:$1 | wc -l" | gplot
}
# git chery-pit (remove commit)
function git-cherry-pit() {
# remove a commit forever, goodbye (well ok it's still in the reflog if you have regrets)
git rebase -r --onto $1^ $1
}
# reset timestamps of commits
function git-rebase-time() {
local datenow="$(date -R)";
git rebase -x "GIT_AUTHOR_DATE=\"$datenow\" GIT_COMMITTER_DATE=\"$datenow\" \
git commit --amend --reset-author --no-edit -CHEAD" "$@"
}
# fuzzy search git log
function git-select() {
git log --oneline "$@" | fzf --preview 'git show $(echo {} | cut -d " " -f 1)' | cut -d " " -f 1
}
# git-size-packed
# from https://stackoverflow.com/a/42544963
function git-size-packed() {
git rev-list --objects --all \
| git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' \
| sed -n 's/^blob //p' \
| sort --numeric-sort --key=2 \
| cut -c 1-12,41- \
| numfmt --field=2 --to=iec-i --suffix=B --padding=7 --round=nearest
}
# git lines per person
function git-stats() {
git ls-files $1 | grep -v "\.csv\|\.bin\|\.so\|\.dll" | xargs -n1 git blame -w | perl -n -e '/^.*?\((.*?)\s+[\d]{4}/; print $1,"\n"' | sort -f | uniq -c | sort -nr
}
# which tags contain a particular committish?
function git-tags-contain() {
local _hash="$1"
git tag --contains $_hash
}
# ratio of commit contents to total commit size
function git-commit-message-ratio() {
local _commitish=${1:-HEAD}
local total_size=$(git cat-file -s $_commitish)
local message_size=$(git log $_commitish -1 --format="%B" | wc -c)
local contents_size=$(($total_size - $message_size))
local ratio=$(echo "scale=3; $contents_size / $total_size" | bc)
echo "total size: $total_size"
echo "message size: $message_size"
echo "contents size: $contents_size"
echo " ratio: $contents_size / $total_size = $ratio"
}
# print an svg piechart to stdout
# requires pygal (pip install pygal)
# ex: piechart "My Chart" "cat 1" 1.25 "cat 2" 3.56 "cat 3" 2.34 > /tmp/out.svg && firefox /tmp/out.svg
function piechart() {
python -c "import sys, pygal; pie_chart = pygal.Pie(); \
pie_chart.title = sys.argv[1]; \
slices = zip(*[iter(sys.argv[2:])]*2); \
map(lambda a: pie_chart.add(a[0],float(a[1])), slices); \
print pie_chart.render()" "$@"
}
# log_2 distribution of file sizes in cwd
function filesizes() {
find . -type f -print0 | \
xargs -0 ls -l | \
awk '{size[int(log($5)/log(2))]++}END{for (i in size) printf("%10d %3d\n", 2^i, size[i])}' | \
sort -n
}
# time function
function bash-time() {
for i in {1..${2:-255}}; do
bash -c "time $1";
done 2>&1 | rg "^real"
}
# haste client
# Either set env variable HASTE_SERVER to your server base url, eg:
# HASTE_SERVER=https://hastebin.com
# or
# Put a file ~/.hastebin with the url.
# Returns url of paste. TODO doesn't return non-zero on failure
# Inspired by https://github.com/seejohnrun/haste-client#lightweight-alternative
function hastebin() {
a=$(cat);
if [ -z "$HASTE_SERVER" ]; then
if [ -f ~/.hastebin ]; then
local HASTE_SERVER=$(cat ~/.hastebin)
else
local HASTE_SERVER='https://hastebin.com'
fi
fi
curl -X POST -s -d "$a" $HASTE_SERVER/documents | awk -v HASTE_SERVER=$HASTE_SERVER -F '"' '{print HASTE_SERVER "/" $4}';
}
# Hex dump to binary python oneliner
# usage: hexdump-to-bin abcd0123 > out.bin
function hexdump-to-bin {
python -c "import sys; sys.stdout.write(sys.argv[1].decode('hex'))" "$1"
}
# weather, from https://wttr.in/:bash.function
# see also: http://wttr.in/:help
function wttr {
local request="wttr.in/${1-$1}"
# if $COLUMNS is less than 125, use the compact form
[ "$COLUMNS" -lt 125 ] && request+='?n'
curl -H "Accept-Language: ${LANG%_*}" --compressed "$request"
}
# gnu build id pretty print from elf section
# note: 'file' command also prints it fwiw
# note: 'readelf' can also dump it with -x .note.gnu.build-id
function gnuid() {
readelf -n "$1" | grep 'Build ID:' | awk '{ print $3 }'
}
# download urls. optionally append second arg to end of urls.
function get-urls() {
# echo to split words
for i in $(echo $1); do curl -J -O $i"$2"; done
}
# check files in the directory tree, printing any with mismatched executable
# flag vs hashbang line
function check-hashbang() {
# burtally slow with gnu find -_-
HASHBANG_BUT_NOT_X=$(fd -t f --exec bash -c "if ((head -n1 {} | rg '#!' > /dev/null) && !(test -x {}) ); then echo {}; fi")
if [ -n "$HASHBANG_BUT_NOT_X" ]; then
echo "HASHBANG_BUT_NOT_X"
echo "$HASHBANG_BUT_NOT_X"
fi
X_BUT_NOT_HASHBANG=$(fd -t f --exec bash -c "if (test -x {} && !(head -n1 {} | rg '#!' > /dev/null)); then echo {}; fi")
if [ -n "$X_BUT_NOT_HASHBANG" ]; then
echo "X_BUT_NOT_HASHBANG"
echo "$X_BUT_NOT_HASHBANG"
fi
}
function in-this-house() {
echo "┏┓
┃┃╱╲ in
┃╱╱╲╲ this
╱╱╭╮╲╲house
▔▏┗┛▕▔ we
╱▔▔▔▔▔▔▔▔▔▔╲"
echo $*
echo "╱╱┏┳┓╭╮┏┳┓ ╲╲
▔▏┗┻┛┃┃┗┻┛▕▔"
}
function ghrbm() {( set -e # Fail early
local DEFAULT_BRANCH_=$(gh api repos/{owner}/{repo} --jq ".default_branch")
git fetch origin
git rebase origin/${DEFAULT_BRANCH_} "$@"
)}
function git-long-commit-messages() {
git log --pretty='%H' | xargs -I {} bash -c 'echo $(git log {} -n1 --pretty="format:%B" | wc --bytes) $(git log {} -n1 --pretty="format:%h %cI %an %s")' | sort --key=1 --reverse --numeric-sort
}
# disable python venv before activating tmux
alias tmux='[ -n "$VIRTUAL_ENV" ] && deactivate; tmux'
# arcanist path
export PATH=$PATH:~/arcinstall/arcanist/bin
# add ccache
export PATH=/usr/lib/ccache:$PATH
# include ~/.local/bin
export PATH=$PATH:~/.local/bin
# Preferred editor for local and remote sessions
if [[ -n $SSH_CONNECTION ]]; then
export EDITOR='vim'
else
# alias vim to nvim
export EDITOR='nvim'
fi
alias vim=${EDITOR}
alias v=${EDITOR}
# extremely sad, meld is not compatible with ubuntu 23.10's version
# of gtk+?
# UPDATE now working, but keep this around just in case
# alias meld='flatpak run org.gnome.meld'
# If available, make use of the bfs binary and search directories
# breadth-first instead of the default depth-first.
#
# Find bfs here: https://github.com/tavianator/bfs
if [ -x "$(command -v bfs)" ]; then
export FZF_ALT_C_COMMAND="bfs -type d -nohidden 2> /dev/null"
fi
# go bins
[ -d ~/go/bin ] && export PATH=$PATH:~/go/bin
# make npm global install to user path
export NPM_CONFIG_PREFIX=~/.npm-global
export PATH=$PATH:$NPM_CONFIG_PREFIX/bin
# enable sccache for rust
if type sccache > /dev/null; then
export RUSTC_WRAPPER=sccache
fi
# add fzf
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
# Source invoke completion
if [ -f ~/.invoke-completion.zsh ]; then
source ~/.invoke-completion.zsh
fi
# Direnv stuff
if type direnv > /dev/null; then
export DIRENV_LOG_FORMAT= # disable logging
eval "$(direnv hook zsh)"
fi
# Local config, if present
if [ -f ~/.zshrc_local ]; then
source ~/.zshrc_local
fi
# venv launch
if [ -f ~/.virtualenvs/default/bin/activate ]; then
source ~/.virtualenvs/default/bin/activate
fi
if [[ "$ZPROF" = true ]]; then
zprof
fi