-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbashrc_beta
358 lines (300 loc) · 16.6 KB
/
bashrc_beta
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
#!/bin/bash
#################################
###### Xterm Transparency #######
[[ "$XTERM_VERSION" ]] && command -v transset-df >/dev/null 2>&1 && transset-df -x 0.7 -m 0.7 --id "$WINDOWID" >/dev/null
# Using bash’s shopt builtin to manage Linux shell behavior
# The shopt builtin offers 53 settings that can alter how bash behaves.
# Read this post and then refer to bash's man page to follow up on how these settings might work for you.
# https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html
# https://www.networkworld.com/article/3574960/using-bashs-shopt-builtin-to-manage-linux-shell-behavior.html
#set -x # Debug trace
#The set -o pipefail option is used in Bash to handle pipeline failures (chaining commands using the "|" operator) more accurately. Typically,
#a pipeline returns the exit code of the last command executed in the pipeline. However, if an intermediate command fails,
#theexit code is usually ignored and the pipeline is considered successful.
#Enabling set -o pipefail changes the default behavior. In this mode, the pipeline will return the exit code of the last command that failed
#within the pipeline, instead of returning the exit code of the last executed command. This can be useful for detecting failuresin complex pipelines,
#especially when dealing with data processing or task automation.
#set -o pipefail # Will cause the pipeline to return the exit code of the last failed command within the pipeline.
#set -e # Debug (Errors: exit) mode 'exit-on-error' or 'errexit'. Terminates execution immediately if any command returns a non-zero error code.
#set -u # Enables 'nounset' or 'unbound variables' mode. Generates an error if an uninitialized variable is referenced.
#set -f # Disable globstar. Disables wildcard expansion (globbing).
#set -efu
#set -eo pipefail
# shopt | column
#
## By enabling the globstar option, you can glob all matching files in this directory and all subdirectories:
## Example: for i in **/*.sh; do echo ${i}; done
## For allowing the Bash shell to expand globes (wildcards) across all matching files in directories and subdirectories
#########################################################################################################################
## Habilita a opção globstar, permitindo que o shell Bash expanda globos (wildcards) em todos os arquivos correspondentes
## nos diretórios e subdiretórios.
shopt -s globstar
## If set, the extended pattern matching features described above (see Pattern Matching) are enabled.
###################################################################################################
## Habilita a opção extglob, que permite o uso de recursos avançados de correspondência de padrões,
## como padrões de correspondência estendidos.
shopt -s extglob
## If set, minor errors in the spelling of a directory component in a cd command will be corrected.
## The errors checked for are transposed characters, a missing character, and a character too many.
## If a correction is found, the corrected path is printed, and the command proceeds. This option is only used by interactive shells.
#########################################################################################################################
## Habilita a opção cdspell, que corrige erros menores na digitação de nomes de diretórios ao usar o comando cd.
## Por exemplo, se você digitar um diretório com caracteres transpostos, ausentes ou extras, o Bash tentará corrigi-lo
## e executar o comando cd para o diretório corrigido.
shopt -s cdspell
## If set, Bash replaces directory names with the results of word expansion when performing filename completion.
## This changes the contents of the Readline editing buffer. If not set, Bash attempts to preserve what the user typed.
#########################################################################################################################
## Habilita a opção direxpand, que substitui nomes de diretórios pelos resultados da expansão de palavras durante
## a conclusão de nomes de arquivos. Isso significa que, ao usar a conclusão de tabulação, o Bash substituirá nomes
## de diretórios pelos caminhos completos correspondentes.
shopt -s direxpand
## If set, Bash attempts spelling correction on directory names during word completion if the directory name initially supplied does not exist.
##############################################################################################################################################
## Habilita a opção dirspell, que realiza correção ortográfica em nomes de diretórios durante a conclusão de tabulação, caso o nome do diretório
## fornecido inicialmente não exista. O Bash tentará corrigir o nome do diretório e completá-lo corretamente.
shopt -s dirspell
## If set, a command name that is the name of a directory is executed as if it were the argument to the cd command.
## This option is only used by interactive shells.
###################################################################################################################
## Habilita a opção autocd, permitindo que um nome de comando que corresponda a um diretório seja executado como se fosse o argumento
## para o comando cd. Isso é útil para navegar diretamente para um diretório digitando seu nome no prompt de comando, em vez de precisar digitar
## explicitamente o comando cd.
shopt -s autocd
## With this option enabled, matching wildcards will not be case sensitive. For example, *.txt will match files with a .txt extension,
## regardless of case.
######################################################################################################################################
## Com essa opção habilitada, a correspondência de globos (wildcards) não será sensível a maiúsculas e minúsculas. Por exemplo, *.txt
## corresponderá a arquivos com extensão .txt, independentemente do uso de letras maiúsculas ou minúsculas.
shopt -s nocaseglob
# Enable history reediting and verification.
shopt -s histreedit
## This option causes the history to be displayed before each command is executed. After displaying the command history,
## you have the option to edit or confirm the execution. This can help to avoid errors when running old commands from history.
######################################################################################################################################
## Essa opção faz com que o histórico seja exibido antes da execução de cada comando. Após exibir o comando do histórico, você tem a opção
## de editar ou confirmar a execução. Isso pode ajudar a evitar erros ao executar comandos antigos do histórico.
shopt -s histverify
## With this option enabled, the history will be stored in single-line format instead of the default multi-line format. This makes it easy
## to search and manipulate history using tools like grep or scripts.
######################################################################################################################################
## Com essa opção ativada, o histórico será armazenado no formato de linha única, em vez do formato de várias linhas padrão.
## Isso facilita a pesquisa e manipulação do histórico usando ferramentas como grep ou scripts.
#shopt -s lithist
## By enabling this option, the history will not store duplicate consecutive commands. This can help reduce the history size and preventrepeated
## commands from taking up unnecessary space.
######################################################################################################################################
## Habilitando essa opção, o histórico não armazenará comandos consecutivos duplicados.
## Isso pode ajudar a reduzir o tamanho do histórico e evitar que comandos repetidos ocupem espaço desnecessário.
shopt -s cmdhist
## With this option enabled, Bash will not complete empty commands when pressing the Tab key twice. This prevents Bash from listing
## all available commands when no commands have been entered.
######################################################################################################################################
## Com essa opção ativada, o Bash não completará comandos vazios ao pressionar a tecla Tab duas vezes.
## Isso evita que o Bash liste todos os comandos disponíveis quando nenhum comando foi digitado
shopt -s no_empty_cmd_completion
#################################
######### SHELL UTILS ###########
[[ -f ~/.shell_utils/shell_utils.sh ]] && . ~/.shell_utils/shell_utils.sh
#################################
# Bash-it
[[ -f /usr/lib/bash-it-git/bash_it.sh ]] && . /usr/lib/bash-it-git/bash_it.sh
# Enable the subsequent settings only in interactive sessions
case $- in
*i*) ;;
*) return;;
esac
# Oh My Bash Config
OMB_THEME=demula #"agnoster" #"pure" #"rr" #"mairan" #"rjorgenson" #"brainy" #"iterate" #"standard" #"demula" #"powerline-naked" #"powerline-multiline" #"90210" #"clean" #"dulcie" #"brunton" #"binaryanomaly" #"random" #"font" #"powerline" #"duru" #"vscode" #"candy" #"hawaii50" #"emperor" #"tylenol" #"morris" #"pzq" #"zitron" #"kitsune"
OMBC=~/.shell_utils/frameworks/oh-my-bash-config.sh && [[ -f "$OMBC" ]] && . "$OMBC"
if [[ "$OMB_THEME" == "agnoster" ]]; then
prompt_theme_agnoster
fi
##### STARTUP
##### INPUTRC
if [ ! -f ~/.inputrc ]; then
cat <<'EOF' | tee ~/.inputrc &>/dev/null
$include /etc/inputrc
set completion-ignore-case On
set show-all-if-ambiguous On
set show-all-if-unmodified On
set bell-style On
set colored-stats On
set colored-completion-prefix On
set menu-complete-display-prefix On
set mark-symlinked-directories On
set visible-stats On
set completion-query-items 50
set history-preserve-point On
EOF
fi
# Force prompt to write history after every command.
shopt -s histappend
shopt -s checkwinsize
# Undocumented feature which sets the size to "unlimited".
# http://stackoverflow.com/questions/9457233/unlimited-bash-history
###export HISTFILESIZE === 100000
###export HISTSIZE === 100000
# ---------------------
# Eternal bash history.
export HISTFILESIZE=
export HISTSIZE=
#export HISTIGNORE="&:?:*:ERR"
# Remove leading and trailing whitespace from history entries.
export HISTIGNORE=' *'
### ignoreboth:erasedups:ignorespace:ignoredups
export HISTCONTROL="ignoreboth:erasedups:cmdfail"
export HISTTIMEFORMAT="[%F %T] "
#export HISTTIMEFORMAT=
# Change the file location because certain bash sessions truncate .bash_history file upon close.
# http://superuser.com/questions/575479/bash-history-truncated-to-500-lines-on-each-login
export HISTFILE=~/.bash_history
bash_shortcuts() {
cat <<'EOF'
Ctrl+A: Moves the cursor to the beginning of the line.
Ctrl+E: Moves the cursor to the end of the line.
Ctrl+U: Clears the line before the cursor.
Ctrl+K: Clears the line after the cursor.
Ctrl+W: Deletes the previous word.
Ctrl+L: Clears the screen.
EOF
return 0
}
_bash_autocomplete() {
local current_word words completions
COMPREPLY=()
current_word="${COMP_WORDS[COMP_CWORD]}"
# Gets the list of possible commands
words=$(echo ${COMP_WORDS[@]:1} | xargs -n1 echo | sort -u)
# Completes the command if there is only one option
if [ $(echo "$words" | wc -l) -eq 1 ]; then
COMPREPLY=("$words")
return
fi
# Gets the completion options for the current command
completions=$(compgen -W "$words" -- "$current_word")
COMPREPLY=($completions)
return 0
}
# Configure prompt behavior
_bash_prompt_command() {
local exit_code="$?"
# Run autocomplete command only when no completions are available
if [ -z "$COMP_LINE" ]; then
return
fi
# Configures the autocomplete function
complete -F _bash_autocomplete
# Clear current prompt
PS1=""
# Define your custom prompt here
# Example: PS1="[\\u@\\h \\W]\\$ "
export PS1="\[\e[32m\]\t \[$txtgrn\]\u\[$txtwht\]@\[$txtgrn\]\h \[$txtylw\]\w\[$txtrst\] \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\n\$ "
# Show prompt
echo -ne "$PS1"
return 0
}
remove_timestamp_in_two_line_format_from_bash_history() {
[[ ! -f "$HISTFILE" ]] && touch "$HISTFILE"
[[ -f "$HISTFILE" ]] && sed -i '/^#/d' "$HISTFILE" >/dev/null 2>&1
return 0
}
remove_timestamp_in_single_line_format_from_bash_history() {
[[ ! -f "$HISTFILE" ]] && touch "$HISTFILE"
[[ -f "$HISTFILE" ]] && sed -i 's/^:[[:space:]][0-9]\{10\}:0;//g' "$HISTFILE" >/dev/null 2>&1
return 0
}
# https://www.baeldung.com/linux/history-remove-avoid-duplicates
remove_duplicate_commands_from_bash_history() {
[[ ! -f "$HISTFILE" ]] && touch "$HISTFILE"
local history_file="${HISTFILE}"
local temp_file="${HISTFILE}.tmp"
[[ -f "${temp_file}" ]] && rm -f "${temp_file}"
# Filter duplicate commads
awk '!a[$0]++' "$history_file" > "$temp_file"
# Replace history
mv -f "$temp_file" "$history_file" >/dev/null 2>&1
return 0
}
remove_unused_timestamps_from_bash_history() {
[[ ! -f "$HISTFILE" ]] && touch "$HISTFILE"
local history_file="${HISTFILE}"
local temp_file="${HISTFILE}.tmp"
[[ -f "${temp_file}" ]] && rm -f "${temp_file}"
# Filter history lines with timestamps and associated commands
awk '/^#[0-9]+$/ { if (getline c && c !~ /^#[0-9]+$/) { print $0 ORS c } } !/^#[0-9]+$/ { print }' "$history_file" > "$temp_file"
# Replace history
mv -f "$temp_file" "$history_file" >/dev/null 2>&1
return 0
}
include_timestamp_in_bash_history() {
[[ ! -f "$HISTFILE" ]] && touch "$HISTFILE"
local history_file="${HISTFILE}"
local temp_file="${HISTFILE}.tmp"
[[ -f "${temp_file}" ]] && rm -f "${temp_file}"
# Filter history lines with timestamps and associated commands
awk '/^#[0-9]+$/ { if (getline c && c !~ /^#[0-9]+$/) { print $0 ORS c } } !/^#[0-9]+$/ { print strftime("#%s") ORS $0 }' "$history_file" > "$temp_file"
# Replace history
mv -f "$temp_file" "$history_file" >/dev/null 2>&1
return 0
}
# Fix several known issues of bash history, and amazingly, it is able to convert .zsh_history to standard .bash_history structure
fix_bash_history() {
remove_timestamp_in_single_line_format_from_bash_history
#remove_duplicate_commands_from_bash_history
remove_unused_timestamps_from_bash_history
include_timestamp_in_bash_history
#history -r # Reload history conflicts with: remove_failed_commands_from_bash_history_and_fix_bash_history
#history -s # Waning, no use!
#history -w # Recording history conflicts with some of these functions
return 0
}
# Only works in PROMPT_COMMAND= or trap 'function' DEBUG
remove_failed_commands_from_bash_history_and_fix_bash_history() {
if [ $? -ne 0 ]
then
#history -d $(($HISTCMD-1)) # Remove last command from history
#history -r # Reload history conflicts with delete: history -d $(($HISTCMD-1)) # <-- Use this line
echo -ne "${shell_color_palette[bred]}Wrong command!${shell_color_palette[color_off]}\t"
#history -s; history -w # Writing to the file can be a problem, it's best to just reload the history, which will prevent the wrong command from being written
elif grep -qE "^:[[:space:]]?[0-9]{10}:0;" "${HISTFILE}" >/dev/null 2>&1
then
fix_bash_history #; history -r
elif ! awk '/^#[0-9]+$/{ if (getline c && c !~ /^#[0-9]+$/) { print $0 ORS c } } !/^#[0-9]+$/{ exit 1 }' "${HISTFILE}" >/dev/null 2>&1
then
fix_bash_history #; history -r
fi
}
# Configure navigation keys
bind '"\t":menu-complete' # TAB
bind '"\e[Z":menu-complete-backward' # Shift + TAB
# jump to next/previous word in a commandline by pressing SHIFT+RIGHT and SHIFT+LEFT
bind '"\e[1;2C":menu-complete' # Shift + Right
bind '"\e[1;2D":menu-complete-backward' # Shift + Left
bind '"\e[1;5A":forward-word' # Ctrl + Up
bind '"\e[1;5B":backward-word' # Ctrl + Down
complete -abcdefgjksuv sudo
complete -abcdefgjksuv doas
# Configure command prompt
fix_bash_history
#trap 'remove_failed_commands_from_bash_history_and_fix_bash_history' DEBUG
#export PROMPT_COMMAND="remove_failed_commands_from_bash_history_and_fix_bash_history; fix_bash_history; _bash_prompt_command"
export PROMPT_COMMAND="_bash_prompt_command"
###export GITAWAREPROMPT=~/.bash/git-aware-prompt
###source $GITAWAREPROMPT/main.sh
############################################################################
fzf_bash() {
file=~/.fzf.bash
if ! test -f "$file" && command -v fzf &>/dev/null; then
fzf --bash | tee "$file" &>/dev/null
fi
test -f "$file" && source "$file"
return 0
}
fzf_bash
# >>> mamba initialize >>>
# !! Contents within this block are managed by 'mamba init' !!
# mamba_setup # Uncomment this line
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
# conda_setup # Uncomment this line