-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bashrc
160 lines (141 loc) · 3.79 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
#-------------------
# Check whether the given command is available
#-------------------
_cmd_avail() { [ -n "$(type -p $1)" ]; }
#-------------------
# Global environment variables
#-------------------
# Python start-up script
export PYTHONSTARTUP=${HOME}/.python/startup.py
if _cmd_avail 'vim' ; then
export EDITOR='vim'
fi
#-------------------
# Local environment variables
#-------------------
# Make history a little more sane
HISTCONTROL=ignoreboth
# Prefer the local version of things (like Homebrew's nano on OS X)
PATH=/usr/local/bin:/usr/local/sbin:${PATH}
# Prefer user-local versions even more
if [ -d "${HOME}/bin" ]; then
PATH=${HOME}/bin:${PATH}
fi
#-------------------
# Aliases
#-------------------
# Simplify listing hidden files & dirs
alias l.="ls -pd .*"
if _cmd_avail 'netstat' && [ "$(uname -s)" != "Darwin" ]; then
if _cmd_avail 'watch'; then
alias ns="watch -n 0.1 netstat -tup"
else
alias ns="netstat -tupc"
fi
fi
# We never want vi if vim is available
if _cmd_avail 'vim' ; then
alias vi='vim'
fi
if _cmd_avail 'gitk' ; then
alias gbv='gitk --branches --remotes --simplify-by-decoration'
fi
if _cmd_avail 'hexdump' ; then
alias hd="hexdump -C"
fi
if _cmd_avail 'vagrant' ; then
alias v='vagrant'
alias vs='vagrant status | grep -v "not created"'
alias vssh='vagrant ssh'
fi
#-------------------
# Enable completion, if available
#-------------------
for f in /usr/local/etc/bash_completion \
/etc/bash_completion \
/etc/profile.d/bash_completion.sh
do
test -f "$f" && {
. "$f"
break
}
done
#-------------------
# Pull in other definitions.
#
# We want this after enabling completion, so
# each can add its own completion support
#-------------------
if [ -d "${HOME}/.bashrc.d" ]; then
for f in "${HOME}/.bashrc.d"/*.sh ; do
. "$f"
done
fi
#-------------------
# Prompt string
#-------------------
__virtual_env() {
[ -z "${VIRTUAL_ENV}" ] || echo -n "($(basename ${VIRTUAL_ENV})) "
}
__git_branch() {
if ! _cmd_avail git; then return; fi
if git branch >/dev/null 2>&1; then
# Ignore the dot-file repo, as that isn't something we usually care about
if [ "`git rev-parse --show-toplevel 2>/dev/null`" == "${HOME}" ]; then
return
fi
branch=`git branch | grep '^\*' | cut -c 3-`
if git status --porcelain 2>&1 | grep . >/dev/null 2>&1; then
echo -ne " \e[31m[${branch}*]"
else
echo -ne " \e[32m[${branch}]"
fi
fi
}
__svn_path() {
return # Disabled; don't do much in SVN these days
if ! _cmd_avail svn; then return; fi
if svn info >/dev/null 2>&1; then
wc_root=`pwd`
# For svn 1.7+, go up until we see a .svn directory
while [ ! -d "$wc_root/.svn" -a "$wc_root" != "/" ]; do
wc_root=`dirname "$wc_root"`
done
# For svn 1.6-, go up until the parent doesn't have a .svn directory
while [ -d "`basename "$wc_root"`/.svn" ]; do
wc_root=`dirname "$wc_root"`
done
repo_len=`svn info "$wc_root" 2>/dev/null | grep "^Repository Root:" | cut -c 18- | wc -c`
svn_path=`svn info "$wc_root" | grep "^URL:" | cut -c 7- | cut -c ${repo_len}-`
if [ `svn status "$wc_root" 2>/dev/null | wc -l` -eq 0 ]; then
echo " \e[32m[${svn_path:-/}]"
else
echo " \e[31m[${svn_path:-/}*]"
fi
fi
}
__abbr_path() {
script="import sys
l = sys.stdin.readline().split('/')
a = l[0:3] + ['...'] + l[-2:]
print('/'.join(min(l, a, key=len)))"
if _cmd_avail python; then
pwd | sed -e "s@^${HOME}@~@" | python -c "$script"
elif _cmd_avail python3; then
pwd | sed -e "s@^${HOME}@~@" | python3 -c "$script"
else
pwd | sed -e "s@^${HOME}@~@"
fi
}
__err_code() {
[ "$1" -eq 0 ] || echo -n "\e[31m[$1] "
}
__make_prompt() {
st=$?
PS1="\n$(__err_code $st)\e[36m[\D{%H:%M:%S}] \u@\h : $(__abbr_path)$(__git_branch)$(__svn_path)\e[0m\n$(__virtual_env)\$ "
}
PROMPT_COMMAND=__make_prompt
# Local config
if [ -f "${HOME}/.bashrc.local" ]; then
. "${HOME}/.bashrc.local"
fi