aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc
blob: 564735473fbaa9fecd3c181fd835a7a6d161eba6 (plain) (blame)
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
# Don't do anything if not running interactively.
[[ -z "$PS1" ]] && return

# Use vi as my text editor.
export EDITOR=vi

# None of this UTF8 drawing characters nonsense.
export NCURSES_NO_UTF8_ACS=1

# My GPG key ID for convenience.
export GPGKEY=255AAC13

# Keep plenty of history.
unset HISTFILESIZE
HISTSIZE=1000000

# Ignore duplicate commands and whitespace in history.
HISTCONTROL=ignoreboth

# Keep the times of the commands in history.
HISTTIMEFORMAT='%F %T  '

# Don't check for mail all the time, it's irritating.
unset MAILCHECK

# Autocorrect fudged paths in cd calls.
shopt -s cdspell &>/dev/null

# Update columns and rows if window size changes.
shopt -s checkwinsize &>/dev/null

# Put multi-line commands onto one line of history.
shopt -s cmdhist &>/dev/null

# Autocorrect fudged paths during completion.
shopt -s dirspell &>/dev/null

# Include dotfiles in pattern matching.
shopt -s dotglob &>/dev/null

# Enable advanced pattern matching.
shopt -s extglob &>/dev/null

# Enable double-starring paths.
shopt -s globstar &>/dev/null

# Append rather than overwrite Bash history.
shopt -s histappend &>/dev/null

# Never beep at me.
hash setterm &>/dev/null && setterm -bfreq 0

# Turn off annoying and useless flow control keys.
hash stty &>/dev/null && stty -ixon

# Use completion, if available.
[[ -e /etc/bash_completion ]] && source /etc/bash_completion

# SSH agent setup, if available.
[[ -e "${HOME}/.ssh/agent" ]] && source "${HOME}/.ssh/agent"

# Add various paths if they exist and aren't already in here.
DIRS="${HOME}/bin
      /usr/local/apache/bin
      /usr/local/mysql/bin
      /usr/local/nagios/bin
      /usr/local/pgsql/bin"
for DIR in $DIRS; do
    if [[ -d "$DIR" ]] && [[ ":${PATH}:" != *":${DIR}:"* ]]; then
        if [[ -n "$PATH" ]]; then
            PATH="${DIR}:${PATH}"
        else
            PATH=$DIR
        fi
    fi
done

# If we're using an xterm, force 256 colors.
case "$TERM" in
    xterm*) TERM=xterm-256color;;
esac

# If we're a screen terminal within a 256 color outer, force a 256 color
# screen terminal too. This deals with a tmux race condition bug.
case "$CONTAINING_TERM" in
    *256color)
        TERM=screen-256color
        unset CONTAINING_TERM
        ;;
esac

# Figure out how many colors we have now.
hash tput && COLORS=$(tput colors)

# Save some color codes based on our colour space.
if [[ $COLORS -ge 256 ]]; then
    COLOR_ROOT='\[\e[38;5;9m\]'
    COLOR_SUDO='\[\e[38;5;11m\]'
    COLOR_USER='\[\e[38;5;10m\]'
    COLOR_NORM='\[\e[0m\]'
elif [[ $COLORS -ge 8 ]]; then
    COLOR_ROOT='\[\e[1;31m\]'
    COLOR_SUDO='\[\e[1;33m\]'
    COLOR_USER='\[\e[1;32m\]'
    COLOR_NORM='\[\e[0m\]'
else
    COLOR_ROOT=
    COLOR_SUDO=
    COLOR_USER=
    COLOR_NORM=
fi

# Reset options for ls and grep.
LS_OPTS=
GREP_OPTS=

# If we have a color terminal, we'll use color for ls and grep.
if [[ $COLORS -ge 8 ]]; then
    hash dircolors &>/dev/null && eval "$(dircolors -b)"
    if ls --help | grep -- --color &>/dev/null; then
        LS_OPTS="${LS_OPTS} --color=auto"
    fi
    if grep --help | grep -- --color &>/dev/null; then
        GREP_OPTS="${GREP_OPTS} --color=auto"
    fi
fi

# Set up more options for grep; exclude version control files.
if ls --help | grep -- --exclude &>/dev/null; then
    for PATTERN in .git .gitignore .gitmodules; do
        GREP_OPTS="${GREP_OPTS} --exclude=${PATTERN}"
    done
fi
if grep --help | grep -- --exclude-dir &>/dev/null; then
    for PATTERN in .cvs .git .hg .svn; do
        GREP_OPTS="${GREP_OPTS} --exclude-dir=${PATTERN}"
    done
fi

# Alias ls and grep with the options we've collected.
alias ls="ls ${LS_OPTS}"
alias grep="grep ${GREP_OPTS}"

# Protect innocent MySQL databases from my stupidity.
alias mysql='mysql --safe-updates'

# I always do this, and I hate slow train.
alias sl='ls'

# I actually use ed now and then. Go figure.
alias ed='ed -p:'

# Make gdb shut up.
alias gdb='gdb -q'

# Decide on color for prompt.
if [[ $EUID -eq 0 ]]; then
    COLOR_PROMPT=${COLOR_ROOT}
elif [[ -n $SUDO_USER ]]; then
    COLOR_PROMPT=${COLOR_SUDO}
else
    COLOR_PROMPT=${COLOR_USER}
fi

# Frontend to controlling prompt.
function prompt {
    case "$1" in

        # Turn complex coloured prompt on.
        on) 
            PROMPT_COMMAND='history -a'
            PS1='[\u@\h:\w]$(prompt return)$(prompt vcs)$(prompt jobs)\$'
            PS1="${COLOR_PROMPT}${PS1}${COLOR_NORM} "
            case "$TERM" in
                screen*) PS1='\[\ek\h\e\\\]'${PS1};;
                xterm*)  PS1='\[\e]0;\h\a\]'${PS1};;
            esac
            ;;

        # Revert to simple inexpensive prompt.
        off)
            PROMPT_COMMAND=
            PS1='\$ '
            ;;

        # Git prompt function.
        git)
            git branch &>/dev/null || return 1
            HEAD="$(git symbolic-ref HEAD 2>/dev/null)"
            BRANCH="${HEAD##*/}"
            [[ -n "$(git status 2>/dev/null | \
                grep -F 'working directory clean')" ]] || STATUS="!"
            printf '(git:%s)' "${BRANCH:-unknown}${STATUS}"
            ;;

        # Mercurial prompt function.
        hg)
            hg branch &>/dev/null || return 1
            BRANCH="$(hg branch 2>/dev/null)"
            [[ -n "$(hg status 2>/dev/null)" ]] && STATUS="!"
            printf '(hg:%s)' "${BRANCH:-unknown}${STATUS}"
            ;;

        # Subversion prompt function.
        svn)
            svn info &>/dev/null || return 1
            URL="$(svn info 2>/dev/null | \
                awk -F': ' '$1 == "URL" {print $2}')"
            ROOT="$(svn info 2>/dev/null | \
                awk -F': ' '$1 == "Repository Root" {print $2}')"
            BRANCH=${URL/$ROOT}
            BRANCH=${BRANCH#/}
            BRANCH=${BRANCH#branches/}
            BRANCH=${BRANCH%%/*}
            [[ -n "$(svn status 2>/dev/null)" ]] && STATUS="!"
            printf '(svn:%s)' "${BRANCH:-unknown}${STATUS}"
            ;;

        # VCS wrapper prompt function.
        vcs)
            prompt git || prompt svn || prompt hg
            ;;

        # Return status prompt function.
        return)
            RETURN=$? 
            [[ $RETURN -ne 0 ]] && printf '<%d>' ${RETURN}
            ;;

        # Job count prompt function.
        jobs)
            [[ -n "$(jobs)" ]] && printf '{%d}' $(jobs | sed -n '$=')
            ;;
    esac
}

# Run local file if it exists.
[[ -e "${HOME}/.bashrc.local" ]] && source "${HOME}/.bashrc.local"

# Start with full-fledged prompt.
prompt on