-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.bashrc
388 lines (306 loc) · 10.4 KB
/
.bashrc
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
##
# ~/.bashrc -- startup file for bash (interactive non-login)
#
# author: Pat Gaffney <[email protected]>
#
# This file is loaded when invoking bash as an interactive shell
# that is *not* a login shell. Because of this behavior, this
# file is loaded by ~./bash_profile -- effectively making it the
# startup file for both login and non-login interactive shells.
#
#####################################################################
##
# Shell Options
#
# If any option has both a single character flag (`-x`) *and* a
# option name (`-o option-name`), the option-name is chosen for
# general readability.
#
# Any options that are turned on by *default* are also listed here
# so that they could be easily turned off in the future if needed.
#####################################################################
# Do not exit an interactive shell upon reading EOF.
set -o ignoreeof;
# Report the status of terminated background jobs immediately,
# rather than before printing the next primary prompt.
set -o notify;
##
# Shell Behavior
#####################################################################
# Executed a directory name as if it were an argument to cd.
shopt -s autocd
# Correct spelling errors in directory names given to cd.
shopt -s cdspell
# Check the hash table for a command name before searching $PATH.
shopt -s checkhash
# Check the window size (update $LINES and $COLUMNS)
# after each command.
shopt -s checkwinsize
# Save all lines of a multi-line command in the same history entry.
shopt -s cmdhist
# Correct spelling errors on directory names during word completion
# if the directory name initially supplied does not exist.
shopt -s dirspell
# Enable extended pattern matching features.
shopt -s extglob
# Enable `**` pattern in filename expansion to match all files,
# directories and subdirectories.
shopt -s globstar
# Append the history list to $HISTFILE instead of replacing it.
shopt -s histappend
# Save multi-line commands to the history with embedded newlines
# instead of semicolons -- requries cmdhist to be on.
shopt -s lithist
# Do not attempt completions on an empty line.
shopt -s no_empty_cmd_completion
# Case-insensitive filename matching in filename expansion.
shopt -s nocaseglob
# Treat unknown commands as aliases during completion,
# attempt completion from the resulting alias.
shopt -s progcomp_alias
# Make echo builtin expand backslash-escape-sequence.
shopt -s xpg_echo
##
# Environment Variables
#####################################################################
# Set the $PATH -- favor /usr/local/bin.
export PATH="/opt/homebrew/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/opt:$HOME/code/go/bin:$HOME/.bin:$HOME/.rd/bin"
# Set Sublime Text to be the default editor.
export EDITOR='subl --wait --stay'
# Ignore files with these suffixes when performing completion.
export FIGNORE='.o:.pyc:.git:cache'
# Ignore files that match these patterns when
# performing filename expansion.
export GLOBIGNORE='.DS_Store:*.o:*.pyc:.git:*cache'
# History file control:
# - ignorespace = don't save lines that begin with a space
# - ignoredups = don't save duplicate lines
export HISTCONTROL='ignorespace:ignoredups'
# Maximum number of lines/commands to save in the history file.
export HISTFILESIZE=1000000
export HISTSIZE=10000
# Ignore the following commands when appending to history.
HISTIGNORE='ls:ll:cd:pwd'
# Prefer US English and use UTF-8.
export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'
# Set colors for ls command.
# 1. directory: ex
# 2. symbolic link: fx
# 3. socket: gx
# 4. pipe: bx
# 5. executable: cx
# 6. block special: aH
# 7. character special: aA
# 8. executable with setuid bit set: cA
# 9. executable with setgid bit set: cH
# 10. directory writable to others, with sticky bit: eA
# 11. directory writable to others, without sticky bit: eH
export CLICOLOR=1
export LSCOLOR='exfxgxbxcxaHaAcAcHeAeH'
# Set up colors for man pages.
# Stolen from: https://russellparker.me/2018/02/23/adding-colors-to-man/
export MANROFFOPT='-c'
export LESS_TERMCAP_mb=$(tput bold; tput setaf 2)
export LESS_TERMCAP_md=$(tput bold; tput setaf 4)
export LESS_TERMCAP_me=$(tput sgr0)
export LESS_TERMCAP_so=$(tput bold; tput setaf 3; tput setab 1)
export LESS_TERMCAP_se=$(tput rmso; tput sgr0)
export LESS_TERMCAP_us=$(tput smul; tput bold; tput setaf 7)
export LESS_TERMCAP_ue=$(tput rmul; tput sgr0)
export LESS_TERMCAP_mr=$(tput rev)
export LESS_TERMCAP_mh=$(tput dim)
# Correctly set GPG TTY.
export GPG_TTY=$(tty)
# Turn off the homebrew hints.
export HOMEBREW_NO_ENV_HINTS='why'
##
# Python
#####################################################################
# Don't let virtualenv modify the bash prompt -- I will do this.
export VIRTUAL_ENV_DISABLE_PROMPT=1
##
# Go
#####################################################################
export GOPATH="$HOME/code/go"
# Print the outdated go packages. This is intended to work with
# go modules. It only prints patch and minor updates.
alias goout="go list -u -m all"
##
# Node
#####################################################################
# Print the top 15 largest NPM packages.
alias npmsize="du -sh node_modules/* | sort -h -r | head -n 15"
##
# Kubernetes
#####################################################################
# Decode and print a secret.
function k8secret {
kubectl get secret "$1" -o json | jq -r '.data | to_entries[] | "\(.key) = \(.value | @base64d)"'
}
# Set up color diffs.
export KUBECTL_EXTERNAL_DIFF="colordiff -N -u"
##
# AWS
#####################################################################
# Because, well shit.
# Props to @jskidis, for this stroke of genius.
function aws {
case $* in
redshit* )
shift 1;
command aws redshift "$@"
;;
* )
command aws "$@"
;;
esac
}
##
# Postgres
#####################################################################
alias pg_start="pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start"
alias pg_stop="pg_ctl -D /usr/local/var/postgres stop -s -m fast"
##
# Dates
#####################################################################
# Convert YYYY-MM-DD to YYYYDDD.
function todoy {
date -j -f '%Y-%m-%d' "$1" '+%Y%j'
}
# Convert YYYYDDD to YYYY-MM-DD.
function fromdoy {
date -j -f '%Y%j' "$1" '+%Y-%m-%d'
}
##
# Prompt
#####################################################################
# Color the Git branch name based on the status.
function prompt_git_dirty {
local status
status=$(git status 2> /dev/null)
local push='Your branch is ahead'
local dirty='added to commit'
local commit='Changes to be committed'
# First check if the repo is dirty.
if [[ $status =~ ${dirty} ]]; then echo $'\e[1;31m';
# Next check if changes have been staged.
elif [[ $status =~ ${commit} ]]; then echo $'\e[1;36m';
# Check if its ahead of remote.
elif [[ $status =~ ${push} ]]; then echo $'\e[1;33m';
# Default to clean.
else echo $'\e[1;32m';
fi
}
# Add the Git branch `:[<branch-name>]` to the prompt.
function prompt_git_branch {
IFS=$'\n'
local branches
branches=$(git branch --no-color 2> /dev/null)
local prefix='\* '
local string=''
for branch in $branches; do
if [[ ${branch} == ${prefix}* ]]; then
string+=':['
string+=$(prompt_git_dirty)
string+=${branch##$prefix}
string+=$'\e[0m'
string+=']'
break
fi
done
echo "$string"
unset IFS
}
# Add Kubernetes cluster name `:(<cluster-name>)` to prompt.
function prompt_k8s_cluster {
local cluster
local string
cluster=$(kubectl config current-context 2>/dev/null)
if [[ ${cluster} == "" || "$?" == "1" ]]; then
echo ""
else
string+=':('
string+=$'\e[1;33m'
string+="$cluster"
string+=$'\e[0m'
string+=')'
echo "$string"
fi
}
# Begin appending information to PS1
export PS1=''
# Add cyan command-number: '\#'
PS1+=$'\[\e[1;36m\]\#.\[\e[0m\] '
# Add bold username: '\u'
PS1+=$'\[\e[1m\]\u\[\e[0m\]'
# Add bold-blue hostname: '\h'
PS1+=$'\[\e[1;34m\]@\h\[\e[0m\]'
# Add current k8s cluster.
PS1+='$(prompt_k8s_cluster) '
# Add red working directory: '\w'
PS1+=$'in \[\e[1;31m\]\w\[\e[0m\]'
# Add git information
PS1+='$(prompt_git_branch) '
# Add magenta time: '\A'
PS1+=$'at \[\e[1;35m\]\A\[\e[0m\]\n'
# Add dollar-sign `$`
PS1+=$'\[\e[1;34m\]$ \[\e[0m\]'
# The number of trailing directory components to retain when adding
# the directory names to the prompt -- trailing directories are
# marked with an `...` ellipsis.
export PROMPT_DIRTRIM=3
##
# Aliases
#
# Set up simple aliases to enable default options for commands.
# For almost every purpose, shell functions are preferred over
# aliases, so some care has been taken to ensure that the alises
# defined here would *not* work better as functions.
#
#####################################################################
# Clear the screen.
alias k="clear"
# List {a}ll files with {h}uman-{s}ize output long.
# -A = List all entries except for `.` and `..`.
# -G = Enable colorized output.
# -h = Use unit suffixes for file size.
# -l = List in long format, omit group id.
# -P = List symbolic link, not its reference.
# -T = Display time information.
alias ll="ls -AGhlTP"
# Go back (1|2) director(y|ies).
alias ..="cd ../"
alias ...="cd ../../"
# Open current directory in Finder.app.
alias finder="open -a Finder ./"
# Put the computer to sleep.
alias nap="pmset sleepnow"
# Get the day of the year as in DDD.
alias doy='date +%j'
##
# Private Configurations
#####################################################################
[[ -s ~/.private ]] && source ~/.private
##
# Bash Completions
#####################################################################
# Set up bash_completion.
if type brew &>/dev/null; then
HOMEBREW_PREFIX="$(brew --prefix)"
# Use homebrew script to source all completions.
if [[ -r "${HOMEBREW_PREFIX}/etc/profile.d/bash_completion.sh" ]]; then
source "${HOMEBREW_PREFIX}/etc/profile.d/bash_completion.sh"
# Manually source all completions.
else
for COMPLETION in "${HOMEBREW_PREFIX}/etc/bash_completion.d/"*; do
[[ -r "${COMPLETION}" ]] && source "${COMPLETION}"
done
fi
fi
# Set up any custom completions.
if [ -d "$HOME/.completions" ]; then
for COMPLETION in "$HOME/.completions/"*; do
[[ -r "${COMPLETION}" ]] && source "${COMPLETION}"
done
fi