-
Notifications
You must be signed in to change notification settings - Fork 1
/
_bashrc
413 lines (334 loc) · 9.93 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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# vim: set ft=sh:
# Jacob Degeling's .bashrc file
#if not interactive, get out of here
[[ -z "$PS1" ]] && return
export PATH=/usr/local/bin:/usr/local/sbin:/usr/local/share/npm/bin:$PATH
if [ -d ~/bin ]; then
export PATH=~/bin:$PATH
fi
export EDITOR=vim
# command colouring
export CLICOLOR=1
# define colors
C_DEFAULT="\[\033[m\]"
C_RESET="\[\033[0m\]"
C_BLACK="\[\033[0;30m\]"
C_RED="\[\033[0;31m\]"
C_GREEN="\[\033[0;32m\]"
C_YELLOW="\[\033[0;33m\]"
C_BLUE="\[\033[0;34m\]"
C_MAGENTA="\[\033[0;35m\]"
C_CYAN="\[\033[0;36m\]"
C_WHITE="\[\033[0;37m\]"
C_LIGHTBLACK="\[\033[1;30m\]"
C_LIGHTRED="\[\033[1;31m\]"
C_LIGHTGREEN="\[\033[1;32m\]"
C_LIGHTYELLOW="\[\033[1;33m\]"
C_LIGHTBLUE="\[\033[1;34m\]"
C_LIGHTMAGENTA="\[\033[1;35m\]"
C_LIGHTCYAN="\[\033[1;36m\]"
C_LIGHTWHITE="\[\033[1;37m\]"
C_BG_BLACK="\[\033[40m\]"
C_BG_RED="\[\033[41m\]"
C_BG_GREEN="\[\033[42m\]"
C_BG_YELLOW="\[\033[43m\]"
C_BG_BLUE="\[\033[44m\]"
C_BG_MAGENTA="\[\033[45m\]"
C_BG_CYAN="\[\033[46m\]"
C_BG_WHITE="\[\033[47m\]"
##############
# functions
##############
# wrapper for making a directory and changing to it in one go
function md () {
mkdir -p "$@" && cd "$@"
}
# change dir and then list contents
function cl () {
cd "$@" && ls
}
function cll () {
cd "$@" && ll
}
function g {
git ${@:-status -s}
}
# Enable tab completion for `g` by marking it as an alias for `git`
if type __git_complete &> /dev/null; then
__git_complete g __git_main
fi;
function pud () {
pushd ${@:-'.'}
}
alias pod='popd'
alias weather='curl http://wttr\.in'
alias grep='grep --color=auto'
function o {
open ${@:-'.'}
}
function port {
port = "$@"
if [ -z "$port" ]; then
lsof -i
else
lsof -i :$port
fi
}
function mu() {
if [ -z "$@" ]; then
echo "usage: mu NAME" >&2
else
awk_arg='/arg_one/ {sum += $6} END { printf "%dMB\n", sum/1024 }'
# echo ${awk_arg/arg_one/$1}
ps aux | awk "${awk_arg/arg_one/$1}"
fi
}
# go up a directory level.
# Takes a parameter that determines the level to go up, i.e.: up 3 will
# be the same as cd ../../../
function up () {
levels="$@"
if [ -z "$levels" ]; then
levels=1
fi
# Test if $levels is a number; the -eq operator expects a number, and will
# output an error if one is not found. Any output to STDERR is redirected
# to the bit bucket (/dev/null) so as not to cause unnessecary output to the
# screen; all we
if [ "$levels" -eq "$levels" ] 2> /dev/null; then
if [ "$levels" -eq "0" ]; then
levels=1
fi
directories=""
for (( c=1; c<=levels; c++ ))
do
parent=../
directories=${directories}${parent}
done
cd $directories
# echo Exit status: $?
else
echo up: expected a number, not $levels
fi
}
# Create a data URL from a file
function dataurl() {
if [ -z "$@" ]; then
echo "usage: dataurl FILE" >&2
else
local mimeType=$(file -b --mime-type "$1")
if [[ $mimeType == text/* ]]; then
mimeType="${mimeType};charset=utf-8"
fi
echo "data:${mimeType};base64,$(openssl base64 -in "$1" | tr -d '\n')"
fi
}
##############
# Prompt: uname in cwd on (git-branch) $
##############
function _c() {
colour=$1
text=$2
echo -ne ${colour}${text}${C_DEFAULT}
}
# Fastest possible way to check if repo is dirty.
function parse_git_dirty() {
git diff --quiet --ignore-submodules HEAD 2>/dev/null; [ $? -eq 1 ] && echo '*'
}
function parse_git_branch() {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1$(parse_git_dirty) /"
}
function sc () {
local colours=$1
local brightness=$2
if [ -z "$1" ]; then
echo "usage: sc command | colourscheme <brightness>"
echo " commands: "
echo " list: show all the available colourschemes"
echo " off: turn shell colours off (use terminal"
echo " emulator's default colours)"
return 1
fi
case "$1" in
"list")
# list colours
find ~/.shellcolour/ -name '*.sh' | sed -r 's/.*base16-(.*)\.(light|dark)\.sh/\1/' | uniq | column
return 0
;;
"off")
echo "off" > ~/.shellcolourrc
return 0
;;
esac
if [ -z "$brightness" ]; then
brightness=light
fi
colours_file=~/.shellcolour/base16-$colours.$brightness.sh
if [ -f "$colours_file" ]; then
source $colours_file
echo "$colours $brightness" > ~/.shellcolourrc
else
echo "sc: hmmm... that colourscheme doesn't exist!"
return 1
fi
}
_complete_screencolours() {
local cur=${COMP_WORDS[COMP_CWORD]}
local prev=${COMP_WORDS[COMP_CWORD-1]}
local wordlist=$(find ~/.shellcolour/ -name '*.sh' | sed -r 's/.*base16-(.*)\.(light|dark)\.sh/\1/' | uniq)
# local pipelist=$(echo $wordlist | sed ':a;N;$!ba;s/\n/|/g')
for word in $wordlist; do
if [ $word = $prev ]; then
COMPREPLY=($(compgen -W "dark light" -- ${cur}))
return 0
fi
done
COMPREPLY=($(compgen -W '${wordlist[@]}' -- "$cur"))
return 0
}
complete -F _complete_screencolours sc
sc $(cat ~/.shellcolourrc 2> /dev/null || echo ocean)
_reboot_required() {
case "$(uname)" in
Linux)
if [ -f /var/run/reboot-required ]; then
echo -ne "⬤ "
return 0
else
echo -ne ""
fi
;;
*)
echo -ne ""
return 1
esac
}
function prompt () {
local arg=$1
local _ps1
case "$arg" in
--minimal|-m)
# echo prompt: minimal prompt
_ps1="$(_c $C_GREEN '\$') "
;;
--basic-git|-g)
# echo prompt: basic prompt + git branch
_ps1="$(_c $C_LIGHTBLUE '\w') $(_c $C_YELLOW '$(parse_git_branch)')$(_c $C_GREEN '\$') "
;;
--basic|-b)
# echo prompt: basic prompt
_ps1="$(_c $C_LIGHTBLUE '\w') $(_c $C_GREEN '\$') "
;;
--full|-f)
# echo prompt: full prompt
_ps1="$(_c $C_LIGHTBLUE '\u') at $(_c $C_CYAN '\h') in $(_c $C_LIGHTBLUE '\w')\$([[ -n \$(git branch 2> /dev/null) ]] && echo \" on \")$(_c $C_YELLOW '$(parse_git_branch)')$(_c $C_GREEN '\$') "
;;
esac
if [ "$_ps1" = "" ]; then
echo "usage: prompt <-mbgf>|<--minimal --basic --basic-git --full>"
return 1
fi
_ps1="$(_c $C_RED '$(_reboot_required)')$_ps1"
# save the last used prompt to a file to preserve the prompt choice across
# sessions
echo $arg > ~/.prompt
export PS1=$_ps1
}
# set up a bash prompt, defaulting to full prompt if the ~/.prompt file
# containing the last used prompt is not found
prompt $(cat ~/.prompt 2> /dev/null || echo --full)
# Function to easily extract files from any type of archive
# https://github.com/xvoland/Extract/blob/master/extract.sh
function extract {
if [ -z "$1" ]; then
# display usage if no parameters given
echo "Usage: extract <path/file_name>.<zip|rar|bz2|gz|tar|tbz2|tgz|Z|7z|xz|ex|tar.bz2|tar.gz|tar.xz>"
else
if [ -f $1 ] ; then
# NAME=${1%.*}
# mkdir $NAME && cd $NAME
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.lzma) unlzma $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x -ad $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*.xz) unxz $1 ;;
*.exe) cabextract $1 ;;
*) echo "extract: '$1' - unknown archive method" ;;
esac
else
echo "$1 - file does not exist"
fi
fi
}
##############
# Command History settings
##############
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoredups:erasedups
# shell options
# ##########################
# append to the history file, don't overwrite it
shopt -s histappend
# globstar
shopt -s globstar
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=100000
HISTFILESIZE=100000
export HISTTIMEFORMAT='%F %T '
export HISTIGNORE="clear:bg:fg:jobs:cd -:cd ../:ll:ls:l"
bind '"\e[A":history-search-backward'
bind '"\e[B":history-search-forward'
#####
# moving quickly around the shell
# thanks to:
# http://jeroenjanssens.com/2013/08/16/quickly-navigate-your-filesystem-from-the-command-line.html
#####
export MARKPATH=$HOME/.marks
function jump {
cd -P "$MARKPATH/$1" 2>/dev/null || echo "No such mark: $1"
}
function mark {
mkdir -p "$MARKPATH"; ln -s "$(pwd)" "$MARKPATH/$1"
}
function unmark {
rm -i "$MARKPATH/$1"
}
case $(uname) in
'Linux')
function marks {
ls -l "$MARKPATH" | sed 's/ / /g' | cut -d' ' -f9- | sed 's/ -/\t-/g' && echo
}
;;
'Darwin')
function marks {
\ls -l "$MARKPATH" | tail -n +2 | sed 's/ / /g' | cut -d' ' -f9- | awk -F ' -> ' '{printf "%-10s -> %s\n", $1, $2}'
}
;;
esac
_completemarks() {
local curw=${COMP_WORDS[COMP_CWORD]}
local wordlist=$(find $MARKPATH -type l -printf "%f\n")
COMPREPLY=($(compgen -W '${wordlist[@]}' -- "$curw"))
return 0
}
complete -F _completemarks jump unmark
############## Source local bash settings
#############
if [ -f ~/.bashrc_local_$(uname) ]; then
source ~/.bashrc_local_$(uname)
fi
if [ -f ~/.bashrc_local ]; then
source ~/.bashrc_local
fi