-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.bashrc
405 lines (350 loc) · 9.08 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
up() {
local cnt=${1:-1}
local old_wd=$PWD
while [ "$cnt" -gt 0 ]; do
cnt=$((cnt - 1))
cd ../
done
OLDPWD=$old_wd
}
cpath()
{
realpath "${1:-.}" | tee >(cat 1>&2) | xclip -in -selection clipboard
}
dr()
{
cd "$(dirname "$(realpath "$1")")"
}
man()
{
local env=
[ $COLUMNS -gt 80 ] && env="COLUMNS=80"
env $env man "$@"
}
_emacsclients()
{
local filespec
while read -r filespec; do
local file=$(sed 's|:[0-9].*||' <<< "$filespec")
local linecol=$(sed -nr 's|^[^:]+:([0-9:]*).*$|+\1|p' <<< "$filespec")
emacsclient "${opts[@]}" $linecol "$file" & disown
done
}
# This can either take a single argument (to accomodate the ec usage below, it
# also takes options starting with a dash), or read an arbitrary number of lines
# from stdin. The input is matched for filename[:line[:colunm]]. The remainder
# of each line is discarded which allows to pipe the output of grep -n to this
# function.
e()
{
local opts=()
while grep -q "^-" <<< "$1"; do
opts+=("$1")
shift
done
if [ $# -gt 0 ]; then
echo "$@" | _emacsclients
else
_emacsclients
fi
}
ec()
{
e -c "$@"
}
# TODO: [compare]: vimdiff <(filter file1) <(filter file2)
# Streamline the `vim $(which foo)` pattern.
see()
{
local path filter
path=$(which "$1") || return
case "$(file "$path")" in
*text*)
# Hack to load e.g. the 'e' function.
filter=". ~/.bashrc; $EDITOR %"
;;
*executable*)
# TODO: also, readelf -a; perhaps allow to select (interactively?)
filter="ls -l --color=auto %; file %"
;;
*data*)
filter="hexdump -C %"
;;
esac
echo "$path" | xargs -o -I % "$SHELL" -c "$filter"
}
# Create a fresh temp directory for today and copy its name to the clipboard.
# cd there if an argument is passed.
tmp()
{
dir=/tmp/$(date +%m-%d)
echo "$dir"
if $(which xclip &>/dev/null); then
printf " $dir " | xclip -in -selection clipboard
fi
mkdir -p $dir
[ -n "$1" ] && cd "$dir"
}
tokib()
{
local x
read -r x
echo $((x / 1024))
}
tomib()
{
local x
read -r x
echo $(( $(echo $x | tokib) / 1024))
}
togib()
{
local x
read -r x
echo $(( $(echo $x | tomib) / 1024))
}
# Kill all background processes of this shell except for the given space-separated list.
killexcept()
{
IFS=' ' local except=" $* "
local tmpfile=/tmp/killer-$$
# We cannot use a pipe here, because disowning won't work since some context
# will not be the same inside the pipe. So we use a temp file.
jobs | awk -F '[\]\[]' '{ print $2 }' > $tmpfile 2> /dev/null
while read jobnum
do
case "$except" in
*" $jobnum "* )
# not killing $jobnum
;;
*)
kill "%$jobnum"
disown "%$jobnum"
;;
esac
done < $tmpfile 2> /dev/null
rm $tmpfile
}
touch-script()
{
if [ ! -s "$1" ]; then
cat <<- EOF > "$1"
#!/bin/bash
die()
{
[ \$# -gt 0 ] && echo >&2 "\$@"
exit 1
}
EOF
fi
cat "$1"
cat - >> "$1"
chmod +x "$1"
touch "$1"
}
dounset()
{
unset LIBRARY_PATH
unset COLLECT_GCC
unset COLLECT_LTO_WRAPPER
unset OFFLOAD_TARGET_NAMES
unset COMPILER_PATH
unset COLLECT_GCC_OPTIONS
}
unalias gsh > /dev/null 2>&1
gsh()
{
git show "${@:-HEAD}"
}
ansi_colored()
{
local text="$1"
local color="$2"
local start="\[\033[01;${color}m\]"
local end="\[\033[00m\]"
printf "%s%s%s" "$start" "$text" "$end"
}
error_code()
{
local e=$?
(( e )) && printf "%s|" "$e"
return $e
}
# See http://eli.thegreenplace.net/2013/06/11/keeping-persistent-history-in-bash
# for details.
#
# Note, HISTTIMEFORMAT has to be set and end with at least one space; for
# example:
#
# export HISTTIMEFORMAT="%F %T "
#
# If your format is set differently, you'll need to change the regex that
# matches history lines below.
log_bash_persistent_history()
{
[[
$(history 1) =~ ^\ *[0-9]+\ +([^\ ]+\ [^\ ]+)\ +(.*)$
]]
local date_part="${BASH_REMATCH[1]}"
local command_part="${BASH_REMATCH[2]}"
if [ "$command_part" != "$PERSISTENT_HISTORY_LAST" ]
then
echo $date_part "|" "$command_part" >> ~/.persistent_history
export PERSISTENT_HISTORY_LAST="$command_part"
fi
}
# Stuff to do on PROMPT_COMMAND
run_on_prompt_command()
{
log_bash_persistent_history
}
path_remove_dups()
{
echo $1 | awk -F: '
{
result = ""
for (i = 1; i <= NF; i++)
if (!seen[$i]) {
seen[$i] = 1
result = result ":" $i
}
print result
} ' | sed -e 's|:\{0,\}\(.*\):\{0,\}|\1|'
}
PROMPT_COMMAND="run_on_prompt_command"
export HISTTIMEFORMAT="%F %T "
# This should go after other modifications of PROMPT_COMMAND (or these other
# modifications should append rather than rewrite the variable).
. /home/vlad/bin/z/z.sh
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
export PATH=$(path_remove_dups $PATH)
# User specific aliases and functions
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
export EDITOR=vim
export ALTERNATE_EDITOR=""
export LSCOLORS="ExGxcxdxCxegedabagacad"
export CLICOLOR=YES
export LC_TIME=C.UTF-8
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=15000
HISTFILESIZE=20000
# only ignore duplicates, do not ignore space-prefixed commands
HISTCONTROL=ignoredups
# 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)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# Assume we have colorful terminal. (Else use this: PS1='[\u@\h \W]\$ ')
PS1='$(error_code)'
PS1="$PS1$(ansi_colored '\u@\h' 32)"
PS1="$PS1$(ansi_colored '\w' 34)\$ "
# TODO: look for another way to test that ls and grep have the --color option
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='grep -E --color=auto'
alias cgrep='grep --color=always'
fi
# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias L='less -R'
alias tre='pstree -apl | less'
alias j='jobs'
alias v='vim'
alias vi='vim'
alias ta='tmux a -t'
alias td='tmux detach'
alias tn='tmux new-session -s'
alias tls='tmux ls'
alias cln='git clone'
alias gs='git status'
alias gsn='git status --untracked=no'
alias gl='git log'
alias gd='git diff'
alias gg='git grep -n'
alias rg='repo grep -n'
alias amend='git commit --amend'
alias gds='git diff --staged'
alias gsd='git diff --staged'
alias config='git --git-dir=$HOME/.dotfiles.git --work-tree=$HOME'
alias cdd='cd `pwd`'
alias qwer='cd /home/ivladak/src/gcc-gomp'
alias bd="$EDITOR ~/bin/build-default.sh"
alias rc="$EDITOR ~/.bashrc"
alias svim="sudo -E $EDITOR"
alias rcre=". ~/.bashrc"
alias ga='gdb --args'
alias oh='objdump -h'
alias re='readelf -aW'
alias trns="tr '\n' ' ' && echo"
alias trsn="tr ' ' '\n'"
alias phgrep='cat ~/.persistent_history|grep --color'
alias hgrep='history|grep --color'
alias psgrep="ps -A | grep "
alias crontab='crontab -i'
alias sshowfind='showfind -s'
alias mplayer='mplayer -af scaletempo'
alias yt='yt-dlp --cookies-from-browser chromium:~/.local/share/qutebrowser'
alias yts='yt -o "%(playlist_index)s:%(title)s.%(ext)s" &>/dev/null'
alias ytp='yt -f bestvideo.2+bestaudio --proxy socks://127.0.0.1:5050 &>/dev/null'
alias fej='find . -type f -print0 -iname \*.jpg -o -iname \*.jpeg | sort -z | xargs -0 feh -Z --auto-rotate --scale-down'
alias clip="xclip -in -selection clipboard"
alias def=sdcv
alias ]=xdg-open
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
essential-defs()
{
{
for d in ll la l L j \
up cdd \
cgrep \
cln gs gsn gl gd gds gsd gg gsh \
oh re; do
case $(type $d | head -1) in
*'alias'*)
alias "$d"
;;
*'function'*)
declare -fp "$d"
;;
*)
echo >&2 "$d is neither alias nor function"
;;
esac
done
} | clip
}
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
. /etc/bash_completion
fi
IGNOREEOF=3
if ps -ef | grep tmux | grep $(id -un) | grep -v grep > /dev/null
then
tmux ls
fi
. ~/.bashrc-teach
# These are supposed to be really small; they are not git-controlled.
if [ -f ~/.bashrc-local ]; then
. ~/.bashrc-local
fi