-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_bashrc.sh
339 lines (287 loc) · 7.95 KB
/
setup_bashrc.sh
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
#!/bin/bash
#
# Contributor: miguelgfierro@github
#
# Set up several functions to .bashrc like cs (a combination of cd+ls), ccat
# (cat with color) or reimplement evince to run in background.
#
#
# IMPORTANT!!!
# METHOD OF USE:
#
# source setup_bashrc.sh
#
# We have to source the file instead of using sh. The reason is because the line
# source ~./.bashrc will source the file in the sub-shell, i.e. a shell which
# is started as child process of the main shell.
#
# Exit if not sourced
if [ "$0" == "$BASH_SOURCE" ]; then
echo >&2 "Setup script has to be sourced, not run with sh. Aborting"
exit 1
fi
# Change this if home is not where you lay your head
INTENDED_BASH_PATH=~/.bashrc
# To set up the bashrc script in Windows using cygwin it is necessary to add
# at the beginning of the script to fix a problem with newlines the following:
if [ "$(uname -o)" == "Cygwin" ]; then
(set -o igncr) 2>/dev/null && set -o igncr;
fi
# Create save path file if it is not created
if [ ! -e ~/.sp ]; then
touch ~/.sp
fi
ADDON=$'#START!SETUP!SCR##########
EB_LOCATION=~/.local/bin
export PATH=$EB_LOCATION:$PATH
# Hide user name and host in terminal
#export PS1="\w$ "
# Make ls every time a terminal opens
ls
# typos
alias sl="ls"
alias exti="exit"
alias eixt="exit"
alias xit="exit"
alias clera="clear"
alias clea="clear"
alias claer="clear"
# extensive ls
alias ll="ls -lasi"
# safe file management
alias cp="cp -iv"
alias rm="rm -i"
alias mv="mv -i"
# quick directory movement
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias nau="nautilus . > /dev/null 2>&1 & disown"
# quickly find files and directory
alias ff="find . -type f -name"
alias fd="find . -type d -name"
# print the current time
alias now="date +%T"
# all-in-one update
alias update="sudo apt-get update -y | grep -v \':\' \
&& sudo apt-get upgrade -y \
&& sudo apt-get dist-upgrade -y \
&& sudo apt-get autoclean -y \
&& sudo apt autoremove -y \
&& which pip > /dev/null 2>&1 \
&& echo \'pip update cycle...\' \
&& pip install --upgrade --quiet pip \
&& pip freeze --local --quiet | grep -v \'^\-e\' | cut -d = -f 1 | xargs -n1 sudo -H pip install --user -U"
alias upd="update | grep \'install\'"
# Note that this refers to a file
alias updatePretty="sudo apt-get update -y | grep -v \':\' \
&& sudo apt-get upgrade -y \
&& sudo apt-get dist-upgrade -y \
&& sudo apt-get autoclean -y \
&& sudo apt autoremove -y \
&& which pip > /dev/null 2>&1 \
&& echo \'pip update cycle...\' \
&& [[ -e ~/git/usefulBash/green_red_pip.sh ]] \
&& bash ~/git/usefulBash/green_red_pip.sh"
# TODO make this independent of location
alias updp="updatePretty"
# Git commands
alias gau="git add -u"
alias gaa="git add -A"
alias gcm="git_commit_message"
alias gp="git_push_current_branch"
alias gri="git_rebase_interactive"
alias grc="git add -A && git rebase --continue"
alias gra="git rebase --abort"
alias gs="git status"
alias gd="git_diff_head"
alias pull="git_pull_current_branch"
alias gcb="git_checkout_branch"
git_commit_message() {
git commit -am "$*"
}
git_push_current_branch() {
local branch_name=$(git symbolic-ref --short HEAD)
git push origin "${branch_name}"
}
git_rebase_interactive() {
local branch=${1:-origin/main}
git rebase -i "${branch}"
}
git_diff_head() {
local num_commits=${1:-2}
git diff HEAD~"${num_commits}"
}
git_pull_current_branch() {
local branch_name=$(git symbolic-ref --short HEAD)
git pull origin "${branch_name}"
}
git_checkout_branch() {
# Check if the branch exists locally
if git show-ref --verify --quiet refs/heads/"$1"; then
# If the branch exists, check it out
git checkout "$1"
else
# If the branch does not exist, create it and check it out
git checkout -b "$1"
fi
}
# OS Power commands
alias reboot="sudo /sbin/reboot"
alias shutdown="sudo /sbin/shutdown"
# Wifi
alias scanwifi="sudo iwlist scan >/dev/null 2>&1 &"
alias rswifi="sudo service network-manager restart"
# open mail client
alias mail="thunderbird > /dev/null 2>&1 & disown"
alias steamw="wine ~/.wine/drive_c/Program\ Files\ \(x86\)/Steam/Steam.exe -no-dwrite"
#
# cd + ls
#
function cs () {
cd $1
ls -a
}
#
# transfer path: save the current path to a hidden file
#
function tp () {
pwd > ~/.sp
}
#
# goto transfer path: goes where the previously saved tp points
#
function gtp () {
cs `cat ~/.sp`
}
#
# cat with color
#
function ccat () {
source-highlight -fesc -i $1
}
#
# Remove trash from terminal and runs program in background
#
function evince () {
/usr/bin/evince $* 2> /dev/null & disown
}
function gedit () {
/usr/bin/gedit $* 2> /dev/null & disown
}
#
# up N: moves N times upwards (cd ../../../{N})
# author: Frederic Dauod
#
function up () {
LIMIT=$1
P=$PWD
for ((i=1; i <= LIMIT; i++))
do
P=$P/..
done
cd $P
export MPWD=$P
}
#
# Open chrome fast
#
function chr () {
if [ $(which google-chrome-beta | wc -l) -eq 1 ]; then
google-chrome-beta $* 2> /dev/null & disown
else
if [ $(which google-chrome | wc -l) -eq 1 ]; then
google-chrome $* 2> /dev/null & disown
else
echo "No chrome :("
fi
fi
}
#
# Do a normal add/commit/push in one go
#
function gut () {
if [ $# -eq 0 ] || [ $# -gt 2 ]; then
echo "args are 1: message, 2: branch (default: main)"
echo "dont forget to \"quote\" your message"
return 1
fi
DEST=""
if [ -z "$2" ]; then
DEST="main"
else
DEST="$2"
fi
if [ $(which git | wc -l) -eq 1 ]; then
git add -A
git commit -m \"$1\"
git push origin $DEST
else
echo "no git :("
fi
}
#
# extract an archive
#
extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "don\'t know how to extract \'$1\'..." ;;
esac
else
echo "\'$1\' is not a valid file!"
fi
}
#
# remove spaces recursively in folder and file names
# author: Michael Krelin
#
function fixnames () {
find . -depth -name \'* *\' \
| while IFS= read -r f ; do mv -i \"$f\" \"$(dirname \"$f\")/$(basename \"$f\"|tr \' \' _)\" ; done
}
function mci () {
mvn clean install
}
# Copy for llm
function cpl() {
folder="${1:-.}" # Default to current folder if no folder argument is provided
depth="${2:-10}"
ignored_folders="${3:-.git __pycache__ node_modules .terraform fonts}"
target_file="/tmp/concat.txt"
: > "$target_file" # Reset target file
while IFS= read -r file; do
echo -e "\n\n--- File: $(echo "$file" | sed \'s/^\.\//\//\')" >> "$target_file"
# Assume global replacement of double spaces to tabs here for each file\'s content
awk \'{ line = $0; gsub(/ /, "\t", line); print line; }\' "$file" >> "$target_file"
done < <(find "$folder" -maxdepth "$depth" -type f | grep -vE "$(echo $ignored_folders | sed \'s/ /|/g\')")
# Use wc to count words in the combined file, then calculate tokens based on the ratio
word_count=$(wc -w < "$target_file" | tr -d \' \')
token_estimate=$(awk -v wc="$word_count" \'BEGIN{ printf "%.0f", wc * 1.3 }\')
echo "Estimated number of tokens in $target_file: $token_estimate"
cat "$target_file" | clip.exe
}
#END!SETUP!SCR#'
# apply the change, replace if an older version exists
! grep -q "#START!SETUP!SCR#" $INTENDED_BASH_PATH || { echo >&2 "Script already run. Replacing... "; sed -i '/#START!SETUP!SCR#/,/#END!SETUP!SCR#/d' $INTENDED_BASH_PATH; }
# Valid bash, not POSIX sh! (suggestions welcome)
echo "$ADDON" >> $INTENDED_BASH_PATH
source $INTENDED_BASH_PATH
# if unix & dos2unix exists apply it
if [ "$(uname -o)" == "Cygwin" ]; then
if command -v dos2unix >/dev/null 2>&1; then
dos2unix $INTENDED_BASH_PATH
fi
fi
echo ".bashrc updated"