Repository navigation
Expand file tree
/
Copy path.bashrc
More file actions
386 lines (322 loc) · 7.15 KB
/
Copy path.bashrc
File metadata and controls
386 lines (322 loc) · 7.15 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
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
# ~/.bashrc
#TODO: make separte file for things that differ between machines
# If not running interactively, don't do anything
[[ $- != *i* ]] && return
echo setting constants
# {{{ CONSTANTS
MACHINE=$(hostname)
# define constants for coloring
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
BLUE="$(tput setaf 6)"
BOLD="$(tput bold)"
NC="$(tput sgr0)"
# top banner
first=true
entry="${GREEN}${MACHINE}${NC} welcomes you ${GREEN}${USER}${NC}!"
entryPatch="${MACHINE} welcomes you ${USER}!"
firstClear=true
if [ -f ~/.dot.conf ]; then
source ~/.dot.conf
else
echo you do not have set up .dot.conf, pls fix
echo if you don\'t know what are the options,
echo please refer to README.md in your dotfile folder
firstClear=false
fi
# }}}
echo setting exports
# {{{ EXPORTS
export XDG_DATA_HOME="/home/nevim/.XDG_DATA"
export PYTHONPATH="/usr/lib/python3.13/site-packages/"
export STM32_PRG_PATH=/home/nevim/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin
# manpages color support
# shamelessly 'borrowed' from https://gist.github.com/bahamas10/542875bb47990933638d2b7dfaa501bf
export MANROFFOPT=-P-c
export LESS_TERMCAP_mb=$'\e[1;31m'
export LESS_TERMCAP_md=$'\e[1;31m'
export LESS_TERMCAP_me=$'\e[0m'
export LESS_TERMCAP_se=$'\e[0m'
export LESS_TERMCAP_so=$'\e[1;30;47m'
export LESS_TERMCAP_ue=$'\e[0m'
export LESS_TERMCAP_us=$'\e[4;1;32m'
export LESS_TERMCAP_mr=$'\e[7m'
export LESS_TERMCAP_mh=$'\e[2m'
export PAGER='less'
# }}}
echo prepare for voluntary clear
# {{{ VOLUNTARY CLEAR
# make clearing method
clear(){
command clear
date="$(date)"
width=$(tput cols)
if $first;then
tput hpa $(((width / 2)-(${#entryPatch} / 2)))
echo $entry
first=false
fi
tput hpa $(((width / 2)-(${#date} / 2)))
echo $date
}
# bind it to <ctrl> + l
#bind -x '"\C-l":clear'
bind -x '"\e\C-l":clear'
# }}}
echo prepare for cleanup after death
# {{{ CLEANUP AFTER DEATH
# make method for NOT making forever-living ssh agents
cleanup(){
if [[ ! -z "$KILL_VIM_SSH" || ( -z "$VIM" && ! -z "$SSH_AGENT_PID" ) ]];then
kill $SSH_AGENT_PID || echo no killing happend
unset SSH_AGENT_PID
echo ssh agent killed
else
echo ssh wasnt started in vim or isnt running altogether
fi
#kill_tmux && echo tmux killed
sleep $1
}
# handle when the window of this shell is closed
sighupHandle(){
cleanup 0
exit
}
# I am using vim (too much)
:q(){
cleanup 1
exit
}
# make it that i wouldn't have forever-living ssh agents
trap 'cleanup 1' EXIT
trap sighupHandle SIGHUP
# }}}
echo preparing ssh
# {{{ SSH
# starting SSH agent
startSSH(){
if [ -z "$SSH_AGENT_PID" ]; then
eval "$(ssh-agent -s)"
if [ -n "$VIM" ];then
echo ssh started in vim
KILL_VIM_SSH=true
sleep 0.5
fi
else
echo SSH agent is already running
fi
# by this point ssh agent should be already running
if [ -z "$sshPrivateKey" ]; then
echo sshPrivateKey is not set!
return 1
fi
if [[ "$keyAdded" != "true" ]]; then
echo adding key at $sshPrivateKey
ssh-add $sshPrivateKey
keyAdded="true"
else
echo you\'ve already added your key
fi
}
stopSSH(){
if [ -n "$SSH_AGENT_PID" ]; then
kill $SSH_AGENT_PID
unset SSH_AGENT_PID
KILL_VIM_SSH=false
keyAdded=false
else
echo SSH agent isn\'t running
fi
}
ssh(){
startSSH
command ssh "$@"
first=true clear
}
ssh-copy-id(){
startSSH
command ssh-copy-id "$@"
}
# }}}
echo preparing git
# {{{ GIT
# autostart SSH agent when trying to access git remote
git(){
case $@ in
push*)
startSSH
command git $@
;;
pull*)
startSSH
command git $@
;;
fetch*)
startSSH
command git $@
;;
*)
command git $@
;;
esac
}
# support for git-prompt
. ~/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
# }}}
echo preparing tmux
# {{{ TMUX
export RUN_TMUX=true
export TMUX_BIN=tmux
# running new tmux (or attaching) with session name derived from parent bash pid
runTmux() {
SESSION_NAME="main"
# try to find session with the correct session id (based on the bashs PID)
# bugfix: added parenthesis around thing that gives value
EXISTING_SESSION=$($TMUX_BIN ls 2> /dev/null | grep "$SESSION_NAME" | wc -l)
if [[ "$EXISTING_SESSION" -gt "0" ]]; then
# if such session exists, attach to it
$TMUX_BIN -2 attach-session -t "$SESSION_NAME"
else
# if such session does not exist, create it
$TMUX_BIN new-session -s "$SESSION_NAME"
fi
# hook after exitting the session
# when the session exists, find a file in /tmp with the name of the session
# and extract a path from it. Then cd to it.
FILENAME="/tmp/tmux_restore_path.txt"
if [ -f $FILENAME ]; then
MY_PATH=$(tail -n 1 $FILENAME)
rm /tmp/tmux_restore_path.txt
cd $MY_PATH
fi
}
kill_tmux() { $TMUX_BIN kill-session -t "main";}
# if opening new tmux window so it won't try to start new tmux session inside tmux
# basicly don't nest tmux
[[ $TERM != "screen" && -z $VIM && $RUN_TMUX && -z $TMUX && ! -z "$DISPLAY" ]] && TERM=xterm-256color && runTmux
# }}}
if [ $firstClear ]; then
clear
fi
# {{{ RANGER SETUP
bind -x '"\C-o": ranger-select files ""'
bind -x '"\eo": ranger-select dir "/"'
function ranger-select {
local F=$(mktemp)
ranger --choose$1=$F
if [ -s $F ] ; then
local SEL="$(<$F sed '/[^0-9A-Za-z._/-]/{s/^/"/; s/$/"/;}' | tr '\n' ' ')"
if [ "${SEL:${#SEL}-1}" != " " ] ; then
SEL="$SEL$2 "
fi
READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$SEL${READLINE_LINE:$READLINE_POINT}"
READLINE_POINT=$(($READLINE_POINT + ${#SEL}))
fi
rm -f $F
}
# }}}
# {{{ PYENV
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init - bash)"
# }}}
# {{{ CUSTOM COMMANDS
# somewhy doesn't work for multiword dir names
# TODO fix that
cl(){
cd $@
ls --color=auto
}
source(){
if [[ $# == 0 ]]; then
command source ~/.bashrc
else
command source "$@"
fi
}
lynx(){
if [[ "$1" = "-r" ]]; then
shift
command lynx $*
else
url=$(echo $* | sed -r -e s/\ /\ /g)
command lynx "duckduckgo.com/$url"
fi
}
w3m(){
if [[ "$1" = "-r" ]]; then
shift
command w3m $*
else
url=$(echo $* | sed -r -e s/\ /\ /g)
command w3m "duckduckgo.com/$url"
fi
}
show(){
kitten icat "$@"
}
# }}}
# {{{ ALIASES
alias ls='ls --color=auto'
alias la='ls -a'
alias l='ls -la'
alias grep='grep --color=auto'
alias please='sudo'
alias pls='sudo'
alias nuke='rm -rf'
alias ip='ip -c'
alias vi='vim -p'
alias vim='vim -p'
alias clr='clear'
alias nano='vim'
alias freakycad='freecad'
alias nevim='vim'
alias neovim='vim'
alias mv='mv -i'
alias cal='qalc'
alias calc='qalc'
alias ipy='ipython3'
alias py='python3'
# }}}
# {{{ PROMPT
exitCode(){
local exit="$?"
formatBefore=$1
formatAfter=$2
if [[ $exit != 0 ]]; then
echo "│ $formatBefore$exit$formatAfter "
fi
return "$exit"
}
shortenPwd(){
local exit="$?"
long="$1"
short="$2"
if (( $(( ${#long} + 20 )) < $(( $COLUMNS / 2 )) )) ; then
echo "$long "
else
echo "$short "
fi
return "$exit"
}
runningJobs(){
local exit="$?"
jobsRunning=$1
formatBefore=$2
formatAfter=$3
if [[ $jobsRunning != 0 ]]; then
echo "│ $formatBefore$jobsRunning$formatAfter "
fi
return "$exit"
}
PS1=''
PS1+='\A │ '
PS1+='\[$GREEN\]\u\[$NC\] │ '
PS1+='$(shortenPwd "\w" "\W")'
PS1+='$(__git_ps1 "│ (%s) ")'
PS1+='$(runningJobs \j \[$BLUE\] \[$NC\])'
PS1+='$(exitCode \[$RED$BOLD\][ ]\[$NC\])'
PS1+='\$> '
PS2='> '
# }}}