aboutsummaryrefslogtreecommitdiff
path: root/.bashrc
blob: f526efa3fd11c6ea40afaa48978fa633ac33d2ea (plain)
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
if [[ $- != *i* ]] ; then
	# Shell is non-interactive, don't do anything.
	return
fi

[ $DEBUG_DOTFILES ] && echo "Loading .bashrc"

# Load initialization
[ $DEBUG_DOTFILES ] && echo "  Loading ~/.config/shell/init"
source ~/.config/shell/init

# Load the sources file, which in turn will source more functionality
[ $DEBUG_DOTFILES ] && echo "  Loading ~/.config/shell/sources"
source ~/.config/shell/sources

# Set vi mode
[ $DEBUG_DOTFILES ] && echo "  Setting vi-mode"
set -o vi

# Bind some emacs-style bindings in insert mode
bind -m vi-insert "\C-a":beginning-of-line
bind -m vi-insert "\C-b":backward-char
bind -m vi-insert "\C-e":end-of-line
bind -m vi-insert "\C-f":forward-char
bind -m vi-insert "\C-k":kill-line
bind -m vi-insert "\C-l":clear-screen
bind -m vi-insert "\C-n":history-search-forward
bind -m vi-insert "\C-p":history-search-backward
bind -m vi-insert "\C-u":kill-region
bind -m vi-insert "\C-y":yank

# Configure Bash options
[ $DEBUG_DOTFILES ] && echo "  Setting Bash options"
shopt -s histappend
shopt -s progcomp

if [ -r "$HOME/.config/shell/vendor/bash/feature-auto-completion/bash_completion" ]
then
	[ $DEBUG_DOTFILES ] && echo "    Setting up autocompletion"
	.  "$HOME/.config/shell/vendor/bash/feature-auto-completion/bash_completion"
fi

export COMP_WORDBREAKS="${COMP_WORDBREAKS//:}"

# Set up the prompt
[ $DEBUG_DOTFILES ] && echo "  Setting PS1"
PROMPT_COMMAND=__precmd

__precmd()
{
	# Record exit code of previous command immediately
	local previous_exit=$?

	# Colors in the Bash prompt should be wrapped in \001 and \002,
	# (RL_PROMPT_START_INGORE and RL_PROMPT_END_IGNORE) in order to avoid
	# wrapping issues later in life. It is also *very* important to wrap
	# things in double quotes for the PS1 coloring to be applied correctly!

	PS1=

	# Set the username, hostname and path
	if [[ 0 < $EUID ]]
	then
		PS1+="\001$SHELLC_F_GREEN\002"
	else
		PS1+="\001$SHELLC_F_RED\002"
	fi

	PS1+="\u\001$SHELLC_RESET_COLOR_F\002"
	PS1+="\001$SHELLC_F_LIGHTGRAY\002@\001$SHELLC_RESET_COLOR_F\002"
	PS1+="\001$SHELLC_F_MAGENTA\002\H\001$SHELLC_RESET_COLOR_F\002"
	PS1+="\001$SHELLC_F_LIGHTGRAY\002:\001$SHELLC_RESET_COLOR_F\002"
	PS1+="\001$SHELLC_F_BLUE\002\w\001$SHELLC_RESET_COLOR_F\002"

	# Add git status
	if git rev-parse --is-inside-work-tree &> /dev/null
	then
		PS1+="\001$SHELLC_F_LIGHTGRAY\002·\001$SHELLC_RESET_COLOR_F\002"

		# Set coloring of the branch name
		if [[ "$(git status -uno --porcelain 2> /dev/null)" == "" ]]
		then
			PS1+="\001$SHELLC_F_GREEN\002"
		else
			PS1+="\001$SHELLC_F_YELLOW\002"
		fi

		# Get the branch name
		local git_branch_name=$(git rev-parse --abbrev-ref HEAD)

		if [[ "$git_branch_name" == "HEAD" ]]
		then
			git_branch_name=$(git rev-parse --short HEAD 2> /dev/null)
		fi

		# Set the prompt
		PS1+="$git_branch_name\001$SHELLC_RESET_COLOR_F\002"
	fi

	# Add the exit code of the previous command
	PS1+=" "

	if [[ $previous_exit -ne 0 ]]
	then
		PS1+="\001$SHELLC_F_RED\002"
	fi

	PS1+="$(printf "%03i" "$previous_exit")\001$SHELLC_RESET_COLOR_F\002"

	# Final prompt character
	PS1+=" \001$SHELLC_F_LIGHTGRAY\002»\001$SHELLC_RESET_FULL\002 \001$CURSOR_STYLE\002"
}