-
Notifications
You must be signed in to change notification settings - Fork 1
/
.bashrc
319 lines (264 loc) · 8.97 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# Copyright (C) 2014-2024 Serghei Iakovlev <[email protected]>
#
# This file is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this file. If not, see <https://www.gnu.org/licenses/>.
# User wide interactive shell configuration and executing commands.
#
# This file is sourced by the second for login shells (after
# '~/.profile'). Or by the first for interactive non-login shells.
# shellcheck shell=bash
# If not running interactively, don't do anything
[[ $- != *i* ]] && return
# Auto-fix minor typos in interactive use of 'cd'
shopt -q -s cdspell
# Bash can automatically prepend cd when entering just a path in
# the shell
shopt -q -s autocd
# Case-insensitive globbing (used in pathname expansion).
shopt -s nocaseglob
# Check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS
shopt -q -s checkwinsize
# Don't let Ctrl-D exit the shell
set -o ignoreeof
# Immediate notification of background job termination
set -o notify
# --------------------------------------------------------------------
# History
# --------------------------------------------------------------------
# Append to the history file, don't overwrite it
shopt -s histappend
# Bash attempts to save all lines of a multiple-line command in the
# same history entry. This allows easy re-editing of multi-line
# commands.
shopt -q -s cmdhist
# See man -P 'less -rp HISTCONTROL' bash
HISTCONTROL="erasedups:ignoreboth"
# The number of commands in history stack in memory
HISTSIZE=5000
# Maximum number of history lines
HISTFILESIZE=10000
# For the protection and ability for future analyzing
HISTTIMEFORMAT="%h %d %H:%M:%S "
# Omit:
# & duplicates
# [ ] lines starting with spaces
# history * history command
# cd -*/cd +* navigation on directory stack
HISTIGNORE='&:[ ]*:history *:cd -*[0-9]*:cd +*[0-9]*'
# Save commands immediately after use to have shared history
# between Bash sessions.
# 'history -a' append the current history to the history file
# 'history -c' clear the history list
# 'history -r' read the history file and append its contents to
# the history list.
PROMPT_COMMAND="history -a; history -c; history -r; ${PROMPT_COMMAND}"
if [ -f ~/.config/dirstack.sh ]; then
. ~/.config/dirstack.sh
fi
# --------------------------------------------------------------------
# Setup Editor
# --------------------------------------------------------------------
# Editor to fallback to if the server is not running. If this
# variable is empty, then start GNU Emacs in daemon mode and try
# connecting again.
ALTERNATE_EDITOR=''
export ALTERNATE_EDITOR
EDITOR='emacsclient -c -nw -a ""'
export EDITOR
VISUAL="$EDITOR"
export VISUAL
# More for less
if [[ $TERM == "dumb" ]] ; then
PAGER=cat
else
PAGER=less
fi
export PAGER
if [[ $TERM != "dumb" ]]; then
# -X will leave the text in your Terminal, so it doesn't disappear
# when you exit less.
# -F will exit less if the output fits on one screen (so you don't
# have to press "q").
# -R ANSI "color" escape sequences are output in "raw" form.
#
# See: https://unix.stackexchange.com/q/38634/50400
LESS="-X -F -R"
LESSCHARSET=UTF-8
LESSHISTFILE="${XDG_CACHE_HOME:-$HOME/.cache}/lesshst"
export LESS LESSCHARSET LESSHISTFILE
fi
# Set the Less input preprocessor.
# Try to find either `lesspipe` or `lesspipe.sh` in the system path.
#
# Install using: `brew install lesspipe` on macOS
if [ -z "$LESSOPEN" ]; then
# Check for the existence of lesspipe or lesspipe.sh
if command -v lesspipe >/dev/null 2>&1; then
LESSOPEN="| /usr/bin/env lesspipe %s 2>&-"
elif command -v lesspipe.sh >/dev/null 2>&1; then
LESSOPEN="| /usr/bin/env lesspipe.sh %s 2>&-"
fi
[ -n "$LESSOPEN" ] && export LESSOPEN
fi
# --------------------------------------------------------------------
# Bash completion
# --------------------------------------------------------------------
if [ "$(uname)" = "Darwin" ] && command -v brew >/dev/null 2>&1; then
__brew_prefix=$(brew --prefix)
# brew install bash-completion
if [ -f "$brew_prefix"/etc/bash_completion ]; then
. "$brew_prefix"/etc/bash_completion
elif [ -f "$brew_prefix"/etc/profile.d/bash_completion.sh ]; then
# brew install bash-completion@2
if [ -d "$brew_prefix"/etc/bash_completion.d ]; then
BASH_COMPLETION_COMPAT_DIR="$brew_prefix"/etc/bash_completion.d
export BASH_COMPLETION_COMPAT_DIR
fi
. "$brew_prefix"/etc/profile.d/bash_completion.sh
fi
unset __brew_prefix
elif ! 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
# --------------------------------------------------------------------
# Setup colors
# --------------------------------------------------------------------
# Default to no color.
color_prompt=no
# Check explicit color preferences.
if [ "${COLORTERM:-}" = "truecolor" ] \
|| [ "${COLORTERM:-}" = "24bit" ] \
|| [ "${COLORTERM:-}" = "1" ] \
|| [ "${COLORTERM:-}" = "gnome-terminal" ] \
|| [ "${USE_ANSI_COLORS:-}" = "true" ] \
|| [ "$TERM" = "xterm-256color" ] \
|| tput setaf 1 >/dev/null 2>&1; then
color_prompt=yes
else
# Detect support for colors based on terminal type.
case "$TERM" in
*-256color) color_prompt=yes;;
xterm | xterm-color) color_prompt=yes;;
esac
# Fallback to terminfo detection if no explicit preference is
# found.
if [ "$color_prompt" = "no" ]; then
if command -v tput >/dev/null 2>&1; then
if tput setaf 1 >/dev/null 2>&1; then
color_prompt=yes
fi
fi
fi
fi
if [ "$colors_support" = true ]; then
# colorize gcc output
GCC_COLORS='error=01;31:'
GCC_COLORS+='warning=01;35:'
GCC_COLORS+='note=01;36:'
GCC_COLORS+='caret=01;32:'
GCC_COLORS+='locus=01:'
GCC_COLORS+='quote=01'
export GCC_COLORS
[[ -z "$COLORTERM" ]] || COLORTERM=1
[[ -z "$CLICOLOR" ]] || CLICOLOR=1
export CLICOLOR COLORTERM
fi
unset colors_support
# Enable color support of ls and also add handy aliases
if [ "$(uname)" = "Darwin" ]; then
if command -v dircolors >/dev/null 2>&1; then
if [ -f ~/.dircolors ]; then
eval "$(dircolors -b ~/.dircolors)"
else
eval "$(dircolors -b)"
fi
fi
else
if command -v gdircolors >/dev/null 2>&1; then
if [ -f ~/.dircolors ]; then
eval "$(gdircolors -b ~/.dircolors)"
else
eval "$(gdircolors -b)"
fi
fi
fi
# --------------------------------------------------------------------
# Setup aliases
# --------------------------------------------------------------------
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# --------------------------------------------------------------------
# Setup Bash prompt
# --------------------------------------------------------------------
__prompt_command() {
local exit_code="$?"
local rcol=''
local bldylw=''
local bldblu=''
local bldblk=''
local bldred=''
if [ "${COLORTERM:-}" = "truecolor" ] \
|| [ "${COLORTERM:-}" = "24bit" ] \
|| [ "${COLORTERM:-}" = "1" ] \
|| [ "${CLICOLOR:-}" = "1" ] \
|| [ "${USE_ANSI_COLORS:-}" = "true" ] \
|| [ "$TERM" = "xterm-256color" ] \
|| tput setaf 1 >/dev/null 2>&1; then
rcol='\[\e[0m\]'
bldylw='\[\e[1;33m\]'
bldblu='\[\e[1;34m\]'
bldblk='\[\e[1;30m\]'
bldred='\[\e[1;31m\]'
fi
# PS1="${bldblk}"
PS1='\u@\h'
# PS1+="${rcol}"
if [ $exit_code != 0 ]; then
PS1+=" [${bldred}${exit_code}${rcol}]"
fi
PS1+=" ${bldblu}"
PS1+='\w'
local b="$(git symbolic-ref HEAD 2>/dev/null)";
if [ -n "$b" ]; then
PS1+="${rcol} ${bldylw}${b##refs/heads/}"
fi
PS1+="${rcol}"
PS1+='\n\$ '
}
PROMPT_COMMAND="__prompt_command; ${PROMPT_COMMAND}"
PS2='> '
# --------------------------------------------------------------------
# Setup direnv
# --------------------------------------------------------------------
if command -v direnv >/dev/null 2>&1; then
eval "$(direnv hook bash)"
fi
# --------------------------------------------------------------------
# Setup Nodejs
# --------------------------------------------------------------------
# Do not use NPM_CONFIG_PREFIX env var when nvm is used.
# nvm is not compatible with the NPM_CONFIG_PREFIX env var.
if [ -z "$NVM_DIR" ]; then
# Resolving EACCES permissions errors when installing packages globally.
NPM_CONFIG_PREFIX="$HOME/.local/"
export NPM_CONFIG_PREFIX
fi
# Local Variables:
# mode: sh
# End: