blob: 95c8250d6ada0be8475ed2db9f1cfd9612acbf30 (
plain)
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
# Do not load prompt, aliases, completion, or interactive tools in non-interactive shells.
case $- in
*i*) ;;
*) return ;;
esac
# Default prompt symbol; override it in ~/.bashrc.local for each machine.
PROMPT_SYMBOL="${PROMPT_SYMBOL:-λ}"
PS1="$PROMPT_SYMBOL "
__prompt_command() {
local EXIT="$?" # This needs to be first
[ "$#" -gt 0 ] && EXIT=$1
local RCol='\[\e[0m\]'
local Red='\[\e[0;31m\]'
local Gre='\[\e[0;32m\]'
local BYel='\[\e[1;33m\]'
local BBlu='\[\e[1;34m\]'
local Pur='\[\e[0;35m\]'
local prompt
if [ "$EXIT" -ne 0 ]; then
prompt="${Red}${PROMPT_SYMBOL}[$EXIT] ${RCol}"
else
prompt="${Gre}${PROMPT_SYMBOL} ${RCol}"
fi
[ -n "${SSH_CONNECTION:-}" ] && prompt+="${BYel}\u@\h "
prompt+="${RCol}${Pur}\W${BYel}$(git_status_prompt) $ ${RCol}"
# Asignar al final evita dejar PS1 vacío si el hook se interrumpe.
PS1="$prompt"
}
__history_and_prompt_command() {
local exit_status="$?"
history -a
history -n
__prompt_command "$exit_status"
}
# A little command that shows your current branch and status when
# you are inside a git repository. Perfect to add to your prompt!
#
# Example (with prompt):
#
# λ unixconfig( master ✓ ) $
#
git_status_prompt() {
local output='' line='' branch='' oid='' status='' ab='' sync=''
local ahead=0 behind=0
# --no-optional-locks: do not take the index lock just to refresh the prompt.
if ! output=$(git --no-optional-locks status --porcelain=v2 --branch 2>/dev/null); then
return
fi
while IFS= read -r line; do
case "$line" in
'# branch.head '*) branch=${line#\# branch.head } ;;
'# branch.oid '*) oid=${line#\# branch.oid } ;;
'# branch.ab '*)
# Format: "+AHEAD -BEHIND"; only present if an upstream is set.
ab=${line#\# branch.ab }
ahead=${ab%% *}
ahead=${ahead#+}
behind=${ab##* }
behind=${behind#-}
;;
'# '*) ;;
*) status=1 ;;
esac
done <<< "$output"
if [ "$branch" = '(detached)' ]; then
branch="@${oid:0:7}"
fi
[ "$ahead" -gt 0 ] && sync+=" ↑$ahead"
[ "$behind" -gt 0 ] && sync+=" ↓$behind"
if [ -n "$status" ]; then
printf '%s\n' "${BYel}( ${BBlu}$branch${Pur}${sync} ${Red}✗${BYel} )"
else
printf '%s\n' "${BYel}( ${BBlu}$branch${Pur}${sync} ${Gre}✓${BYel} )"
fi
}
PROMPT_COMMAND=__history_and_prompt_command # Save history and generate PS1 after commands
export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
export XDG_STATE_HOME="${XDG_STATE_HOME:-$HOME/.local/state}"
export EDITOR="${EDITOR:-vim}"
export VISUAL="${VISUAL:-$EDITOR}"
export PAGER="${PAGER:-less}"
export LESS="${LESS:--R -F -X}"
# Git commands
alias gc='git commit -m'
alias gaa='git add .'
alias gp='git push'
alias gl="git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all"
alias gchk='git checkout'
alias grefresh='git fetch --all && git pull'
alias gs='git status'
# LS
alias ll='ls -lah'
alias lc='ls --color=never'
alias ls='ls --color=auto'
alias lt='ls -lAht'
# Handful utils
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias mv='mv -i'
alias rm='rm -i'
alias cp='cp -i'
alias diff='diff --color=auto'
alias ip='ip -color=auto'
alias df='df -h'
alias free='free -h'
alias e='yazi'
alias ytd="yt-dlp"
alias grep='grep --color=auto'
alias lsblk='lsblk -fl'
alias genkey='ssh-keygen -t ed25519 -C "$(git config --get user.email)"'
alias ports='ss -tulnp'
alias myip='ip route get 1 | awk "{print \$7}" | head -1'
alias reload='source ~/.bashrc'
alias oh-btw='fastfetch'
mkcd() { mkdir -p "$1" && cd "$1"; }
croot() {
local root
if ! root=$(git rev-parse --show-toplevel 2>/dev/null); then
printf 'croot: not inside a Git repository\n' >&2
return 1
fi
cd "$root" || return
}
gb() {
local branch
branch=$(git for-each-ref --format='%(refname)' refs/heads refs/remotes \
| grep -v '/HEAD$' \
| sed -e 's|^refs/heads/||' -e 's|^refs/remotes/||' \
| fzf --no-preview --prompt='branch> ') || return
git checkout "${branch#origin/}"
}
showpath() {
tr ':' '\n' <<< "$PATH"
}
serve() {
local port=${1:-8000}
python -m http.server "$port"
}
extract() {
if [ "$#" -ne 1 ]; then
printf 'Usage: extract ARCHIVE\n' >&2
return 2
fi
local archive=$1
# Evita que un archivo como "-archivo.zip" se interprete como opción.
[ "${archive#-}" != "$archive" ] && archive="./$archive"
if [ ! -f "$archive" ]; then
printf 'extract: %s is not a regular file\n' "$1" >&2
return 1
fi
case "$archive" in
*.tar.gz|*.tgz) tar xzf "$archive" ;;
*.tar.bz2|*.tbz) tar xjf "$archive" ;;
*.tar.xz) tar xJf "$archive" ;;
*.tar) tar xf "$archive" ;;
*.zip) unzip "$archive" ;;
*.gz) gunzip "$archive" ;;
*.bz2) bunzip2 "$archive" ;;
*.xz) unxz "$archive" ;;
*.rar) unrar x "$archive" ;;
*) printf 'extract: unknown archive format: %s\n' "$1" >&2; return 1 ;;
esac
}
psf() {
if [ "$#" -ne 1 ]; then
printf 'Usage: psf PATTERN\n' >&2
return 2
fi
pgrep -af -- "$1"
}
bak() {
if [ "$#" -ne 1 ]; then
printf 'Usage: bak FILE\n' >&2
return 2
fi
if [ ! -e "$1" ]; then
printf 'bak: %s does not exist\n' "$1" >&2
return 1
fi
command cp -- "$1" "$1.$(date +%Y%m%d_%H%M%S).bak"
}
# generic recursive chmod.
# - First arg is either 'f' or 'd' to apply chmod to files or directories
# - Second arg is permissions. You usually use 644 for files and 755 for dirs
rchmod() {
if [ "$#" -ne 2 ] || [ -z "$2" ]; then
printf 'Usage: rchmod {f|d} mode\n' >&2
return 2
fi
case "$1" in
f|d) ;;
*) printf 'Usage: rchmod {f|d} mode\n' >&2; return 2 ;;
esac
if [[ ! $2 =~ ^[0-7]{3,4}$ ]]; then
printf 'Invalid mode: %s\n' "$2" >&2
return 2
fi
find . -type "$1" -exec chmod -- "$2" {} +
}
# Recursive chmod for directories (set 755)
alias rchmodd="rchmod d 755"
# Recursive chmod for files (set 644)
alias rchmodf="rchmod f 644"
# Fix all permissions recursively
alias fixperm="rchmodd; rchmodf"
# SSH load key
sshlk() {
if [ "$#" -ne 1 ]; then
printf 'Usage: sshlk KEY_NAME\n' >&2
return 2
fi
local key="$HOME/.ssh/$1"
if [ ! -f "$key" ]; then
printf 'sshlk: key does not exist: %s\n' "$key" >&2
return 1
fi
# Fixed socket shared by every terminal of this user.
local sock="${XDG_RUNTIME_DIR:-$HOME/.ssh}/ssh-agent.sock"
local agent_status
# 1. Is there already a usable agent in this terminal's environment?
if [ -n "${SSH_AUTH_SOCK:-}" ]; then
ssh-add -l >/dev/null 2>&1
agent_status=$?
else
agent_status=2
fi
# 2. If not, is there a shared agent already started from another terminal?
if [ "$agent_status" -ge 2 ] && [ -S "$sock" ]; then
SSH_AUTH_SOCK="$sock" ssh-add -l >/dev/null 2>&1
agent_status=$?
[ "$agent_status" -lt 2 ] && export SSH_AUTH_SOCK="$sock"
fi
# 3. If neither works, start a new agent on the fixed socket.
# ssh-add returns 1 when an agent is running but has no identities (normal case).
if [ "$agent_status" -ge 2 ]; then
rm -f "$sock" # stale socket left by a dead agent
if ! ssh-agent -a "$sock" >/dev/null; then
printf 'sshlk: could not start ssh-agent\n' >&2
return 1
fi
export SSH_AUTH_SOCK="$sock"
fi
ssh-add -- "$key"
}
# Screen Shot
#sshot() {
# maim -d 2 -u -g 1920x1080+0+540 $1
#}
# Smart Mount
smount() {
if [ "$#" -ne 1 ]; then
printf 'Usage: smount DEVICE\n' >&2
return 2
fi
udisksctl mount -b "$1"
}
# Smart Unmount
sumount() {
if [ "$#" -ne 1 ]; then
printf 'Usage: sumount DEVICE\n' >&2
return 2
fi
udisksctl unmount -b "$1"
udisksctl power-off -b "$1"
}
# MAKE BASH GREAT AGAIN!
if [ -f /usr/share/bash-completion/bash_completion ]; then
source /usr/share/bash-completion/bash_completion
fi
# Readline
# Match completions regardless of letter case.
bind 'set completion-ignore-case on'
# List all candidates on the first TAB instead of beeping and waiting for a second one.
bind 'set show-all-if-ambiguous on'
# Color completion candidates by file type, using LS_COLORS.
bind 'set colored-stats on'
# When cycling with menu-complete, show the common prefix of all candidates first.
bind 'set menu-complete-display-prefix on'
# TAB cycles through candidates one by one instead of only completing the common prefix.
bind 'TAB:menu-complete'
# Up/Down arrows search history for entries starting with what is already typed.
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
# Never ring the terminal bell.
bind 'set bell-style none'
# Treat - and _ as equivalent when completing, so my-fi<TAB> finds my_file.
bind 'set completion-map-case on'
# Append a trailing slash when completing a symlink that points to a directory.
bind 'set mark-symlinked-directories on'
# Ctrl+Right / Ctrl+Left move the cursor by word.
bind '"\e[1;5C": forward-word'
bind '"\e[1;5D": backward-word'
# Disable flow control so Ctrl+S / Ctrl+Q reach the shell and vim.
stty -ixon 2>/dev/null
# colorful man pages
export LESS_TERMCAP_md=$'\e[1;34m' # bold
export LESS_TERMCAP_us=$'\e[4;32m' # underline
export LESS_TERMCAP_so=$'\e[1;33m' # standout (search hits, status)
export LESS_TERMCAP_me=$'\e[0m'
export LESS_TERMCAP_ue=$'\e[0m'
export LESS_TERMCAP_se=$'\e[0m'
export MANROFFOPT='-P -c'
# History
HISTSIZE=100000 # I like it big
HISTFILESIZE=200000
HISTCONTROL=ignoreboth:erasedups
HISTIGNORE='clear:exit:history:ls:ll'
HISTTIMEFORMAT="%d/%m/%y %H:%M " # Pls add date time to commands
shopt -s histappend # Append and do not replace
# History is appended and synchronized by __history_and_prompt_command.
# Shell behavior
shopt -s checkwinsize
shopt -s cdspell
shopt -s dirspell
shopt -s globstar
path_prepend() {
[[ ":$PATH:" == *":$1:"* ]] || PATH="$1${PATH:+:$PATH}"
}
path_append() {
[[ ":$PATH:" == *":$1:"* ]] || PATH="${PATH:+$PATH:}$1"
}
if command -v zoxide &>/dev/null; then
eval "$(zoxide init bash)"
fi
if command -v fzf &>/dev/null; then
eval "$(fzf --bash)"
if command -v rg &>/dev/null; then
export FZF_DEFAULT_COMMAND='rg --files --hidden --follow --glob "!.git"'
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
fi
export FZF_DEFAULT_OPTS='
--height 40%
--layout=reverse
--border
--preview "if [ -d {} ]; then ls -la -- {}; elif command -v bat >/dev/null 2>&1; then bat --style=numbers --color=always --line-range :500 -- {}; else head -n 500 -- {}; fi"
--preview-window=right:50%:wrap
'
fi
# User binaries
path_append "$HOME/.local/bin"
#Execute my random text art
if command -v rndtxtart &>/dev/null; then
rndtxtart
fi
# Machine-specific configuration
if [[ -r "$HOME/.bashrc.local" ]]; then
source "$HOME/.bashrc.local"
fi
|