-
Notifications
You must be signed in to change notification settings - Fork 2
/
bash_profile
286 lines (245 loc) · 8.46 KB
/
bash_profile
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
#!/usr/bin/env bash
# Terminate for non-interactive sessions.
case $- in
*i*) ;;
*) return ;; esac
#
# Core Utility Functions
#
text.substr() {
echo "${1:$2:$3}"
}
os.devnull() {
"$@" 2>/dev/null >/dev/null
}
os.setenv() {
export "${1}"="${2}"
}
os.getenv() {
env | grep "^$1=" | cut -d'=' -f2
}
#
# System Detection & Path Management
#
sys.platform() {
uname -s | tr "[:upper:]" "[:lower:]"
}
os.platform.is_darwin() {
[[ $(sys.platform) == darwin ]]
}
os.platform.is_linux() {
[[ $(sys.platform) == linux ]]
}
os.path.exists() {
[[ -f "$1" ]]
}
sys.path.contains() {
os.devnull command -v "$1"
}
sys.path.prepend() {
[[ -d "$1" ]] && PATH="$1:${PATH}"
}
#
# Shell Management Functions
#
shell.setopt() {
sys.path.contains "shopt" && shopt -s "$1"
}
shell.import() {
# shellcheck source=/dev/null
[[ -f "$1" ]] && source "$1"
}
shell.eval() {
eval "$($1)"
}
shell.setup_prompt() {
os.setenv "PS1" "\h \[\e[1;32m\]\$(shell.iterm2_style_path)\[\e[0m\] [\A] > "
}
shell.iterm2_style_path() {
# Mimic iTerm2's absolute path abbreviation. For example,
# "/usr/local/bin" abbreviates to "/u/l/bin".
IFS="/"
read -ra relpath <<<"$(dirs +0)"
buffer=""
dirname=$((${#relpath[*]} - 1))
for folder_name in "${relpath[@]}"; do
if [[ $folder_name != "${relpath[$dirname]}" ]]; then
buffer+="$(text.substr "$folder_name" 0 1)/"
fi
done
echo -n "$buffer${folder_name}"
}
#
# SSH Agent Management
#
ssh_agent.active_sessions() {
pgrep -u "$USER" ssh-agent
}
ssh_agent.start() {
[[ -z "$1" ]] && return 1
os.devnull rm "$1"
ssh-agent | sed "s/^echo/#echo/" >"$1"
chmod 600 "$1"
shell.import "$1"
}
ssh_agent.init() {
local env="$1"
[[ -z "$env" ]] && return 1
touch "$env"
if ! os.devnull ssh_agent.active_sessions; then
ssh_agent.start "$env"
else
shell.import "$env"
fi
if ! os.devnull ssh-add -l; then
os.devnull ssh-add -k
fi
}
#
# Homebrew Functions
#
brew.prefix() {
if os.path.exists /opt/homebrew/bin/brew; then
echo /opt/homebrew
elif os.path.exists /usr/local/bin/brew; then
echo /usr/local
fi
}
os.setenv "TERM" "xterm-256color"
os.setenv "GPG_TTY" "$(tty)"
os.setenv "DOTFILES_VERSION" "4.2.0"
os.setenv "DIRENV_LOG_FORMAT" ""
# Python Shell Environment
os.setenv "POETRY_VIRTUALENVS_PATH" "$HOME/.virtualenvs"
os.setenv "PYENV_ROOT" "$HOME/.pyenv"
os.setenv "PYTHONDONTWRITEBYTECODE" true
os.setenv "WORKON_HOME" "$HOME/.virtualenvs"
# Homebrew Shell Environment
os.setenv "BREW_PREFIX" "$(brew.prefix)"
os.setenv "HOMEBREW_CASKROOM" "$BREW_PREFIX/Caskroom"
os.setenv "HOMEBREW_CELLAR" "$BREW_PREFIX/Cellar"
os.setenv "HOMEBREW_REPOSITORY" "$BREW_PREFIX/Homebrew"
os.setenv "HOMEBREW_SHELLENV_PREFIX" "$BREW_PREFIX"
os.setenv "MANPATH" "$BREW_PREFIX/share/man${MANPATH+:$MANPATH}:"
os.setenv "INFOPATH" "$BREW_PREFIX/share/info:${INFOPATH:-}"
os.setenv "HOMEBREW_NO_ENV_HINTS" 1
os.setenv "ARCHFLAGS" "-arch $(uname -m)"
os.setenv "BASH_SILENCE_DEPRECATION_WARNING" 1
os.setenv "HSTR_CONFIG" "hicolor"
# Node.js Environment
os.setenv "NODE_REPL_HISTORY" "$HOME/.node_history" # persistent REPL history
os.setenv "NODE_REPL_HISTORY_SIZE" 32768 # allow 32³ entries
os.setenv "NODE_REPL_MODE" "sloppy" # use non-strict mode code
os.setenv "NVM_DIR" "$HOME/.nvm"
# Shell Session Command History
os.setenv "HISTCONTROL" "ignoredups:erasedups:ignoreboth"
os.setenv "HISTFILESIZE" -1
os.setenv "HISTSIZE" -1
os.setenv "SHELL_SESSION_HISTORY" 0 # Save & reload history after each command
os.setenv "PROMPT_COMMAND" "history -a; history -n; $PROMPT_COMMAND"
os.setenv "MANPAGER" "less"
os.setenv "VISUAL" "nano"
os.setenv "EDITOR" "nano"
os.setenv "HSTR_TIOCSTI" "y"
# Set various Bash options.
shell.setopt "cdspell" # directory name auto-correct during cd.
shell.setopt "checkwinsize" # Update window size after each command.
shell.setopt "cmdhist" # Save multi-line commands as one.
shell.setopt "histappend" # Append command history instead of
# clobbering.
shell.setopt "hostcomplete" # tab-complete hostnames.
shell.setopt "no_empty_cmd_completion" # Do not suggest empty commands
# during tab completion.
shell.setopt "nocaseglob" # Case-insensitive path expansion.
shell.setopt "cdspell" # Automatic spell-correct during
# tab completion.
shell.setopt "histverify" # !$ does not execute automatically.
shell.setopt "histappend" # Append command history instead of clobbering.
# Untracked Shell Scripts (DO NOT SORT)
sys.path.prepend "$HOME/.bin"
# Prefer GNU Utilities instead of FeeeBSD When Available
sys.path.prepend "$BREW_PREFIX/opt/coreutils/libexec/gnubin"
sys.path.prepend "$BREW_PREFIX/opt/findutils/libexec/gnubin"
sys.path.prepend "$BREW_PREFIX/opt/gnu-getopt/libexec/gnubin"
sys.path.prepend "$BREW_PREFIX/opt/gnu-sed/libexec/gnubin"
sys.path.prepend "$BREW_PREFIX/opt/gnu-tar/libexec/gnubin"
sys.path.prepend "$BREW_PREFIX/opt/gnu-time/libexec/gnubin"
sys.path.prepend "$BREW_PREFIX/opt/gnu-which/libexec/gnubin"
sys.path.prepend "$BREW_PREFIX/opt/grep/libexec/gnubin"
sys.path.prepend "$BREW_PREFIX/opt/whois/bin"
sys.path.prepend "$BREW_PREFIX/opt/icu4c/bin"
sys.path.prepend "$BREW_PREFIX/opt/icu4c/sbin"
sys.path.prepend "$BREW_PREFIX/opt/openssl/bin"
sys.path.prepend "$BREW_PREFIX/opt/openjdk/bin"
sys.path.prepend "$BREW_PREFIX/opt/e2fsprogs/bin"
sys.path.prepend "$BREW_PREFIX/opt/e2fsprogs/sbin"
sys.path.prepend "$BREW_PREFIX/bin"
sys.path.prepend "$BREW_PREFIX/sbin"
sys.path.prepend "$BREW_PREFIX/share/google-cloud-sdk/path.bash.inc"
sys.path.prepend "$HOME/.local/bin"
sys.path.prepend "$HOME/.docker/bin"
sys.path.prepend "$HOME/.pyenv/bin"
shell.import "$BREW_PREFIX/bin/virtualenvwrapper_lazy.sh"
shell.import "$HOME/.config/op/plugins.sh"
shell.import "$BREW_PREFIX/opt/bash-completion/etc/bash_completion"
shell.import "$BREW_PREFIX/share/google-cloud-sdk/completion.bash.inc"
shell.import "$BREW_PREFIX/opt/git-extras/share/git-extras/git-extras-completion.sh"
shell.import "$BREW_PREFIX/opt/nvm/nvm.sh"
shell.import "$HOME/.iterm2_shell_integration.bash"
shell.import "$BREW_CASKROOM/google-cloud-sdk/latest/google-cloud-sdk/completion.bash.inc"
shell.import "$HOME/.docker/init-bash.sh"
shell.import "$HOME/.config/op/plugins.sh"
# Untracked Private Overrides (DO NOT SORT)
shell.import "$HOME/.bash_profile.local"
# Use "most" for Manpages when available, otherwise use "less".
sys.path.contains "most" && os.setenv "MANPAGER" "most"
# Use "vscode" for text editing when available, otherwise use "nano".
sys.path.contains "code" && os.setenv "VISUAL" "cursor"
sys.path.contains "code" && os.setenv "EDITOR" "cursor"
sys.path.contains "ngrok" && shell.eval "ngrok completion"
sys.path.contains "aws" && shell.eval "complete -C aws_completer aws"
sys.path.contains "direnv" && shell.eval "direnv hook bash"
sys.path.contains "pyenv" && shell.eval "pyenv init --path"
sys.path.contains "pyenv" && shell.eval "pyenv init -"
sys.path.contains "pyenv" && shell.eval "pyenv virtualenv-init -"
sys.path.contains "rbenv" && shell.eval "rbenv init -"
sys.path.contains "dircolors" && shell.eval "dircolors -b $HOME/.dircolors"
# sys.path.contains "conda" && shell.eval "conda shell.bash hook"
complete -cf sudo # enable sudo tab-complete
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias .....="cd ../../../.."
alias ~="cd ~/Desktop"
alias ~~="cd ~/"
alias -- -="cd -"
alias r="cd ~/Repos"
alias c="clear"
alias code="code-insiders"
alias g="git"
alias cp="cp -i"
alias l="ls"
alias sl="ls"
alias la="ll -la"
alias grep="grep --color=auto"
alias fgrep="fgrep --color=auto"
alias egrep="egrep --color=auto"
alias rsync="rsync -v -P"
alias sudo="sudo "
alias ga="git add"
alias gd="git diff"
alias gs="git status"
alias map="xargs -n1"
alias hh="hstr"
# Create alias to open cwd in Finder.
os.platform.is_darwin && alias o="open ./"
# Create alias to cd to current working Finder directory.
os.platform.is_darwin && alias f='cd "$(eval fpwd)" || exit 0'
os.platform.is_linux || sys.path.contains "gls" && alias ls="ls --color=auto -gXF"
os.platform.is_linux || sys.path.contains "gls" && alias ll="ls --color=auto -algX"
sys.path.contains "rlwrap" && alias node="env NODE_NO_READLINE=1 rlwrap node"
sys.path.contains "bat" && alias cat="bat --style=\"plain\" --paging never"
sys.path.contains "network" && complete -W "$(network listcommands)" "network"
sys.path.contains "dotfiles" && complete -W "$(dotfiles -listcommands)" "dotfiles"
shell.setup_prompt
ssh_agent.init "$HOME/.ssh-agent.env"