-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bashrc
541 lines (435 loc) · 12.9 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#
# GMJ .bashrc
#
# 2020-11-15
#
# Don't do this unless you are testing.
#
# set -e
# Helper functions
#PROG=`basename "$0" | tr -d '\n'`
PROG="bashrc" # $0 not piking it up right for .bashrc
PROG='.bashrc'
function info() { echo ${PROG}\: info: "$@" 1>&2; }
function warn() { echo ${PROG}\: warning: "$@" 1>&2; }
function error() { echo ${PROG}\: error: "$@" 1>&2; }
function debug() { [[ -v DEBUG ]] && echo ${PROG}\: debug: "$@" 1>&2 || true ; }
function die() { echo ${PROG}\: fatal: "$@" 1>&2 && exit 1; }
#
# Generic things
#
export PS1="\# [\t] \u@\h \W/ $ "
#bind -x '"\C-l": clear;'
alias rm=' rm -i'
alias ag=' alias | grep -i'
alias eg=' printenv | grep -i'
alias hg=' history | grep -i'
alias ht=' history | tail'
alias fpg=' find . -print | egrep -i'
alias egi=' egrep -i'
alias oi=' offlineimap'
alias psg=' /bin/ps -auxww | grep'
alias p8=' ping -c 3 8.8.8.8' # make sure routing works
alias pp=' ping -c 3 port111.com' # make sure dns and routing work
if [ -f /usr/bin/locate ]; then
alias locate='locate -r'
elif [ -f /usr/bin/mlocate ]; then
alias locate='mlocate -r'
else
warn no locate command. Install\?
fi
#
# cd functions that list the directory stack
#
function dirl() {
# "DIR"ectory "L"ist directory stack, one per line
# Usage: dirl
for d in `dirs`; do echo $d; done
}
function dirc() {
# "DIR"ectory "C"onnect - connect to directory and list stack
# Usage: dirc [DIR
pushd ${1:-"$HOME"} > /dev/null
dirl
}
function dirp () {
# "DIR"ectory "P"op - pop N entries off the directory stack
# Usage: dirp [N]
#
# OLD:
# alias dirp='popd > /dev/null && dirl'
for i in `seq ${1:-"1"}`; do
debug "dirl: popd. i is $i"
popd > /dev/null;
done
dirl
}
alias cd=dirc
#
# aliases (functions) that take args
#
function gf() {
# grep-find: grep for patterins in files via find
#
# Usage: gf patterns [files [days]]
#
# Examples:
# gf findMeAnywhere
# gf findMeInTextFiles '*.txt'
# gf findMeInTextFiles .txt
# gf BEGIN\|END .org 30
local files=""
local days="365"
set -o noglob
# $1 is pattern(s) for egrep
if [ -z ${1+x} ]; then
echo 'gf needs string(s) to search for ' 1>&2
info "Usage: gf patterns [files [days]]"
return 1
fi
# $2 (if present) is files for find. No globbing, so "*.txt" OK
if [ ! -z ${2+x} ]; then
if [[ "$2" =~ ^\. ]]; then
# Special case: treat ".foo" as "*.foo"
# Avoids needing to quote on command line
files="-name *$2"
else
files="-name ${2}"
fi
fi
# $3 (if present) is find -mtime arg, default 365
if [ ! -z ${3+x} ]; then
days="${3}"
fi
# set -x
find . -type f -mtime -${days} $files -exec egrep --color -H -i "${1}" \{\} \;
# set +x
set +o noglob
}
function hgt() {
# hgt == "history grep (for arg) tail"
#echo "Histroy Grep tail"
if [ -z ${1+x} ]; then
echo 'hgt needs an argument' 1>&2
return 1
fi
history | grep -i "$1" | tail
return 0
}
# Set HOSTNAME if ~/etc/hostname exists
if [ -e ${HOME}/etc/hostname ]; then
export HOSTNAME=`cat ${HOME}/etc/hostname`
elif [ -e /etc/hostname ]; then
export HOSTNAME=`cat /etc/hostname`
else
export HOSTNAME="unknown"
fi
# Set timezone if ~/bin/tz.sh exists
if [ -e ~/bin/tz.sh ]; then
echo Setting timezone.
source ~/bin/tz.sh
fi
# Set local for numeric output
LOCAL=`locale -a | grep -i en_us | head -1`
if [[ "$LOCAL" != "" ]]; then export LC_NUMERIC="$LOCAL"; fi
# set up ssh agent
#
# Add keys by hand if needed via
#
# ssh-add ~/.ssh/id_*
if [ -e ~/bin/sshagent ]; then
#echo Starting SSH agent
source ~/bin/sshagent
fi
# git stuff
alias glm='git ls-files -m'
alias gam='git add `git ls-files -m`'
alias gcm='git commit -m'
alias gs='git status --ahead'
# Add git stuff to prompt
# http://thelucid.com/2008/12/02/git-setting-up-a-remote-repository-and-doing-an-initial-push/
function git-branch-name {
git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3
}
function git-branch-prompt {
local branch=`git-branch-name`
if [ $branch ]; then printf " [%s]" $branch; fi
}
#
# Because MacOS has to be different...
#
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
# ...
alias 2clip='xclip -selection c'
alias 3clip='printf %s "$(cat /dev/stdin)" | xclip -selection c' # no final \n
elif [[ "$OSTYPE" == "darwin"* ]]; then
alias 2clip='pbcopy'
# elif [[ "$OSTYPE" == "cygwin" ]]; then
# # POSIX compatibility layer and Linux environment emulation for Windows
# elif [[ "$OSTYPE" == "msys" ]]; then
# # Lightweight shell and GNU utilities compiled for Windows (part of MinGW)
# elif [[ "$OSTYPE" == "win32" ]]; then
# # I'm not sure this can happen.
# elif [[ "$OSTYPE" == "freebsd"* ]]; then
# # ...
# else
# Unknown.
fi
# because hosnames assigned by IT deperments like abc123456789ef are not meaningful
function prompt-hostname {
if [ -f ~/etc/hostname ]; then head -1 ~/etc/hostname; elif [ -f /etc/hostname ]; then head -1 /etc/hostname; else echo STUPIDMAC; fi
}
export PS1="\# [\t] \u@\h \W/\$(git-branch-prompt) $ "
PS1="\u@\$(prompt-hostname) \[\033[0;36m\]\W\[\033[0m\]\[\033[0;32m\]\$(git-branch-prompt)\[\033[0m\] \$ "
# Preserve history across sesssions
#
# http://unix.stackexchange.com/questions/1288/preserve-bash-history-in-multiple-terminal-windows
#
export HISTCONTROL=ignoredups:erasedups # no duplicate entries
export HISTSIZE=100000 # big big history
export HISTFILESIZE=100000 # big big history
shopt -s histappend # append to history, don't overwrite it
export IGNOREEOF=5 # Don't log by accedent with ^D
# Save and reload the history after each command finishes
#export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
export PROMPT_COMMAND="history -a; history -c; history -r;"
# Useful functions
# remove an item from the path
pathrm() {
if [ -d "$1" ]; then
#echo 1 $1
removeThis="`echo $1 | sed -e 's#/#\\\/#'g`"
newPath=`echo $PATH | awk -v RS=: -v ORS=: "/$removeThis/ {next} {print}" | sed 's/[ :]*$//g'`
export PATH=$newPath
fi
}
# add path to the end if not there
pathlast() {
if [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then
#echo pathlast $1
export PATH="${PATH:+"$PATH:"}$1"
fi
}
# add path to the front if not there
pathfirst() {
if [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then
#echo pathfirst $1
export PATH="$1:${PATH}"
fi
}
# show path
path() {
echo $PATH
}
# show path, one entry per line
alias pathcat="echo $PATH | sed 's/:/\n/g'"
# Be sure we have a few specific paths if they exist
pathlast $HOME/bin
pathlast /opt/bin
pathlast /usr/local/bin
pathlast /opt/bin
pathlast $HOME/.local/bin
#
# Execute any .sh files in ~/rc.local/*.sh
#
if [ -d ${HOME}/rc.local ]; then
for rcfile in $(find ${HOME}/rc.local -name \*.sh); do
#echo running localrc ${rcfile}
source ${rcfile}
done
fi
#
# Invoking emacs
#
alias emacs='setsid emacs'
# from http://stuff-things.net/2014/12/16/working-with-emacsclient/
if [ -z "$SSH_CONNECTION" ]; then
export EMACSCLIENT=emacsclient
alias ec="$EMACSCLIENT -c -n"
export EDITOR="$EMACSCLIENT -c"
export ALTERNATE_EDITOR=""
else
export EDITOR=$(type -P emacs || type -P ed)
fi
export VISUAL=$EDITOR
# http://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script
# if hash emacsclient 2>/dev/null; then
# # http://stackoverflow.com/questions/5570451/how-to-start-emacs-server-only-if-it-is-not-started
# export ALTERNATE_EDITOR="" # Because I should never have to start emacs
# export VISUAL="emacsclient -t"
# export EDITOR="emacsclient -t"
# alias e='[ "$DISPLAY" == ""] && emacsclient -t || emacsclient -c'
# elif hash emacs 2>/dev/null; then
# export ALTERNATE_EDITOR="" # Because I should never have to start emacs
# export VISUAL="emacs"
# export EDITOR="emacs"
# alias e='[ "$DISPLAY" == ""] && emacsclient -t || emacsclient -c'
# else
# 2>& echo BOO no emacs here
# export ALTERNATE_EDITOR="" # Because I should never have to start emacs
# export VISUAL=""
# export EDITOR=""
# unalias e 2>/dev/null || true
#
# fi
#
# Do OS-specific setup
#
#
# ls aliases
#
# coloring for ls functions
if [[ "$OSTYPE" == "linux-gnu" ]]; then
# This works on terminals, piped to cat, tail etc
# This fails piped to more, less etc
# color="--color";
#
# Always omits color
# color="--color=never"
#
# This works when piped to less, more
# color="--color=auto"
#
# Probably want different settings of 'color='
# based on intend output (terminal, more/less).
#
# Need to fix/test this gmj <2018-05-18>
color="--color";
#color=""
else
color=""
fi
BIN_LS=/bin/ls
alias ls=' ls '$color' -a'
#
# NOTE/TODO: <2018-07-10> commented out function versions of ls
# commands were an attempt at allowing different versions of ls
# for directories other than $PWD. Needs work. "ls *" fails.
#
# List
# Usage: ls [DIR]
# function ls {
# DIR=${1:-.};
# $BIN_LS $color -a $DIR;
# }
#alias llrt=' ls -ltr '$color' -a | tail'
# Long List Reverse Tail
# Usage: llrt [DIR]
# function llrt {
# DIR=${1:-.};
# $BIN_LS $color -a -ltr $DIR | tail
# }
unalias llrt 2> /dev/null
function llrt() { ls -lrt $color ${*:-}; }
#alias llt=' ls -lt '$color' -a'
# Long List Time
# Usage: llt [DIR]
# function llt {
# set -x
# echo LLT2
# DIR=${1:-.}
# $BIN_LS $color -a -lt $DIR
# set +x
# }
unalias llt 2> /dev/null
function llt() { ls -lt $color ${*:-}; }
#alias lltm=' ls '$color' -a -lt | more'
# Long List Time, More
# Usage: lltm [DIR]
# function lltm {
# DIR=${1:-.};
# $BIN_LS $color -a -lt $DIR | more;
# }
unalias llm 2> /dev/null
function lltm() { ls -lt $color ${*:-} | more; }
# alias lltl=' ls '$color' -a -lt | less'
# Long List Time, Less
# Usage: lltl [DIR]
# function lltl {
# DIR=${1:-.};
# $BIN_LS $color -a -lt $DIR | less;
# }
unalias lltl 2> /dev/null
function lltl() { ls -alt $color ${*:-} | more; }
#alias llth=' ls '$color' -a -lt | head'
# Long List Time, Head
# Usage: llth [DIR [LINES]]
# function llth {
# DIR=${1:-.};
# LINES=${2:-10};
# $BIN_LS $color -a -lt $DIR | head -$LINES;
# }
function llth() { ls -lt $color ${*:-} | head; }
# alias lltt=' ls '$color' -a -lt | tail'
# Long List Time, Tail
# Usage: lltt [DIR [LINES]]
# function lltt {
# DIR=${1:-.};
# LINES=${2:-10};
# $BIN_LS $color -a -lt $DIR | tail -$LINES;
# }
unalias lltt 2> /dev/null
function lltt() { ls -alt $color ${*:-} | tail; }
#alias lss=' ls '$color' -a -1s | sort -n'
# List Sort Size
# Usage: lss [DIR]
# function lss {
# DIR=${1:-.};
# $BIN_LS $color -a -1s $DIR | sort -n
# }
unalias lss 2> /dev/null
function lss() { ls -a1s $color ${*:-} | sort -n; }
#alias lssr=' ls '$color' -a -1s | sort -nr'
# List Sort Size Reverse
# Usage: lssr [DIR]
# function lssr {
# DIR=${1:-.};
# $BIN_LS $color -a -1s $DIR | sort -nr
# }
unalias lssr 2> /dev/null
function lssr() { ls -a1s $color ${*:-} | sort -nr; }
#
# Aliases for viewing the newest file in a directoy
#
# list the newest file in the current directory
# TODO: need to handle spaces in filenames
function nf ()
{
NF=`find ${1:-.} -maxdepth 1 -type f -print0 | xargs -0 ls -1t | head -1;`;
echo ${NF:-/dev/null} | sed "s/ /\\\ /g"
}
#alias nftf='tail -f `nf`' # tail follow newest file
unalias nfh 2> /dev/null
function nftf { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs tail -f ; }
#alias nft='tail `nf`' # tail newest file
unalias nft 2> /dev/null
function nft { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs tail ; }
#alias nfh='head `nf`' # head newest file
unalias nfh 2> /dev/null
function nfh { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs head ; }
#alias nfl='less `nf`' # less newest file
unalias nfl 2> /dev/null
function nfl { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs less ; }
#alias nfc='cat `nf`' # cat newest file
unalias nfc 2> /dev/null
function nfc { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs cat ; }
#alias nfls='ls -A1t `nf`' # ls newest file, excluding .
unalias nfls 2> /dev/null
function nfls { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs ls -A1t ; }
#alias nflsl='ls -Atl `nf`' # ls newest file, long
unalias nflsl 2> /dev/null
function nflsl { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs ls -Atl ; }
# alias for viewing files
#
# Notes on setting up file/mime type assiciatons
# https://unix.stackexchange.com/questions/77136/xdg-open-default-applications-behavior
#
# So...
#
# locate -r 'emacs.*\.desktop'
# xdg-mime default emacs.desktop text/plain
#
if [[ ! -z "`which xdg-open`" ]]; then alias open='xdg-open '; fi
# Let somebody know we finished running
touch $HOME/.bashrc-ran
debug ".bashrc done"