-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_bash
More file actions
223 lines (172 loc) · 5.5 KB
/
base_bash
File metadata and controls
223 lines (172 loc) · 5.5 KB
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
# bashrc base
# locale settings
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
# utility function
_dir_exist() {
if [[ -d "$1" ]]; then
return 0 # return true (exit status 0)
else
return 1
fi
}
_file_exist() {
if [[ -f "$1" ]]; then
return 0 # return true (exit status 0)
else
return 1
fi
}
_have() { type "$1" &>/dev/null; }
_source_if() { [[ -r "$1" ]] && source "$1"; }
# usually for mac usage
_dot_if() { [[ -r "$1" ]] && . "$1"; }
# -------------------- prompt styling --------------------
PROMPT_AT=@
__ps1() {
# list of colors
local r='\[\e[32m\]' h='\[\e[34m\]' \
u='\[\e[33m\]' w='\[\e[35m\]' \
b='\[\e[36m\]' x='\[\e[0m\]' y='\[\e[37m\]'
local P='$' dir=$PWD T B
# dir="${dir##*/}"
# timestamp format
T="[\D{%H.%M%z}]"
# directory path format
[[ $EUID == 0 ]] && P='#' && u=$r && p=$u # root
[[ $PWD = / ]] && dir=/
[[ $PWD = "$HOME" ]] && dir='~'
# git branch format
B=$(git branch --show-current 2>/dev/null)
[[ $B == master || $B == main ]] && b="$r"
[[ -n "$B" ]] && B="$y+$b$B"
PS1="$T $u\u$y$PROMPT_AT$h\h$y:$w$dir $B\n$h$P$x "
}
# append rather than overwrite
shopt -s histappend
# attempts to save all lines of a multiple-line command in the same history entry
shopt -s cmdhist
# with cmdhist, saved with embedded newlines rather than semicolon separators
shopt -s lithist
HISTCONTROL=ignoreboth
HISTSIZE=10000
HISTFILESIZE=20000
HISTTIMEFORMAT="%y/%m/%d %T "
HISTIGNORE="history:ls:l:pwd:exit:"
if [[ ${BASH_VERSION:0:1} -gt 5 || ${BASH_VERSION:0:1} -ge 5 && ${BASH_VERSION:2:1} -ge 1 ]]; then
PROMPT_COMMAND=("history -a" "history -c" "history -r" "__ps1")
else
PROMPT_COMMAND="history -a; history -c; history -r; __ps1"
fi
# --------------------------------------------------------
# alias & export
_have brew && brew_etc="$(brew --prefix)/etc" && [[ -r "${brew_etc}/profile.d/bash_completion.sh" ]] && . "${brew_etc}/profile.d/bash_completion.sh"
_have vim && export EDITOR="vim"
_have vim && _have nvim && alias vim="nvim"
_have nvim && export EDITOR="nvim"
## tools
# TODO: add checking if mac
_dir_exist "/opt/homebrew/bin" && export PATH="$PATH:/opt/homebrew/bin"
if _have nordvpn; then
alias nvp="nordvpn connect"
alias nvpus="nordvpn connect united_states"
alias nvpd="nordvpn disconnect"
fi
if _have pyenv; then
eval "$(pyenv init -)"
alias python="$(pyenv which python)"
alias pip="$(pyenv which pip)"
fi
if _have volta; then
export VOLTA_HOME="$HOME/.volta"
export PATH="$PATH:$VOLTA_HOME/bin"
fi
if _have go; then
export GOPATH=$HOME/go
export PATH="$PATH:$GOPATH/bin"
fi
_have fvm && export PATH="$PATH:$HOME/fvm/default/bin"
_have maestro && export PATH=$PATH:$HOME/.maestro/bin
_dot_if "/opt/homebrew/etc/profile.d/z.sh"
_dot_if "/usr/share/z/z.sh"
_have fzf && eval "$(fzf --bash)"
_dot_if "/opt/homebrew/opt/fzf/shell/key-bindings.bash"
# local config
# naming derived from brlntrc, shorten to brc
_source_if "$HOME/.brc"
# ----------------------- completion ---------------------
_dot_if "/usr/local/etc/profile.d/bash_completion.sh"
# TODO: SOMEHOW BROKEN
# _dot_if "/opt/homebrew/etc/profile.d/bash_completion.sh"
_have z && . <(z completion bash)
# --------------------------------------------------------
# git alias
# copied from oh-my-zsh plugin.
# https://github.com/ohmyzsh/ohmyzsh/blob/master/plugins/git/git.plugin.zsh
__git_prompt_git() {
GIT_OPTIONAL_LOCKS=0 command git "$@"
}
git_current_branch() {
local ref
ref=$(__git_prompt_git symbolic-ref --quiet HEAD 2> /dev/null)
local ret=$?
if [[ $ret != 0 ]]; then
[[ $ret == 128 ]] && return # no git repo.
ref=$(__git_prompt_git rev-parse --short HEAD 2> /dev/null) || return
fi
echo ${ref#refs/heads/}
}
alias gs='git status'
alias gco='git checkout'
alias gcb='git checkout -b'
alias gc!='git commit --verbose --amend'
alias gc='git commit --verbose'
alias gfa='git fetch --all --tags --prune --jobs=10'
alias ggpull='git pull origin "$(git_current_branch)"'
alias gpsupf='git push --set-upstream origin $(git_current_branch) --force-with-lease --force-if-includes'
alias gpsup='git push --set-upstream origin $(git_current_branch)'
function gr() {
if git rev-parse --git-dir > /dev/null 2>&1; then
open "$(git remote get-url origin | sed 's/:/\//; s/^git@/https:\/\//')"
else
echo "error: current directory is not in a git repository."
fi
}
if _have gh; then
alias grr='gh pr view --web'
fi
function gwa() {
local branch="$1"
local flattened="${branch//\//-}"
git worktree add -b "$branch" "worktree/$flattened"
}
function gwr() {
git worktree remove -f "$1"
}
function gwar() {
local branch="$1"
local flattened="${branch//\//-}"
git worktree add --track -b "$branch" "worktree/$flattened" origin/$branch
}
# kubectl alias
# copied from oh-my-zsh plugin
# https://github.com/ohmyzsh/ohmyzsh/blob/master/plugins/kubectl/kubectl.plugin.zsh
if _have kubectl; then
alias k=kubectl
source <(kubectl completion bash)
complete -o default -F __start_kubectl k
fi
if _file_exist "/opt/homebrew/bin/terragrunt"; then
complete -C /opt/homebrew/bin/terragrunt terragrunt
fi
if _file_exist "$HOME/vpn/start.sh"; then
alias vpns=$HOME/vpn/start.sh
fi
if _file_exist "$HOME/vpn/stop.sh"; then
alias vpnk=$HOME/vpn/start.sh
fi
if _file_exist "$HOME/vpn/status.sh"; then
alias vpnstat=$HOME/vpn/status.sh
fi
_dot_if "$HOME/.local/bin/env"
_dir_exist "$HOME/.local/bin" && export PATH="$PATH:$HOME/.local/bin"