This repository has been archived by the owner on Mar 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
bashrc
405 lines (346 loc) · 10.2 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
# .bashrc for Arch Linux, user mode
# many bits shamelessly stolen from jwr
# If not running interactively, don't do anything
[[ $- != *i* ]] && return
if [ "$COLORTERM" == "xterm" ] || [ "$COLORTERM" == "xfce4-terminal" ]; then
export TERM=xterm-256color
fi
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
. ~/.config/git/git-prompt.sh
. ~/.config/git/git-completion.bash
# --- prompt {{{
#GIT_PS1_SHOWCOLORHINTS=true
GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWSTASHSTATE=true
GIT_PS1_SHOWUNTRACKEDFILES=true
GIT_PS1_SHOWUPSTREAM=auto
HOST_COLOR='\[\033[1;36m\]' # b.cyan
RESET_COLOR='\[\033[0m\]' # white
ERROR_COLOR='\[\033[1;31m\]'
GIT_COLOR='\[\033[1;33m\]' # b.yellow
GIT_STAT='$(__git_ps1 " [%s]")'
if [ `whoami` == "root" ] ; then
USER_COLOR='\[\033[0;31m\]' # b.red
DIR_COLOR=$USER_COLOR
SYMBOL='\\$'
else
USER_COLOR='\[\033[1;32m\]' # b.green
DIR_COLOR='\[\033[1;34m\]' # b.blue
SYMBOL='\$'
fi
#export PS1="$DIR_COLOR[\W] $USER_COLOR$SYMBOL$RESET_COLOR "
export PS1="$DIR_COLOR[\W]$GIT_COLOR$GIT_STAT $USER_COLOR$SYMBOL$RESET_COLOR "
export PS2='continue> '
export PS3='choose: '
export PS4='|${BASH_SOURCE} ${LINENO}${FUNCNAME[0]:+ ${FUNCNAME[0]}()}| '
#}}}
# --- options {{{
shopt -s autocd # change to named directory
shopt -s cdable_vars # if cd arg is not valid, assumes its a var defining a dir
shopt -s cdspell # autocorrects cd misspellings
shopt -s checkwinsize # update the value of LINES and COLUMNS after each command if altered
shopt -s cmdhist # save multi-line commands in history as single line
shopt -s histappend # append to (not overwrite) the history file
shopt -s histreedit # re-edit a failed history substitution
shopt -s histverify
shopt -s dotglob # include dotfiles in pathname expansion
shopt -s expand_aliases # expand aliases
shopt -s extglob # enable extended pattern-matching features
shopt -s globstar # recursive globbing
shopt -s progcomp # programmable completion
shopt -s hostcomplete # attempt hostname expansion when @ is at the beginning ofa word
shopt -s nocaseglob # pathname expansion will be treated as case-insensitive
# bash completion
set show-all-if-ambiguous on
#}}}
# --- history {{{
# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
unset HISTFILESIZE
HISTCONTROL=ignoreboth:erasedups
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=3000
#HISTFILESIZE=
# don't append the following to history: consecutive duplicate
# commands, ls, bg and fg, and exit
#export HISTIGNORE='jobs:set -x:%1:%2'
export HISTIGNORE="&:[ ]*"
# share history across all terminals
PROMPT_COMMAND='history -a; history -c; history -r'
export HISTSIZE PROMPT_COMMAND
#}}}
# --- misc. visual enhancements {{{
# visual bell
set bell-style visible
eval $(dircolors -b ~/.dir_colors)
export GREP_COLOR="1;31"
# make less more friendly for non-text input files, see lesspipe(1)
[ -x $HOME/.local/bin/lesspipe.sh ] && eval "$($HOME/.local/bin/lesspipe.sh)"
#}}}
# --- functions {{{
# Note-taking macro courtsey jwr
n() {
local arg files=()
for arg; do
files+=( "/mnt/sda3/documents/0_notes/$arg" )
done
${EDITOR:-vi} "${files[@]}"
}
nls() {
tree -CR --noreport /mnt/sda3/documents/0_notes | awk '{
if (NF==1) print $1;
else if (NF==2) print $2;
else if (NF==3) printf " %s\n", $3
}' | $VIEWER
}
# TAB completion for notes
_notes() {
local files=(/mnt/sda3/documents/0_notes/**/"$2"*)
[[ -e ${files[0]} ]] && COMPREPLY=( "${files[@]##/mnt/sda3/documents/0_notes}" )
}
complete -o default -F _notes n
# calc - simple calculator
# usage: calc <equation>
calc() { echo "$*" | bc; }
Ex() {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.tar.xz) tar xJf $1 ;;
*.tar.lzma) tar --lzma -xf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar 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 ;;
*.xz) unxz $1 ;;
*.exe) cabextract $1 ;;
*.lha) lha e $1 ;;
*) echo "\`$1': unrecognized archive type" || return 1 ;;
esac
else
echo "\`$1' is not a valid file"
fi
}
pack() {
local FILE
FILE=$1
case $FILE in
*.tar.bz2) shift && tar cjf $FILE $* ;;
*.tbz2) shift && tar cjf $FILE $* ;;
*.tar.gz) shift && tar czf $FILE $* ;;
*.tgz) shift && tar czf $FILE $* ;;
*.zip) shift && zip $FILE $* ;;
*.rar) shift && rar $FILE $* ;;
esac
}
# nwf stands for "notify when finished"
nwf() {
"$@"
if [ $? -gt 0 ]; then
notify-send -u critical "$1 FAILED"
else
notify-send -u normal "$1 finished successfully"
fi
}
signpkgs() {
for pkg in *.pkg.tar.xz; do gpg --detach-sign --default-key 962DDE58 $pkg; done
}
signpkg() {
gpg --detach-sign --default-key 962DDE58 $1
}
tmprmhome() {
find ~/ -name ".bash_history.tmp" -delete
}
tmprmsda2() {
find /mnt/sda2 -name ".bash_history.tmp" -delete
}
tmprmsda3() {
find /mnt/sda3 -name ".bash_history.tmp" -delete
}
usrsyms() {
find /usr/share -type l ! -exec test -r {} \; -print
}
# navigation
export MARKPATH=$HOME/.marks
function j {
cd -P "$MARKPATH/$1" 2>/dev/null || echo "No such mark: $1"
}
function m {
mkdir -p "$MARKPATH"; ln -s "$(pwd)" "$MARKPATH/$1"
}
function mx {
rm -i "$MARKPATH/$1"
}
function ms {
ls -l "$MARKPATH" | sed 's/ / /g' | cut -d' ' -f8- | sed 's/ -/\t-/g' && echo
}
_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 j mx
#}}}
# --- enhanced completion function {{{
# This is a 'universal' completion function - it works when commands have
#+ a so-called 'long options' mode , ie: 'ls --all' instead of 'ls -a'
# Needs the '-o' option of grep
#+ (try the commented-out version if not available).
# First, remove '=' from completion word separators
#+ (this will allow completions like 'ls --color=auto' to work correctly).
COMP_WORDBREAKS=${COMP_WORDBREAKS/=/}
_get_longopts() {
#$1 --help | sed -e '/--/!d' -e 's/.*--\([^[:space:].,]*\).*/--\1/'| \
#grep ^"$2" |sort -u ;
$1 --help | grep -o -e "--[^[:space:].,]*" | grep -e "$2" |sort -u
}
_longopts() {
local cur
cur=${COMP_WORDS[COMP_CWORD]}
case "${cur:-*}" in
-*) ;;
*) return ;;
esac
case "$1" in
\~*) eval cmd="$1" ;;
*) cmd="$1" ;;
esac
COMPREPLY=( $(_get_longopts ${1} ${cur} ) )
}
complete -o default -F _longopts configure bash
complete -o default -F _longopts wget id info a2ps ls recode
_tar() {
local cur ext regex tar untar
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
# If we want an option, return the possible long options.
case "$cur" in
-*) COMPREPLY=( $(_get_longopts $1 $cur ) ); return 0;;
esac
if [ $COMP_CWORD -eq 1 ]; then
COMPREPLY=( $( compgen -W 'c t x u r d A' -- $cur ) )
return 0
fi
case "${COMP_WORDS[1]}" in
?(-)c*f)
COMPREPLY=( $( compgen -f $cur ) )
return 0
;;
+([^Izjy])f)
ext='tar'
regex=$ext
;;
*z*f)
ext='tar.gz'
regex='t\(ar\.\)\(gz\|Z\)'
;;
*[Ijy]*f)
ext='t?(ar.)bz?(2)'
regex='t\(ar\.\)bz2\?'
;;
*)
COMPREPLY=( $( compgen -f $cur ) )
return 0
;;
esac
if [[ "$COMP_LINE" == tar*.$ext' '* ]]; then
# Complete on files in tar file.
#
# Get name of tar file from command line.
tar=$( echo "$COMP_LINE" | \
sed -e 's|^.* \([^ ]*'$regex'\) .*$|\1|' )
# Devise how to untar and list it.
untar=t${COMP_WORDS[1]//[^Izjyf]/}
COMPREPLY=( $( compgen -W "$( echo $( tar $untar $tar \
2>/dev/null ) )" -- "$cur" ) )
return 0
else
# File completion on relevant files.
COMPREPLY=( $( compgen -G $cur\*.$ext ) )
fi
return 0
}
complete -F _tar -o default tar
_make() {
local mdef makef makef_dir="." makef_inc gcmd cur prev i;
COMPREPLY=();
cur=${COMP_WORDS[COMP_CWORD]};
prev=${COMP_WORDS[COMP_CWORD-1]};
case "$prev" in
-*f)
COMPREPLY=($(compgen -f $cur ));
return 0
;;
esac;
case "$cur" in
-*)
COMPREPLY=($(_get_longopts $1 $cur ));
return 0
;;
esac;
# ... make reads
# GNUmakefile,
# then makefile
# then Makefile ...
if [ -f ${makef_dir}/GNUmakefile ]; then
makef=${makef_dir}/GNUmakefile
elif [ -f ${makef_dir}/makefile ]; then
makef=${makef_dir}/makefile
elif [ -f ${makef_dir}/Makefile ]; then
makef=${makef_dir}/Makefile
else
makef=${makef_dir}/*.mk # Local convention.
fi
# Before we scan for targets, see if a Makefile name was
#+ specified with -f.
for (( i=0; i < ${#COMP_WORDS[@]}; i++ )); do
if [[ ${COMP_WORDS[i]} == -f ]]; then
# eval for tilde expansion
eval makef=${COMP_WORDS[i+1]}
break
fi
done
[ ! -f $makef ] && return 0
# Deal with included Makefiles.
makef_inc=$( grep -E '^-?include' $makef |
sed -e "s,^.* ,"$makef_dir"/," )
for file in $makef_inc; do
[ -f $file ] && makef="$makef $file"
done
# If we have a partial word to complete, restrict completions
#+ to matches of that word.
if [ -n "$cur" ]; then gcmd='grep "^$cur"' ; else gcmd=cat ; fi
COMPREPLY=( $( awk -F':' '/^[a-zA-Z0-9][^$#\/\t=]*:([^=]|$)/ \
{split($1,A,/ /);for(i in A)print A[i]}' \
$makef 2>/dev/null | eval $gcmd ))
}
complete -F _make -X '+($*|*.[cho])' make gmake pmake
_killall() {
local cur prev
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
# Get a list of processes
#+ (the first sed evaluation
#+ takes care of swapped out processes, the second
#+ takes care of getting the basename of the process).
COMPREPLY=( $( ps -u $USER -o comm | \
sed -e '1,1d' -e 's#[]\[]##g' -e 's#^.*/##'| \
awk '{if ($0 ~ /^'$cur'/) print $0}' ))
return 0
}
complete -F _killall killall killps
# Local Variables:
# mode:shell-script
# sh-shell:bash
# End:
#}}}
# vim:ft=sh: