-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbashrc.bash
More file actions
310 lines (273 loc) · 6.92 KB
/
bashrc.bash
File metadata and controls
310 lines (273 loc) · 6.92 KB
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
#shellcheck disable=1091,2155
### bash, readline
stty -ixon werase undef
set -o ignoreeof
shopt -s histverify
shopt -s globstar
#shellcheck disable=2016
bind -x '"\C-w":echo -n "${READLINE_LINE}" | xsel -ib'
# bind -x '"\C-w":echo -n "${READLINE_LINE}" | wl-copy'
#shellcheck disable=2016
bind -x '"\ew":pwd | sed "s%$HOME%\$HOME%" | tr -d '"'"'\n'"'"' | xsel -ib'
# bind -x '"\ew":pwd | sed "s%$HOME%\$HOME%" | tr -d '"'"'\n'"'"' | wl-copy'
# bind -x '"\C-l\C-k":clear;clear'
#shellcheck disable=2016
bind '"\emfl":"(IFS=$''\\n''; for line in ; do echo $line; done)\e4\eb\e2\C-b"'
### history
source "$HOME/dotfiles/bash/history.bash"
history_config
### LS_COLORS and other colors
eval "$(dircolors -b)"
export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
export JQ_COLORS='0;31:0;39:0;39:0;39:0;32:1;39:1;39'
### prompt
if "$HOME/dotfiles/bin/is_workstation"; then
#'[bold;foreground-color;rgb;R;G;B]working directory[reset]$ '
PS1='\[\e[01;38;2;159;151;216m\]\w\[\e[00m\]\$ '
else
PS1='\[\e[01;38;2;159;151;216m\]${SHORT_HOSTNAME:-$"HOSTNAME"}:\w\[\e[00m\]\$ '
fi
### Less
#squeeze blank lines, long prompt, ANSI colors, quit if one screen, ignore-case, padding 10 lines
export LESS='-s -M -R -F -i -j10'
export SYSTEMD_LESS="-M -R -i"
BOLD=$(tput bold)
# RED=$(tput setaf 1)
BLUE=$(tput setaf 4)
CYAN=$(tput setaf 6)
SGR0=$(tput sgr0)
export BOLD
export BLUE
export CYAN
export SGR0
export LESS_TERMCAP_md
export LESS_TERMCAP_us
export LESS_TERMCAP_ue
LESS_TERMCAP_md="${BOLD}${BLUE}" # begin bold
LESS_TERMCAP_us="$CYAN" # begin underline
LESS_TERMCAP_ue="$SGR0" # reset underline
### misc
export DEFAULT_IFS=\ $'\t'$'\n'
export EDITOR=vim
export PAGER=less
export MANPAGER="$PAGER"
### Aliases
alias clr='clear;clear'
###### systemctl
alias sc="sudo systemctl"
alias scn="sudo systemctl --no-pager"
alias scu="systemctl --user"
alias scun="systemctl --user --no-pager"
###### tmux
alias tmclr="rm .tmux/resurrect/*"
alias tso='tmux show -A'
alias tpv='tmux splitw -vdI &'
alias tph='tmux splitw -hdI &'
###### intalation from sources
alias makel='make PREFIX="$HOME/.local"'
alias cmig='mkdir Release && cd Release && cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$HOME/.local" .. && ccmake .'
###### other abbrevs
alias ls='ls --color=auto'
alias ll='ls -lAh'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
alias hd='hexdump'
alias hdh="hd -v -e '/1 \"%02X \"'"
alias r="source /usr/bin/ranger"
alias ffmpeg='ffmpeg -hide_banner'
alias cevt='cat -evt'
###### microscripts
alias sbrc='source "$HOME/.bashrc"'
alias ppath='echo -e "${PATH//:/\\n}"'
# alias tmpl='mktemp /tmp/XXXXXX.log | tr -d '"'"'\n'"'"' | xsel -ib'
alias tmpl='mktemp /tmp/XXXXXX.log | tr -d '"'"'\n'"'"' | wl-copy'
alias hcurl='curl -s -o /dev/null -D -'
alias cul='curl -L'
### Functions
function has_cmd {
type -t "$1" >/dev/null
}
function bytes {
local -r hd_args=(-v -e '/1 "%02X "')
if [[ "$#" = "0" ]]; then
#input on stdin
hd "${hd_args[@]}"
elif [[ -r "$1" ]]; then
#filename passed as arg
hd "${hd_args[@]}" "$1"
else
#value passed as arg
echo -n "$1" | hd "${hd_args[@]}"
fi
echo
}
function cod {
if [[ "$#" = "0" ]]; then
# local a="$(xsel -ob)"
local a="$(wl-paste)"
printf "%s %x" "$a" "'$a"
else
printf %x "'$1"
fi
echo
}
function cl {
#shellcheck disable=2164
cd "$@"
ll
}
function mc {
mkdir "$1"
#shellcheck disable=2164
cd "$1"
}
function __faketty {
script -qfc "$(printf "%q " "$@")" /dev/null
}
#compare binary files
function bindiff {
test -n "$1" || {
echo "No input files" >&2
return 1
}
test -n "$2" || {
echo "No second input file" >&2
return 1
}
diff -u --color=always <(xxd "$1") <(xxd "$2")
}
#add filed or directory to dotfiles
function dotfiles__mv {
test -n "$1" || {
echo "No file or directory name" >&2
return 1
}
expandedArg="$(realpath "${1}")"
if ! { [ -f "$expandedArg" ] || [ -d "$expandedArg" ]; }; then
echo "No such file or directory" >&2
return 1
fi
test -h "$1" && {
echo "Symlink already created" >&2
return 1
}
#shellcheck disable=2164
pushd "$HOME" >/dev/null
src=$(realpath --relative-to="$HOME" "$expandedArg")
dirName=$(dirname "$src")
baseName=$(basename "$src")
if [ "$dirName" = "." ]; then
dstDirName="$HOME/dotfiles"
else
dstDirName="$HOME/dotfiles/$dirName"
fi
mkdir -p "$dstDirName"
mv "$src" "$dstDirName" && ln -s -T "$dstDirName/$baseName" "$src"
#shellcheck disable=2164
popd >/dev/null
}
complete -fd dotfiles__mv
function tmux__list_options {
local range
case $1 in
-s)
range='/^ {5}Available server options/,/^ {5}Available session options/p'
;;
-e)
range='/^ {5}Available session options/,/^ {5}Available window options/p'
;;
-w)
range='/^ {5}Available window options/,/^ {5}Available pane options/p'
;;
-p)
range='/^ {5}Available pane options/,/^[A-Z]/p'
;;
*)
range='/^ {5}Available server options/,/^[A-Z]/p'
;;
esac
man -P cat tmux | sed -En "$range" | grep -E --color=never '^ {5}[a-z]'
}
#stdin input expected
function conf__pick {
if [[ ! "${1-}" =~ [[:alnum:]_-] ]]; then
echo "At least one string key expected" >&2
return 1
fi
# local delim=':'
# awk --assign delim="$delim" 'BEGIN {
awk 'BEGIN {
for (i=1; i<ARGC; i++) {
key=ARGV[i];
key_order[i-1] = key
map[key] = "";
}
ARGC=1;
}
#Not a comment
!/^#/ {
key = $1;
if(key in map) {
map[key] = $2;
}
}
END {
for(i in key_order) {
print map[key_order[i]];
}
}' "$@"
#| cut -c 2-
}
function conf__read {
read -r -d '' "$@" < <(conf__pick "$@")
}
function lines {
#from-to
#point,around
local range="$1"
local sed_arg
if [[ "$range" = *,* ]]; then
local point="${range%,*}"
local delta="${range#*,}"
sed_arg="$((point - delta)),$((point + delta))p;$((point + delta + 1))q"
elif [[ "$range" = *-* ]]; then
local last="${range#*-}"
sed_arg="${range/-/,}p;$((last + 1))q"
else
echo "lines range required"
return 1
fi
local file="$2"
if ! [[ -r "$file" ]]; then
file="-"
fi
sed -n "$sed_arg" "$file"
}
function cmake_uninstall {
xargs rm <install_manifest.txt
xargs -L1 dirname <install_manifest.txt | xargs rmdir -p
}
function proc_env {
local pid="$1"
if ! ((pid > 0)); then
echo "PID expected to be a positive number" >&2
return 1
fi
tr '\0' '\n' <"/proc/$pid/environ"
}
function is_ubuntu {
[[ "$(type -t lsb_release >/dev/null && lsb_release -i | cut -f 2-)" = "Ubuntu" ]]
}
function stopwatch {
start=$(date +%s)
while true; do
time="$(($(date +%s) - start))"
printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)"
done
}
source "$HOME/dotfiles/bash/fancy_tools.bash"
"$HOME/dotfiles/bin/is_workstation" &&
source "$HOME/dotfiles/bash/workstation.bash"
is_ubuntu &&
source "$HOME/dotfiles/bash/ubuntu.bash"