-
Notifications
You must be signed in to change notification settings - Fork 0
/
.zshrc
240 lines (197 loc) · 6.5 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
# OhMyZsh configuation
export ZSH="$(echo ~)/.oh-my-zsh"
# ZSH_THEME="blinks-time"
ZSH_THEME="blinks"
HIST_STAMPS="dd.mm.yyyy"
COMPLETION_WAITING_DOTS="true"
# add plugins to ~/.oh-my-zsh/custom/plugins/
plugins=(git)
source $ZSH/oh-my-zsh.sh
# initialize homebrew
eval "$(/opt/homebrew/bin/brew shellenv)"
# make sure keys are added to the agent
if ! ssh-add -l | grep -q "[email protected]"; then
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
fi
if ! ssh-add -l | grep -q "[email protected]"; then
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_pers
fi
# initialize fzf
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
# User configuration
setopt histignorealldups
unsetopt nomatch
## Autocomplete
autoload -Uz compinit
compinit
zstyle ':completion:*' auto-description 'specify: %d'
zstyle ':completion:*' completer _expand _complete _correct _approximate
## Paths 'n Flags
export USER_BIN=~/bin
export PATH=${USER_BIN}:$PATH
export PATH="/usr/local/sbin:$PATH"
export PATH="$(brew --prefix)/opt/libpq/bin:$PATH"
export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
export CPPFLAGS="-I$(brew --prefix)/opt/libpq/include"
export LDFLAGS="-L$(brew --prefix)/opt/libpq/lib"
export PKG_CONFIG_PATH="$(brew --prefix)/opt/libpq/lib/pkgconfig"
## Other Variables
export LANG=en_US.UTF-8
export RIPGREP_CONFIG_PATH=~/.ripgreprc
export NVM_DIR="$HOME/.nvm"
## Preferred editor for local and remote sessions
if [[ -n $SSH_CONNECTION ]]; then
export EDITOR='vim'
elif [[ -n $CODESPACES ]]; then
export EDITOR='vim.tiny'
else
export EDITOR='nvim'
fi
## Aliases
# alias glog='git log --graph --oneline --decorate'
alias gpuo='git push -u origin $(current_branch)' # also gpsup
alias be='bundle exec'
alias rake='noglob rake' # required for certain strap tasks
alias linecount='tee >(wc -l | xargs echo)' # for piping ripgrep
alias caff='caffdown 36000'
alias baudit='bundle exec bundle-audit update && bundle exec bundle-audit check'
alias vim='nvim'
function n() {
nvim $1
}
function gcam() {
git commit -am $1
}
function git-log-clean() {
local log=$(git --no-pager log --pretty="%s. %b" [email protected] --since=$1 --until=$2 --all --no-merges)
if [[ -n "$log" ]]; then
echo "$log" | awk '!/^index on/' | awk '!x[$0]++' | sed 's/\*/\'$'\n\*/g' | sed '/^$/d' | sed '1!G;h;$!d'
else
echo "No commits found"
fi
}
function git-echo-copy() {
local yesterday=$(git-log-clean $1 $2)
echo "$yesterday"
if [[ "$yesterday" != "No commits found" ]]; then
git-log-clean $1 $2 | pbcopy
fi
}
function gitToday() { git-echo-copy midnight }
function gitYesterday() { git-echo-copy yesterday.midnight midnight }
function timestamp () {
timestamp=$(date +'%Y%m%d%H%M%S')
echo $timestamp
echo $timestamp | pbcopy
}
## Functions
function rtest () {
if ! [[ -z "$2" ]]; then
ruby -I test $1 -n "/$2/"
else
ruby -I test $1
fi
}
function runtest () {
if ! [[ -z "$2" ]]; then
bin/run ruby -I test $1 -n "/$2/"
else
bin/run ruby -I test $1
fi
}
# get running pods whose name includes a substring
# param1: namespace
# param2: pod name substring
function kpods () {
kubectl get pods -n $1 | rg -oP "$2\S*(?=\s+\d+\/\d+\s+Running)"
}
# log into a pod of the given name
# param1: namespace
# param2: pod name
function kshell () {
ContainerType=$(echo "$2" | rg -oP ".*(?=(-[^a-z][a-z0-9]*){2})")
kubectl -n $1 -c "$ContainerType" exec -it "$2" -- /bin/bash
}
# log into the first running pod whose name includes a substring
# param1: namespace
# param2: pod name substring
function kshellfirst () {
ContainerName=$(kpods $1 $2 | head -n1)
ContainerType=$(echo "$ContainerName" | rg -oP ".*(?=(-[^a-z][a-z0-9]*){2})")
kubectl -n $1 -c "$ContainerType" exec -it "$ContainerName" -- /bin/bash
}
# find filenames that look like arg
function rgg() {
rg --iglob "*$1*" -g '!/Library/' --files
}
# match literal string for arg
# useful for characters that bash wants to interpret
function rgl() {
rg -F $1 $2
}
# match a string in files AND filenames
function rga() {
( rg $1; rgg $1 )
}
# find and replace. leave second arg blank for find-and-remove.
# supports filenames with whitespace.
function rgr() {
rg $1 --files-with-matches -0 | xargs -0 sed -i '' "s/$1/$2/g"
}
# count occurrences of arg
function rgcount() {
rg $1 | tee >(wc -l | xargs echo)
}
# search for an element by attribute key and/or value
function rgelement() {
rg --multiline --multiline-dotall "<$1[^<]*$2[^<]*>"
}
# The following function runs the caffeinate command for a given number of seconds,
# and then displays a countdown for that amount of time.
# It hides the curser while the countdown is running, and then displays a message
# when the countdown is complete, and restores the cursor visibility.
function caffdown() {
caffeinate -dimsut $1 &
local caffeinate_pid=$!
tput civis
trap "local stop_script=1; kill $caffeinate_pid; tput cnorm; echo \"\n caffdown cancelled!\"" SIGINT SIGTERM EXIT
for i in $(seq $1 -1 1); do
local hours=$((i/3600))
local minutes=$(( (i%3600)/60 ))
local seconds=$(( (i%60) ))
printf "%02d:%02d:%02d\033[0K\r" $hours $minutes $seconds
if [[ $stop_script ]]; then
break
fi
sleep 1
done
tput cnorm
trap - SIGINT SIGTERM EXIT
}
function profanno() {
local filepath="$1"
local filename=$(basename "$filepath")
be stackprof tmp/stackprof.dump --file "$filepath" > "tmp/$filename"
}
# retrieve and set environment variables in the MacOS Keychain
function keychain-environment-variable () {
security find-generic-password -w -a ${USER} -D "environment variable" -s "${1}"
}
function set-keychain-environment-variable () {
[ -n "$1" ] || print "Missing environment variable name"
read "?Enter Value for ${1}: " secret
( [ -n "$1" ] && [ -n "$secret" ] ) || return 1
security add-generic-password -U -a ${USER} -D "environment variable" -s "${1}" -w "${secret}"
}
# Automatically set keychain-stored environment variables in the current shell
if command -v security >/dev/null 2>&1; then
# export GITHUB_USER=$(keychain-environment-variable GITHUB_USER);
# export GITHUB_TOKEN=$(keychain-environment-variable GITHUB_TOKEN);
fi
## Last Call
if [ "$(uname -s)" = "Darwin" ]; then
eval "$(rbenv init -)"
[ -s "$(brew --prefix)/opt/nvm/nvm.sh" ] && \. "$(brew --prefix)/opt/nvm/nvm.sh" # This loads nvm
[ -s "$(brew --prefix)/opt/nvm/etc/bash_completion.d/nvm" ] && \. "$(brew --prefix)/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion
fi
export PATH="$PATH:/Users/lukeabel/Repos/devops_utilities"