-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bashrc.extra
352 lines (311 loc) · 12.3 KB
/
.bashrc.extra
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# This file should be made world-readable, and then included at the end of your ~/.bashrc
if [[ "$(uname)" == "Darwin" ]]; then
MACOS="1"
else
MACOS=""
fi
# Terminal color codes
# Note 1: Linux understands either \e or \033, but MacOS only understands \033
# Note 2: Enclosing the color codes in \[...\] is useful in the $PS1 prompt, because that excludes the code from bash's character counts and thus fixes alignment and wrapping issues. In 'echo' on MacOS, however, the extra \[ and \] will output to the screen.
# Note 3: For some strange reason, \[${fc_cyan_u}\] does NOT fix the wrapping issues, while \[\033[4;36m\] will. So both options are provided here, depending on what you need.
clrreset="\033[m"; clrreset_enc="\[\033[m\]"
fc_black="\033[0;30m"; fc_black_enc="\[\033[0;30m\]"
fc_red="\033[0;31m"; fc_red_enc="\[\033[0;31m\]"
fc_green="\033[0;32m"; fc_green_enc="\[\033[0;32m\]"
fc_yellow="\033[0;33m"; fc_yellow_enc="\[\033[0;33m\]"
fc_blue="\033[0;34m"; fc_blue_enc="\[\033[0;34m\]"
fc_magenta="\033[0;35m"; fc_magenta_enc="\[\033[0;35m\]"
fc_cyan="\033[0;36m"; fc_cyan_enc="\[\033[0;36m\]"
fc_white="\033[0;37m"; fc_white_enc="\[\033[0;37m\]"
fc_black_b="\033[1;30m"; fc_black_b_enc="\[\033[1;30m\]"
fc_red_b="\033[1;31m"; fc_red_b_enc="\[\033[1;31m\]"
fc_green_b="\033[1;32m"; fc_green_b_enc="\[\033[1;32m\]"
fc_yellow_b="\033[1;33m"; fc_yellow_b_enc="\[\033[1;33m\]"
fc_blue_b="\033[1;34m"; fc_blue_b_enc="\[\033[1;34m\]"
fc_magenta_b="\033[1;35m"; fc_magenta_b_enc="\[\033[1;35m\]"
fc_cyan_b="\033[1;36m"; fc_cyan_b_enc="\[\033[1;36m\]"
fc_white_b="\033[1;37m"; fc_white_b_enc="\[\033[1;37m\]"
fc_black_u="\033[4;30m"; fc_black_u_enc="\[\033[4;30m\]"
fc_red_u="\033[4;31m"; fc_red_u_enc="\[\033[4;31m\]"
fc_green_u="\033[4;32m"; fc_green_u_enc="\[\033[4;32m\]"
fc_yellow_u="\033[4;33m"; fc_yellow_u_enc="\[\033[4;33m\]"
fc_ulue_u="\033[4;34m"; fc_ulue_u_enc="\[\033[4;34m\]"
fc_magenta_u="\033[4;35m"; fc_magenta_u_enc="\[\033[4;35m\]"
fc_cyan_u="\033[4;36m"; fc_cyan_u_enc="\[\033[4;36m\]"
fc_white_u="\033[4;37m"; fc_white_u_enc="\[\033[4;37m\]"
bc_black="\033[40m"; bc_black_enc="\[\033[40m\]"
bc_red="\033[41m"; bc_red_enc="\[\033[41m\]"
bc_green="\033[42m"; bc_green_enc="\[\033[42m\]"
bc_yellow="\033[43m"; bc_yellow_enc="\[\033[43m\]"
bc_blue="\033[44m"; bc_blue_enc="\[\033[44m\]"
bc_magenta="\033[45m"; bc_magenta_enc="\[\033[45m\]"
bc_cyan="\033[46m"; bc_cyan_enc="\[\033[46m\]"
bc_white="\033[47m"; bc_white_enc="\[\033[47m\]"
# Custom terminal prompt
# HH:MM:SS host directory user>
# HH:MM:SS host directory root#
__prompt_command() {
local EXIT="$?" # This needs to be the first line to be able to capture the real code
PS1="${fc_blue_b_enc}\t ${fc_white_enc}\j ${fc_cyan_b_enc}\u@\h "
if [[ $EXIT != 0 ]]; then
# Last command failed, so add a prefix
PS1="\[\033[41;37m\]ERR=${EXIT}${clrreset_enc} ${PS1}"
fi
if [[ -n "$VIRTUAL_ENV" ]]; then
PS1="${PS1}${fc_magenta_b_enc}[venv] "
fi
if [[ ${EUID} == 0 ]] ; then
# Root user
PS1="${PS1}${fc_red_enc}\W #${clrreset_enc} "
else
# Normal user
PS1="${PS1}${fc_green_enc}\W >${clrreset_enc} "
fi
}
PROMPT_COMMAND="__prompt_command"
# Add color
alias grep='grep --colour=auto'
alias egrep='egrep --colour=auto'
alias fgrep='fgrep --colour=auto'
[[ -z $MACOS ]] && alias ls='ls --color=auto'
alias tree='tree -C'
# Show more information on jobs
alias jobs='jobs -l'
# Aliases just to shorten things
alias ..='cd ..'
alias cd..='cd ..'
alias c='clear'
alias h='history'
alias j='jobs'
# Make parent folders if necessary
alias mkdir='mkdir -pv'
# Customize the standard ls display
if [[ $MACOS ]]; then
alias l="ls -lhvF"
alias la="ls -AlhvF"
else
alias l="ls -lhvF --time-style='+%F %r ' --group-directories-first"
alias la="ls -AlhvF --time-style='+%F %r ' --group-directories-first"
fi
# List files in date order, newest on top
if [[ $MACOS ]]; then
alias lt="ls -AlhvFt"
else
alias lt="ls -AlhvFt --time-style='+%F %r '"
fi
# Show file permissions in numeric format (pipe l/la/lt commands into this; i.e. "lt | numericmodes")
alias numericmodes='awk '\''{
k = 0
s = 0
for( i = 0; i <= 8; i++ )
{
k += ( ( substr( $1, i+2, 1 ) ~ /[rwxst]/ ) * 2 ^( 8 - i ) )
}
j = 4
for( i = 4; i <= 10; i += 3 )
{
s += ( ( substr( $1, i, 1 ) ~ /[stST]/ ) * j )
j/=2
}
if ( k )
{
printf( "%0o%0o ", s, k )
}
print
}'\'
# Show my own processes, with 3/10sec refresh rate
[[ -z $MACOS ]] && alias t='htop -d 3 -u $USER'
# Copy to clipboard (must install xclip first)
[[ -z $MACOS ]] && alias pbcopy='xclip -selection clipboard'
# Show mounts in aligned columns
alias m='mount | column -t'
# From within a VM, mount a shared folder from the host machine
if [[ -z $MACOS ]]; then
mountvbox() {
sudo mkdir -p /mnt/$1 && sudo mount -t vboxsf $1 /mnt/$1 && l /mnt/$1
}
fi
# Show the $PATH environment variable with each entry on its own line
alias path='echo -e ${PATH//:/\\n}'
# Make rm safer by preventing user from accidentally removing '/', and prompt if deleting more than 3 files
[[ -z $MACOS ]] && alias rm='rm -I --preserve-root'
# Make mv and cp safer by prompting before overwriting any files
alias mv='mv -i'
alias cp='cp -i'
# Make ln safer by prompting whether to remove destinations
alias ln='ln -i'
# Make these safer by preventing any changes to '/'
if [[ -z $MACOS ]]; then
alias chown='chown --preserve-root'
alias chmod='chmod --preserve-root'
alias chgrp='chgrp --preserve-root'
fi
# Show IP addresses in columns
alias i='ifconfig | grep "^\W*inet" | column -t | cut -c -$COLUMNS'
# Show human-readable numbers
alias df='df -h'
# Show sizes in MB
[[ -z $MACOS ]] && alias free='free -m'
# Show every line of a file that is NOT commented out
alias nocomment="grep -v -e '^\s*[#;]' -e '^$'"
# List all aliases and functions
alias al='echo "Aliases and functions (use 'alc' to see full details):"; alias | /usr/bin/grep "^alias "; declare -F -p | cut -d " " -f 3 | grep -v "^_"'
alias alc='alias; declare -f'
# Some useful git aliases
# Show current status of local store
alias gs="echo; echo 'Remotes:'; git remote -v; echo; echo 'Branches:'; git branch -a -vv; echo; echo 'Working tree:'; git status -s; echo; echo 'Commit log:'; git log -6 --oneline --decorate; echo"
# Show full commit log
alias gl='git log --oneline --decorate'
# Unstage a file, by resetting the file to the HEAD
alias gu='git reset HEAD --'
# Run a 'git diff', followed by a 'git add' if requested
ga() {
[ $# -eq 1 ] || return 1
echo
git diff $1
echo
echo -n "Would you like to perform 'git add' on this same file? (y/N)"
read ans
if [[ "$ans" == "y" ]] || [[ "$ans" == "Y" ]]; then
git add -v $1
fi
echo
}
# Output a horizontal rule to visually separate projects
alias e="echo -e '\n========================================================\n'"
# Allow aliases to work even when using sudo
alias sudo='sudo '
# Restore the auto service/file completion after the “sudo” command
complete -cf sudo
# Set default editor
export EDITOR=nano
# Various customizations to the command history
export HISTCONTROL=ignoreboth 2>/dev/null # Ignore any command that begins with whitespace, and also do not store duplicate commands in the history
typeset -r HISTCONTROL # (mark this option read-only)
export HISTSIZE=2000 2>/dev/null
typeset -r HISTSIZE # (mark this option read-only)
export HISTFILESIZE=${HISTSIZE} 2>/dev/null
typeset -r HISTFILESIZE # (mark this option read-only)
export HISTIGNORE= 2>/dev/null
typeset -r HISTIGNORE # (mark this option read-only)
export HISTTIMEFORMAT="%F %r | " 2>/dev/null # Format the history with datestamps
typeset -r HISTTIMEFORMAT # (mark this option read-only)
shopt -s cmdhist # Put multi-line commands on one line
shopt -s histappend # Append instead of overwriting the entire file
# Other shell options
shopt -s cdspell # Minor errors in the spelling of a directory component in a cd command will be corrected
shopt -s checkwinsize # Check the window size after each command and, if necessary, updates the values of LINES and COLUMNS
shopt -s dotglob # Include filenames beginning with a . in the results of filename expansion
shopt -s expand_aliases # Aliases are expanded too
shopt -s extglob # Extended pattern matching features are enabled
# Be notified asynchronously about completed background jobs
set -o notify
# Shows running processes, and optionally filters based on the name passed (i.e. "p root")
if [[ $MACOS ]]; then
p() {
if [[ -z "$1" ]]; then
ps -o ruser,user,start,time,pid,command -xe
else
ps -o ruser,user,start,time,pid,command -xu $1
fi
}
else
p() {
if [[ -z "$1" ]]; then
ps -o ruser,user,start,time,pid,command ax
else
ps -o ruser,user,start,time,pid,command aU $1
fi
}
fi
# Extends the information displayed by the 'which' command
# Usage: wh <command>
wh() {
loc=$(which $1)
if [[ -z "$loc" ]]; then
echo "Command not found: $1"
else
echo $loc
whatis $1
ls -l $loc
file -bkrLz $loc
fi
}
# Opens the specified file with nano, and will prompt for sudo password if it is not writeable
# Usage: edit <file>
edit() {
if [[ -f "$1" ]]; then
if [[ -w "$1" ]]; then
$EDITOR "$1"
else
echo "File is not writable; opening it with sudo..."
sudo $EDITOR "$1"
fi
else
echo "File does not exist: $1"
fi
}
# Recursively search all non-binary files within the current directory
# Usage: grepr <searchtext>
grepr() {
grep --color=auto -RIn "$1" *
}
# Set the window/tab title
# Usage: settitle <title>
settitle() {
echo -en "\033]0; $* \007";
}
# Automatically extract files
# Usage: extract <file>
extract() {
if [[ -f $1 ]] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via ex()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# Search for and activate a Python virtual environment
ve() {
if [[ -z "$VIRTUAL_ENV" ]]; then
_ve bin/activate ||
_ve venv/bin/activate ||
_ve .venv/bin/activate ||
_ve ../venv/bin/activate ||
_ve ../.venv/bin/activate ||
_ve ../../venv/bin/activate ||
_ve ../../.venv/bin/activate ||
echo "ERROR: Could not find a virtual environment to source."
else
echo
venv="$VIRTUAL_ENV"
deactivate
echo -e "${fc_magenta_b}Deactivated virtual environment $venv${clrreset}"
echo
echo
fi
}
_ve() {
if [[ -f "$1" ]]; then
echo
echo
. "$1"
echo -e "${fc_magenta_b}You are now using virtual environment $VIRTUAL_ENV${clrreset}"
echo
return 0
else
return 1
fi
}