-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdot_bashrc
253 lines (209 loc) · 6.97 KB
/
dot_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
# shellcheck shell=bash
# If not running interactively, don't do anything
[[ ${-} != *i* ]] && return
# --- options
shopt -s cdable_vars
shopt -s checkhash
shopt -s checkjobs
shopt -s direxpand
shopt -s dirspell
shopt -s dotglob
shopt -s histappend
shopt -s nocaseglob
# --- colors and prompt
if [[ "${OSTYPE}" == "darwin"* ]]; then
[[ -f "/opt/local/share/git/contrib/completion/git-prompt.sh" ]] && \
source /opt/local/share/git/contrib/completion/git-prompt.sh
elif [[ "${OSTYPE}" == "linux-gnu"* ]]; then
[[ -f "/usr/share/git/completion/git-prompt.sh" ]] && \
source /usr/share/git/completion/git-prompt.sh
fi
GIT_PS1_SHOWDIRTYSTATE='1' # '*'=unstaged, '+'=staged
GIT_PS1_SHOWUPSTREAM='verbose' # 'u='=no difference, 'u+1'=ahead by 1 commit
GIT_PS1_STATESEPARATOR='' # no space between branch and index status
GIT_PS1_DESCRIBE_STYLE='describe'
function __prompt_cmd() {
local exit_status=${?}
# cache the value in a variable before using pringf to avoid delay
local GIT_PS1
GIT_PS1="$(__git_ps1)"
DIRTRIM_AWK=$(cat << 'EOF'
BEGIN { FS = OFS = "/" }
{
sub(ENVIRON["HOME"], "~");
if (length($0) > 35 && NF > 4)
print $1,$2,".." NF-4 "..",$(NF-1),$NF
else
print $0
}
EOF
)
DIRTRIM="$(printf "%b" "${PWD}" | awk "${DIRTRIM_AWK}")"
local GRAY="\e[0;37m"
local DARK_GRAY="\e[0;90m"
local RED="\e[0;31m"
local CYAN="\e[0;36m"
local PURPLE="\e[0;35m"
local NORMAL="\e[0;0m"
# set window title bar
if [ -z "${PROMPT_TITLE}" ]; then
# the var is empty
printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/\~}"
else
# the var isn't empty
printf "\033]0;%s\a" "${PROMPT_TITLE}"
fi
# empty line before the prompt
printf "\n"
if [ -n "${PROMPT_PRIVATE_SESSION}" ]; then
printf "%b" "${RED}private "
fi
# check for ssh session
if [[ -n "${SSH_CLIENT}" ]]; then
printf "%b" "${GRAY}${USER}@${HOSTNAME%%.*} "
fi
# if this is a subshell, print shell level
# ignore Tmux and lf levels
if [[ ${SHLVL} != 1 ]]; then
local desc
local lvl=${SHLVL}
# by default, Tmux does not allow you to run itself in Tmux session
# therefore, extra "if" statements are not required
if [[ ${TMUX} != "" ]]; then
desc="${desc}tmux+"
lvl=$((lvl-1))
fi
if [[ ${LF_LEVEL} != "" ]]; then
if [[ ${LF_LEVEL} == "1" ]]; then
desc="${desc}lf+"
else
desc="${desc}lf(${LF_LEVEL})+"
fi
lvl=$((lvl-LF_LEVEL))
fi
printf "%b" "${DARK_GRAY}${desc}${lvl} "
fi
# print current directory
printf "%b" "${CYAN}${DIRTRIM}"
# print git prompt
printf "%b\n" "${DARK_GRAY}${GIT_PS1}${NORMAL}"
# set prompt
if [[ ${exit_status} = 0 ]]; then
PS1="\[${PURPLE}\]❯ \[${NORMAL}\]"
else
PS1="\[${RED}\]❯ \[${NORMAL}\]"
fi
}
PROMPT_COMMAND="__prompt_cmd"
# --- aliases
if [[ "${OSTYPE}" == "darwin"* && ! -f /opt/local/libexec/gnubin/ls ]] ; then
alias ls="ls -G"
else
alias ls="ls --color=auto"
fi
alias la="ls -a"
alias ll="ls -al"
alias ldot="ls -ld .*"
alias cp="cp -i"
alias df="df -h"
alias du="du -h"
alias today="date +%Y-%m-%d"
alias week="date +%V"
alias myip="dig +short myip.opendns.com @resolver1.opendns.com"
alias localip="ipconfig getifaddr en0"
alias random-mac="openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'"
# Make colors in ripgrep as in The Silver Searcher
alias rg="rg --colors 'match:fg:black' --colors 'match:bg:yellow'\
--colors 'match:style:nobold' --colors 'path:fg:green'\
--colors 'path:style:bold' --colors 'line:fg:yellow'"
alias proj="source projects.sh"
alias yt480p="yt-dlp -f 'bestvideo[height<=480]+bestaudio'"
alias yt720p="yt-dlp -f 'bestvideo[height<=720]+bestaudio'"
alias yt1080p="yt-dlp -f 'bestvideo[height<=1080]+bestaudio'"
# darwin specific
if [[ "${OSTYPE}" == "darwin"* ]]; then
alias show="defaults write com.apple.finder AppleShowAllFiles -bool true && killall Finder"
alias hide="defaults write com.apple.finder AppleShowAllFiles -bool false && killall Finder"
alias vscode="/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code"
alias smerge="/Applications/Sublime\ Merge.app/Contents/SharedSupport/bin/smerge"
alias subl="/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl"
elif [[ "${OSTYPE}" == "linux-gnu"* ]]; then
if [[ "${DESKTOP_SESSION}" == "plasma" ]]; then
alias open="kioclient exec"
else
alias open="xdg-open"
fi
fi
# --- variables
# Most environment variables have been moved to ~/.profile
HISTCONTROL=ignoreboth:erasedups:ignoredups
HISTSIZE=100000
HISTFILESIZE=100000
# --- keybindings
set -o vi
if [[ "${OSTYPE}" == "darwin"* ]]; then
[[ -r "/opt/local/share/mcfly/mcfly.bash" ]] && \
source /opt/local/share/mcfly/mcfly.bash
elif [[ "${OSTYPE}" == "linux-gnu"* ]]; then
[[ -r "/usr/share/doc/mcfly/mcfly.bash" ]] && \
source /usr/share/doc/mcfly/mcfly.bash
fi
# --- functions
function __path_remove() {
PATH=$(echo -n "${PATH}" | awk -v RS=: -v ORS=: '$0 != "$1"' | sed 's/:$//');
export PATH
}
function __java_check_current_version() {
java -version 2>&1 | head -1 | cut -d'"' -f2 | sed '/^1\./s///' | cut -d'.' -f1
}
function java-set() {
__path_remove "${JAVA_HOME}" && __path_remove "${JAVA_BIN}"
JAVA_HOME=$(/usr/libexec/java_home -v "${1}")
export JAVA_HOME
JAVA_BIN=${JAVA_HOME}/bin
export JAVA_BIN
export PATH=${PATH}:${JAVA_BIN} && \
echo "The current Java version is $(__java_check_current_version)"
}
function git-latest-branch() {
local rev_list latest_tag
rev_list=$(git rev-list --tags --max-count=1)
latest_tag=$(git describe --tags "${rev_list}")
git fetch --tags
git checkout "${latest_tag}"
}
function title() {
PROMPT_TITLE="${1}"
}
function bash-private() {
PROMPT_PRIVATE_SESSION="TRUE" HISTFILE=/dev/null bash
}
# --- plugins and third party
# run direnv hook
if command -v direnv &> /dev/null; then
eval "$(direnv hook bash)"
fi
# source bash-completion
if [[ "${OSTYPE}" == "darwin"* ]]; then
[[ -f /opt/local/share/bash-completion/bash_completion ]] && \
source /opt/local/share/bash-completion/bash_completion
elif [[ "${OSTYPE}" == "linux-gnu"* ]]; then
[[ -f /usr/share/bash-completion/bash_completion ]] && \
source /usr/share/bash-completion/bash_completion
fi
# settings mcfly
export MCFLY_KEY_SCHEME=vim
export MCFLY_FUZZY=true
export MCFLY_RESULTS=50
# settings colors for less
export LESS_TERMCAP_mb=$'\e[1;32m'
export LESS_TERMCAP_md=$'\e[1;32m'
export LESS_TERMCAP_me=$'\e[0m'
export LESS_TERMCAP_se=$'\e[0m'
export LESS_TERMCAP_so=$'\e[01;33m'
export LESS_TERMCAP_ue=$'\e[0m'
export LESS_TERMCAP_us=$'\e[1;4;31m'
# speed-up fzf
export FZF_DEFAULT_COMMAND='fd --type f --color=never'
export FZF_ALT_C_COMMAND='fd --type d . --color=never'
# vim:ft=sh