-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.bashrc
138 lines (119 loc) · 4.16 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
# Exports
export EDITOR=vim
# History
HISTCONTROL=ignoreboth # don't put duplicate lines or lines starting with space in the history.
shopt -s histappend # append to the history file, don't overwrite it
HISTSIZE=1500
HISTFILESIZE=4000
# General bash stuff
shopt -s checkwinsize # check the window size after each command and, if necessary, update the values of LINES and COLUMNS
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)" # make less more friendly for non-text input files, see lesspipe(1)
# Add bash aliases.
if [ -f ~/.bash_aliases ]; then
source ~/.bash_aliases
fi
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
# Much of what is below has been tweaked from
# https://github.com/cowboy/dotfiles
# git branch (gb) with descriptions
function gb() {
branches=$(git for-each-ref --format='%(refname)' refs/heads/ | sed 's|refs/heads/||')
for branch in $branches; do
desc=$(git config branch.$branch.description)
if [ $branch == $(git rev-parse --abbrev-ref HEAD) ]; then
branch="* \033[0;32m$branch\033[0m"
else
branch=" $branch"
fi
echo -e "$branch \033[0;36m$desc\033[0m"
done
}
# ANSI CODES - SEPARATE MULTIPLE VALUES WITH ;
#
# 0 reset 4 underline
# 1 bold 7 inverse
#
# FG BG COLOR FG BG COLOR
# 30 40 black 34 44 blue
# 31 41 red 35 45 magenta
# 32 42 green 36 46 cyan
# 33 43 yellow 37 47 white
if [[ ! "${prompt_colors[@]}" ]]; then
prompt_colors=(
"1;32" # information color
"1;37" # bracket color
"31" # error color
)
if [[ "$SSH_TTY" ]]; then
# connected via ssh
prompt_colors[0]="32"
elif [[ "$USER" == "root" ]]; then
# logged in as root
prompt_colors[0]="35"
fi
fi
# Inside a prompt function, run this alias to setup local $c0-$c9 color vars.
alias prompt_getcolors='prompt_colors[9]=; local i; for i in ${!prompt_colors[@]}; do local c$i="\[\e[0;${prompt_colors[$i]}m\]"; done'
# Exit code of previous command.
function prompt_exitcode() {
prompt_getcolors
[[ $1 != 0 ]] && echo " $c2$1$c9"
}
# Git status.
function prompt_git() {
prompt_getcolors
local status output flags
status="$(git status 2>/dev/null)"
[[ $? != 0 ]] && return;
output="$(echo "$status" | awk '/# Initial commit/ {print "(init)"}')"
[[ "$output" ]] || output="$(echo "$status" | awk '/# On branch/ {print $4}')"
[[ "$output" ]] || output="$(git branch | perl -ne '/^\* (.*)/ && print $1')"
flags="$(
echo "$status" | awk 'BEGIN {r=""} \
/^# Changes to be committed:$/ {r=r "+"}\
/^# Changes not staged for commit:$/ {r=r "!"}\
/^# Untracked files:$/ {r=r "?"}\
END {print r}'
)"
if [[ "$flags" ]]; then
output="$output$c1:$c0$flags"
fi
echo "$c1 git:$c0$output$c9"
}
# Maintain a per-execution call stack.
prompt_stack=()
trap 'prompt_stack=("${prompt_stack[@]}" "$BASH_COMMAND")' DEBUG
function prompt_command() {
local exit_code=$?
# If the first command in the stack is prompt_command, no command was run.
# Set exit_code to 0 and reset the stack.
[[ "${prompt_stack[0]}" == "prompt_command" ]] && exit_code=0
prompt_stack=()
# Manually load z here, after $? is checked, to keep $? from being clobbered.
[[ "$(type -t _z)" ]] && _z --add "$(pwd -P 2>/dev/null)" 2>/dev/null
# While the simple_prompt environment var is set, disable the awesome prompt.
[[ "$simple_prompt" ]] && PS1='\n$ ' && return
prompt_getcolors
PS1="\n"
# history #: !234
PS1="$PS1$c0!\!$c1$c9 "
# date: HH:MM:SS
PS1="$PS1$c0$(date +"%H$c1:$c0%M$c1:$c0%S")$c1$c9"
# path: [user@host:path] "$PS1$c1[$c0\u$c1@$c0\h$c1:$c0\w$c1]$c9"
PS1="$PS1 $c0\w$c1 " # [path]
# git: git:branch:flags
PS1="$PS1$(prompt_git)"
# exit code: 127
PS1="$PS1$(prompt_exitcode "$exit_code")"
PS1="$PS1\n$c0 ♣_( ♥_♥ )_♣ $c9"
}
PROMPT_COMMAND="prompt_command"