-
Notifications
You must be signed in to change notification settings - Fork 4
/
.bash_profile
155 lines (133 loc) · 4.11 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
# ------ misc bash settings --------{{{
shopt -s histappend # append history lists of different sessions instead of overwriting
export BASH_SILENCE_DEPRECATION_WARNING=1 # silence macOS 10.15 bash deprecation warning
#}}}
# ------ source .shrc --------------{{{
if [ -f $HOME/.dotfiles/.shrc ]; then
source $HOME/.dotfiles/.shrc
else
echo "Error: Couldn't find ~/.dotfiles/.shrc"
fi
#}}}
# ------ inputrc settings ----------{{{
# don't ignore case when tab-completing
bind 'set completion-ignore-case off'
# add trailing '/' when tab-completing folders
bind 'set mark-directories on'
bind 'set mark-symlinked-directories on'
# unicode readline
bind 'set input-meta on'
bind 'set output-meta on'
bind 'set convert-meta off'
# word jumping with Shift-Ctrl-Left/Right in iTerm2
bind '"\e[1;6D": backward-word'
bind '"\e[1;6C": forward-word'
#}}}
# ------ custom bash prompt --------{{{
my_command_prompt(){
# Truncate the directory path if it exceeds a certain length
truncate_pwd
# Display git branch and dirty status
git_status_prompt
# Display name of activated venv
venv_status_prompt
# Define colors
local WHITE="\[\033[1;37m\]"
local GREEN="\[\033[0;32m\]"
local CYAN="\[\033[0;36m\]"
local GRAY="\[\033[0;37m\]"
local BLUE="\[\033[0;34m\]"
local MAGENTA="\[\033[0;35m\]"
local RED="\[\033[0;31m\]"
local YELLOW="\[\033[0;33m\]"
# Define root prompt
export SUDO_PS1="${RED}\u@\h:${MAGENTA}\w/ ${GRAY}"
USRNAME=`whoami`
if [ "$USRNAME" == "root" ]; then
export PS1="${RED}\u@\h:${MAGENTA}\w/ ${GRAY}"
else
export PS1="${GREEN}\h:${MAGENTA}\${NEW_PWD}/${YELLOW}\${GITSTAT}\${VENVSTAT} ${GRAY}"
fi
}
function truncate_pwd #{{{
{
local pwdmaxlen=25
local trunc_symbol=".."
local dir=${PWD##*/}
pwdmaxlen=$(( ( pwdmaxlen < ${#dir} ) ? ${#dir} : pwdmaxlen ))
NEW_PWD=${PWD/#$HOME/\~}
local pwdoffset=$(( ${#NEW_PWD} - pwdmaxlen ))
if [ ${pwdoffset} -gt "0" ]
then
NEW_PWD=${NEW_PWD:$pwdoffset:$pwdmaxlen}
NEW_PWD=${trunc_symbol}/${NEW_PWD#*/}
fi
}
#}}}
function git_status_prompt #{{{
{
local GITSTATUS=`git status 2>&1`
# Retrieve current branch
if [[ ${GITSTATUS} =~ On\ branch\ ([^[:space:]]+) ]]; then
GITBRANCH=${BASH_REMATCH[1]}
fi
if [[ ${GITSTATUS} =~ "ot a git repository" ]]; then
# No git repo
GITSTAT=""
elif [[ ${GITSTATUS} =~ "must be run in a work tree" ]]; then
# Bare git repo
GITSTAT=""
elif [[ ${GITSTATUS} =~ "working tree clean" ]]; then
# Clean repo
GITSTAT="[${GITBRANCH}]"
# Display only if not on master branch
test "$GITBRANCH" != master || GITSTAT=""
else
# Dirty repo
GITSTAT="[${GITBRANCH} ±]"
# Display only if not on master branch
test "$GITBRANCH" != master || GITSTAT="[±]"
fi
}
#}}}
function venv_status_prompt #{{{
{
local VENVNAME="${VIRTUAL_ENV##*/}"
if [[ -z $VENVNAME ]]; then
VENVSTAT=""
else
VENVSTAT="(${VENVNAME})"
fi
}
#}}}
PROMPT_COMMAND=my_command_prompt
#}}}
# ------ tab-completion hacks ------{{{
# auto-complete commands after sudo, man, mans
complete -cf sudo
complete -cf man
complete -cf mans
complete -cf manv
complete -cf mangv
# auto-complete like scp after custom gvimscp
complete -F _scp -o nospace gvimscp
# Bash_completion
if [ -r /opt/homebrew/etc/profile.d/bash_completion.sh ]; then
export BASH_COMPLETION_USER_DIR=~/.dotfiles/.bash-completion
. /opt/homebrew/etc/profile.d/bash_completion.sh
fi
# iTerm shell integration
#if [ -f ~/.dotfiles/Scripts/.iterm2_shell_integration.bash ]; then
#. ~/.dotfiles/Scripts/.iterm2_shell_integration.bash
#fi
# fzf keybindings
if [ -f $HOME/.dotfiles/.fzf.sh ]; then
source $HOME/.dotfiles/.fzf.sh
fi
# auto-complete remote hosts for ssh -> bash_completion
# Rem: does not work for ssh user@host; instead use ssh host -l user
#complete -W "$(echo `cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | sed -e s/,.*//g | uniq | grep -v "\["`;)" ssh
#}}}
# ------ Vim Modeline --------------{{{
# vim: set foldmethod=marker:
#}}}