-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash_config
471 lines (382 loc) · 11.5 KB
/
.bash_config
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
#!/bin/bash
## .bashrc versionned config ##
## How to use : add this to .bashrc
# [ -f ~/.bash_config ] && . ~/.bash_config
## from debian ##
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# don't put duplicate lines in the history. See bash(1) for more options
export HISTCONTROL=ignoredups
# more history
export HISTSIZE=100000
export HISTFILESIZE=100000
# append to the history file, don't overwrite it
shopt -s histappend
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# make less more friendly for non-text input files, see lesspipe(1)
if [ -x /usr/bin/lesspipe ]; then
eval "$(lesspipe)"
fi
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (no color if dumb term)
case "$TERM" in
dumb)
PS1='[${debian_chroot:+($debian_chroot)}\u@\h \w][\A]$ '
;;
*)
DEFAULT="\[\033[0m\]"
BOLD="\[\033[1m\]"
UNDERLINE="\[\033[4m\]"
BLUE="\[\033[1;34m\]"
LIGHT_BLUE="\[\033[0;34m\]"
RED="\[\033[1;31m\]"
LIGHT_RED="\[\033[0;31m\]"
if [ -f ~/git/kube-ps1/kube-ps1.sh ]; then
source ~/git/kube-ps1/kube-ps1.sh
export KUBE_PS1_BINARY=$(which kubectl) # to bypass alias kubecolor
EXTRA_PS1='$(kube_ps1)'
fi
PS1="[\${debian_chroot:+(\$debian_chroot)}$BLUE\u$DEFAULT@$UNDERLINE\h$DEFAULT \w][\t](\$(rc=\$?; [ \$rc -eq 0 ] && echo \$rc || echo \"$RED\$rc$DEFAULT\"))$EXTRA_PS1\$ "
;;
esac
export PS1
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"'";${PROMPT_COMMAND:-:}"
;;
*)
;;
esac
# enable color support of ls and grep
if [ "$TERM" != "dumb" ]; then
eval "`dircolors -b`"
alias ls='ls --color=auto'
alias grep='grep --color=auto'
fi
## alias and functions ##
# emacs
export ALTERNATE_EDITOR="" # to tell emacsclient to start emacs --daemon and then connect to it if it is not already started; currently redundant wi all the -a "" that should be removed (after testing)
function emacs-client
{
FRAME_OPTIONS="--no-wait -c"
echo -- $1 --
if [[ "$1" == "--tty" || "$1" == "-t" || "$1" == "-nw" ]]; then
FRAME_OPTIONS="$1"
shift
fi
# if only one argument, and is a grep result, open file on the right line
if [ $# -eq 1 ]; then
local fileline="$1"
if echo "$fileline" | grep -q '[^:]*:[0-9]\+\(:.*\)\?$'; then
local file="$(echo "$fileline" | awk -F: '{print $1}')"
local line="$(echo "$fileline" | awk -F: '{print $2}')"
emacsclient -a "" +$line ${FRAME_OPTIONS} "$file"
return
fi
fi
emacsclient -a "" ${FRAME_OPTIONS} "$@"
}
function ec
{
if [ -z "$DISPLAY" ]; then
# no x term
emacs-client --tty "$@"
else
# x term: get focus, and don't wait
if [ -z "$*" ]; then
emacs-client -e "(select-frame-set-input-focus (selected-frame))" > /dev/null &
else
emacs-client "$@" > /dev/null &
fi
fi
}
function ecs
{
file=$(readlink -f "$1")
ec "/sudo::$file"
}
alias e='ec'
alias ect='emacsclient -a "" --tty'
export EDITOR='emacsclient -a "" -c'
alias vi='ec'
alias vim='ec'
alias eg='ec -e "(select-frame-set-input-focus (selected-frame))" -e "(magit-status)"'
alias mg='eg'
alias d='ec doc.org'
# diff: force color=auto
alias diff='diff --color=auto'
# gdb
alias gdb='gdb --args'
# ps
function ps_grep ()
{
processes=$(ps aux)
echo "$processes" | grep "$@"
}
function ps_igrep ()
{
ps_grep -i "$@"
}
alias p='ps_igrep'
alias pp='ps_grep'
# alias autres
alias c='cd ..'
alias ..='cd ..'
# rsync
alias rs='rsync -avzP'
# svn
alias sd='svn diff --diff-cmd colordiff'
function sdl { sd $@ | less -RXF; }
# git
alias ga='git add'
alias gb='git branch'
alias gba='git branch -a'
alias gc='git commit'
alias gcm='git commit -m'
alias gd='git diff --color'
alias gdc='git diff --color --cached'
#alias gf='git fetch' # gf used for grep + find
alias gu='gf && gr'
alias gp='git push'
alias gr='git rebase origin/master'
alias gst='git status | less -RXF'
alias gl='git log'
alias gk='gitk --all&'
# bash history
export HISTSIZE=5000
export HISTFILESIZE=5000
# divers
alias o='xdg-open'
alias la='ls -A'
alias l='ls -latrh'
alias ll='ls -l'
alias lll='l --full-time'
alias lh='ls -latr'
alias lS='ls -laSrh'
function last { ls -t "$@" | head -1; }
function cl { cd $(last "$@"); }
export LESSQUIET=1
alias le='less -RXF'
function llast { le $(last "$@"); }
## Grep customization ##
function declare_grep_exclude_arguments() {
# outputs exclude options for grep, according to global & local exclude config files
#TODO recursively find ignore files in parent directories
local __grep_exclude_arguments=()
while read -r pattern; do
[[ $pattern == '#'* || $pattern == '' ]] && continue
__grep_exclude_arguments+=("--exclude" "$pattern")
done < <(cat {$HOME,$PWD}/.grepignore 2> /dev/null)
while read -r pattern; do
[[ $pattern == '#'* || $pattern == '' ]] && continue
__grep_exclude_arguments+=("--exclude-dir" "$pattern")
done < <(cat {$HOME,$PWD}/.grepignore-dir 2> /dev/null)
# this outputs a definition of a bash array named "__grep_exclude_arguments" to be evaluated by the caller
declare -p __grep_exclude_arguments
}
export -f declare_grep_exclude_arguments
function grep_custom() {
eval $(declare_grep_exclude_arguments)
grep -nrIH --color "${__grep_exclude_arguments[@]}" "$@"
}
export -f grep_custom
alias gg='grep_custom'
alias g='gg -i'
alias gw='g -w'
alias ggw='gg -w'
alias gv='grep -v'
alias f='find . -iname'
function ff() { local keyword="$1"; shift; f "*$keyword*" "$@"; }
#TODO: handle grepignore for find too
function ggf() { local filename="$1"; shift; find . -name "$filename" -exec bash -c 'grep_custom "$@"' grep_custom "$@" {} \;; }
alias gf='ggf -i'
alias gcmake='gf CMakeLists.txt'
alias ggcmake='ggf CMakeLists.txt'
alias gdoc='gf doc.org'
alias ggdoc='ggf doc.org'
alias gpackage='gf package.json'
alias ggpackage='ggf package.json'
# sift
alias s='n sift --color'
alias scmake='s --files CMakeLists.txt --files "*.cmake"'
alias sdoc='s --files doc.org'
alias spackage='s --files package.json'
alias sdmake='s --files dmake.yml'
alias spy='s --files "*.py"'
# ripgrep
export RIPGREP_CONFIG_PATH=$HOME/.ripgreprc
alias r='rg'
alias rcmake='r --glob CMakeLists.txt --glob "*.cmake"'
alias rdoc='r --glob doc.org'
alias rpackage='r --glob package.json'
alias rdmake='r --glob dmake.yml'
alias rpy='r --glob "*.py"'
alias sc='screen -OURd'
alias sls='screen -ls'
alias dus='du -shc * | sort -h'
alias sum="awk '{sum+=\$1} END {print sum}'"
alias avg="awk '{sum += \$1} END {if (NR > 0) print sum / NR}'"
alias jsonlint='python -mjson.tool'
alias w='wc -l'
alias t='tail -F'
function tl { t -v $(last "$@"); }
alias ss='sshrc'
complete -F _ssh ss
export N_IONICE=3
export N_NICE=10
alias n='nice -n$N_NICE ionice -c $N_IONICE'
function nice_process() { for pid in $@; do sudo renice -n $N_NICE -p $pid ; sudo ionice -c $N_IONICE -p $pid ; done }
alias np='nice_process'
# valgrind
alias v_gdb='valgrind --leak-check=full --num-callers=50 --leak-check-heuristics=all --keep-stacktraces=alloc-and-free --soname-synonyms=somalloc=*tcmalloc* --suppressions=/home/riccardi/git/core-tmp/tools/valgrind/stl.supp --db-attach=yes'
alias vxml='valgrind --leak-check=full --num-callers=50 --leak-check-heuristics=all --soname-synonyms=somalloc=*tcmalloc* --suppressions=/home/riccardi/git/core-tmp/tools/valgrind/stl.supp --xml=yes --xml-file=valgrind.memcheck.$RANDOM.xml'
alias vmassif='valgrind --tool=massif --max-snapshots=1000 --threshold=0.1'
alias v='v_gdb'
alias vl='valkyrie -l'
alias cg='valgrind --tool=callgrind'
alias vh='valgrind --tool=helgrind'
# http save request
alias httpsave='http_proxy=localhost:8000'
# useless mais bon
alias jacadi='sudo'
alias siteuplait='sudo'
alias st='strace -f -s 256 -o strace.$$.out'
alias xml='xmllint --format'
alias xrestop-dump='xrestop -b -m 1 > xrestop-$(date +%s)'
# cd does a ls after a cd
# unless we are in /usr or /lib (lots of file in there...)
cd() {
builtin cd "$@"
# pas réussi à faire ça en un seul test...
if [ -z $(echo "$PWD" | grep "^/usr\|^/lib") ] && [ "$PWD" != "$HOME" ] && [ "$(ls -1 | wc -l)" -lt 200 ]; then
ls
fi
}
# un copy par ssh
# tar cvf - /tarred_up_director | ssh -l root Linux_Server 'cd /tmp ; tar xf -'
# multiline sed (from http://austinmatzko.com/2008/04/26/sed-multi-line-search-and-replace/)
sedml() {
+ sed -n '
# if the first line copy the pattern to the hold buffer
1h
# if not the first line then append the pattern to the hold buffer
1!H
# if the last line then ...
$ {
# copy from the hold to the pattern buffer
g
# do the search and replace
'"$2"'
# print
p
}
'
}
alias boobank='boobank -n 30'
# own bin
export PATH=$HOME/bin:$PATH
# Systran
# clean svn dir
#svn status | grep "^\?" | awk '{print $2}' | xargs rm -rf
export MAKE_IONICE=5 # default level is 4, so use 5 to have lower priority than default
export MAKE_PARALLEL=8
export MAKE_NICE=10 # default level is 0
alias nic='nice -n$MAKE_NICE ionice -c 2 -n $MAKE_IONICE'
function make_nice() { nic make -j$MAKE_PARALLEL "$@"; }
function ninja_nice() { nic ninja "$@"; }
alias m='time make_nice'
alias ni='time ninja_nice'
# init work directory
function mkwork {
local bug="$1"
local name="$2"
if [ -z "${name}" ]; then # TODO better test: if it starts with "id-" then it's the bug id: don't add the date
name=$bug
bug=$(date +%Y-%m-%d)
fi
local dir="${bug}-${name}"
mkdir -p "${dir}"
cd "${dir}"
# this is safe to do this multiple times
git init
git config --add magit.extension wip-save
if [ ! -f doc.org ]; then
cat > doc.org <<EOF
#+TITLE: refs ${bug}: ${name}
#+SETUPFILE: ~/Documents/theme-readtheorg.setup
* goal
* TODO
EOF
fi
git add doc.org
git commit -m "First notes"
ec doc.org&
}
# docker
alias dk='docker'
alias dc='docker-compose'
alias dcu='dc up --build -d'
alias de='docker exec -it'
alias drr='docker run -it --rm'
alias dce='de $(dc ps -q | head -1)'
alias dcb='dce bash'
alias di='d-info'
alias dip='d-ip'
function d-ip-cid () {
docker inspect "$@" | jq -r '.[].NetworkSettings.Networks | map(.) | .[].IPAddress'
}
function d-info-cid () {
docker inspect "$@" | jq -r '.[] | { id: .Id[0:12], name: .Name, ips: [ .NetworkSettings.Networks | map(.) | .[].IPAddress ] }'
}
function d-ps () {
local name=$1
shift
docker ps -f name=$name "$@"
}
function d-get-cid () {
d-ps "$@" -q
}
function d-ip () {
d-ip-cid $(d-get-cid "$@")
}
function d-info() {
d-info-cid $(d-get-cid "$@")
}
# kubernetes
alias k=kubectl
alias kx=kubectx
alias kn=kubens
export KUBECTX_IGNORE_FZF=1
alias kubectl='kubecolor --light-background'
complete -o default -F __start_kubectl kubecolor
# python
alias dmake_pdb='ipdb3 $(which dmake)'
# http server
function serve-http () {
local port=${1:-8080}
if ! docker run --rm --name serve-http-${port} -v $PWD/:/usr/share/nginx/html:ro -p ${port}:80 -d nginx; then
echo "Use another port: e.g. serve-http 8081"
return 1
fi
url=http://$(hostname -I | cut -d" " -f1):${port}
ls -1tr | while read -r f; do
echo $url/$f;
done
}
function serve-http-stop () {
local port=${1:-8080}
if ! docker stop -t0 serve-http-${port}; then
echo "Check running servers: serve-http-list"
return 1
fi
}
function serve-http-list () {
docker ps -f name=serve-http-
}
alias pw='pwgen -s 12 1'