-
Notifications
You must be signed in to change notification settings - Fork 0
/
.shellrc
562 lines (484 loc) · 15.9 KB
/
.shellrc
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#!/usr/bin/env bash
SHELL_NAME=${1:-"zsh"}
export EDITOR="hx"
export DOCKER_HOST="unix://$HOME/.colima/default/docker.sock"
# xdg-ify
export XDG_STATE_HOME=$HOME/.local/state
export XDG_DATA_HOME=$HOME/.local/share
export XDG_CACHE_HOME=$HOME/.cache
export XDG_CONFIG_HOME=$HOME/.config
# personal
export SRC=$HOME/Source
export GITHUB="$SRC/github.com"
export GITCJ="$SRC/gitlab.cj.dev"
export ICLOUD="$HOME/Library/Mobile Documents/com~apple~CloudDocs/Documents"
export JEFF="$SRC/github.com/jeffwindsor"
export EXOCORTEXT="$HOME/portable/exocortex"
export DOTFILES="$JEFF/dotfiles"
export DOTFILES_NIXOS="$JEFF/nixos-config"
export DOTFILES_GNOME="$JEFF/gnome-config"
export DOTFILES_NVIM="$XDG_CONFIG_HOME/nvim"
# Jump Directories
alias .....="cd ../../../../"
alias ....="cd ../../../"
alias ...="cd ../../"
alias ..="cd .."
alias config="cd $XDG_CONFIG_HOME"
alias hub="cd \$GITHUB"
alias icloud="cd \$ICLOUD"
alias jeff="cd \$JEFF"
alias src="cd \$SRC && lla"
alias dot="cd \$DOTFILES"
alias dotn="cd \$DOTFILES_NIXOS"
alias dotv="cd \$DOTFILES_NVIM"
alias exo="cd \$EXOCORTEXT"
alias dots="edit-via-fuzzy-search \$DOTFILES"
alias dotsn="edit-via-fuzzy-search \$DOTFILES_NIXOS"
alias dotsv="edit-via-fuzzy-search \$DOTFILES_NVIM"
alias exos="edit-via-fuzzy-search \$EXOCORTEXT"
alias ten="cd \$SRC/github.com/steveshogren/10-minute-vim-exercises/ && fzf | xargs $EDITOR && cd -"
alias startpage="cd \$SRC/github.com/jeffwindsor/startpage/ && $EDITOR index.html && cd -"
# lowers confusion on target machines, they may not know Alacritty, Wezterm, etc
alias ssh="TERM=xterm-256color ssh"
#===============================================================================
# Package Managers
#===============================================================================
#== Homebrew: https://brew.sh/ ==
if command -v brew &>/dev/null; then
# only auto-updates db every 12+ hours
export HOMEBREW_AUTO_UPDATE_SECS=43200
# initialize
eval "$($(which brew) shellenv)"
function brew-list-all() {
echo '== Formula Leaves =='
brew leaves
echo
echo '== Casks =='
# -1 is to 'Force output to be one entry per line'
brew list -1 --casks
}
alias bi="brew info"
alias bl="brew-list-all"
alias blc="brew list --casks --versions"
alias blf="brew list --formula --versions"
alias bn="brew install"
alias br="brew remove"
alias bs="brew search"
alias bsd="brew search --desc"
function bup() {
brew update
brew upgrade
}
alias bx="brew autoremove"
if type fuzzy_brew_install &>/dev/null; then
# requires fzf-brew zsh plugin or equivilant be installed
alias bn_="fuzzy_brew_install"
alias br_="fuzzy_brew_uninstall"
alias bn__="fuzzy_cask_install"
alias br__="fuzzy_cask_uninstall"
fi
#== Nix https://nixos.org/ ==
elif command -v nix &>/dev/null; then
# standard nix
alias ni="nix-env --install"
alias nl="nix-env -q"
alias nlg="nix-env --list-generations"
alias nr="nix-env --uninstall"
alias ns="nix-shell -p"
alias nup="nix-env --upgrade"
alias ngc="nix-store --gc"
# nix-darwin (macos)
if command -v darwin-rebuild &>/dev/null; then
alias rebuild="darwin-rebuild build --flake \$XDG_CONFIG_HOME/nix"
# alias nds="nix run nix-darwin -- switch --flake ~/.config/nix"
#nixos
elif [ -d "/etc/nixos" ]; then
alias clean_gens="nix-env --delete-generations +5 && nix-collect-garbage"
alias list_gens="sudo nix-env --list-generations --profile /nix/var/nix/profiles/system"
alias optimize="nix optimise-store"
alias rebuild="sudo nixos-rebuild switch"
alias upgrade="sudo nixos-rebuild switch --upgrade"
fi
fi
#===============================================================================
# Development
#===============================================================================
#==ASDF (Extendable version manager): https://github.com/asdf-vm/asdf
if command -v asdf &>/dev/null; then
if command -v "$(brew --prefix asdf)/libexec/asdf.sh" &>/dev/null; then
# node
export NPM_CONFIG_USERCONFIG=$XDG_CONFIG_HOME/npm/npmconfig
export NPM_PACKAGES=$HOME/.npm-packages
export NVM_DIR="$XDG_DATA_HOME"/nvm
source "$(brew --prefix asdf)/libexec/asdf.sh"
fi
# Install one or more versions of specified language
asdf_add() {
local lang=${1}
if [[ ! $lang ]]; then
lang=$(asdf plugin-list | fzf)
fi
if [[ $lang ]]; then
local versions
versions=$(asdf list-all "$lang" | fzf --tac --no-sort --multi)
if [[ $versions ]]; then
for version in $(echo $versions); do asdf install $lang $version; done
fi
fi
}
# Remove one or more versions of specified language
asdf_rm() {
local lang=${1}
if [[ ! $lang ]]; then
lang=$(asdf plugin-list | fzf)
fi
if [[ $lang ]]; then
local versions
versions=$(asdf list $lang | fzf -m)
if [[ $versions ]]; then
for version in $(echo $versions); do asdf uninstall $lang $version; done
fi
fi
}
fi
#==Git (Source Control): https://github.com/git/git
if command -v git &>/dev/null; then
export GIT_LOG_PRETTY_FORMAT='%C(green)%h%C(reset) - %s%C(cyan) | %an%C(dim) | %ch%C(auto)%d%C(reset)'
# pull personal repositories that I want up to date
function dpl() {
# -C specifies location for git to act on, allows running of this command from anywhere
git -C $DOTFILES pull -v
git -C $DOTFILES_NIXOS pull -v
git -C $DOTFILES_GNOME pull -v
git -C $JEFF/nvim_kickstart pull -v
git -C $JEFF/nvim_mini pull -v
git -C $JEFF/startpage pull -v
git -C $JEFF/ascii-art pull -v
}
# used at work because there is a limit on how many pulls per sec
function git-rec() {
for d in */; do
echo "==> $d"
sleep 5
cd ./"$d" || return
git pull
cd ..
done
}
alias gclean="git clean -xdf"
alias gls="git ls-tree --full-tree --name-only -r HEAD."
alias gs="git status"
alias gpl="git pull"
# git blame (more precise)
alias gb="git blame -w -C -C -C"
# git blame line range
alias gbl="git blame -w -C -C -C -L"
# git diff by words changed
alias gdw="git diff --word-diff"
alias gm="git maintenance start"
# git clone from git hub to my source directory
function ghub() { git clone [email protected]:"$1" "$SRC"/github.com/"$1"; }
# git clone from git hub to my source directory
function glab() { git clone [email protected]:"$1" "$SRC"/gitlab.cj.dev/"$1"; }
#==Lazygit (Git TUI): https://github.com/jesseduffield/lazygit ==
if command -v lazygit &>/dev/null; then
alias gg="lazygit"
alias lg="lazygit"
alias dg="lazygit --path \$DOTFILES"
fi
fi
#===============================================================================
# Editors
#===============================================================================
#== Helix: https://github.com/helix-editor/helix
if command -v hx &>/dev/null; then
alias h="hx"
alias h.="hx ."
fi
#===============================================================================
#== Neovim: https://github.com/neovim/neovim
if command -v nvim &>/dev/null; then
function nvim-clean-caches() {
echo "Cleaning current caches"
rm -rf "$HOME"/.local/share/nvim
rm -rf "$HOME"/.local/state/nvim
rm -rf "$HOME"/.cache/nvim
}
function nvim-switch-config() {
if [[ -n "$1" ]]; then
nvim-clean-caches
echo "Moving Config to $1"
rm "$XDG_CONFIG_HOME/nvim"
ln -s "$XDG_CONFIG_HOME/nvim_$1" "$XDG_CONFIG_HOME/nvim"
else
echo "No target config given"
fi
}
function nvim-switch-config-selection() {
local SELECTION
SELECTION=$(printf "lazynvim\nmini\nkickstart\nbespoke" | fzf)
if [[ -n "$SELECTION" ]]; then
nvim-switch-config "$SELECTION"
else
echo "No nvim config selection made"
fi
}
alias nsc="nvim-switch-config-selection"
alias v="nvim"
alias vim="nvim"
alias vi="nvim"
alias v.="nvim ."
alias vup="nvim --headless \"+Lazy! sync\" +qa"
fi
#===============================================================================
# Terminal
#===============================================================================
#== cat replacement (rust): https://github.com/sharkdp/bat
if command -v bat &>/dev/null; then
alias cat="bat --style=plain"
fi
#===============================================================================
#== ls replacement (rust): https://github.com/eza-community/eza
if command -v eza &>/dev/null; then
alias la="eza -F --group-directories-first --git --all --no-user"
alias ll="eza -lF --group-directories-first --no-user"
alias lla="eza -lF --group-directories-first --git --all --no-user"
alias l="clear && lla"
alias tree="eza --tree --git"
alias treea="eza --tree --git --all"
alias treed="eza --tree --git --level"
else
alias l="ls"
alias la="ls -a"
alias ll="ls -l"
alias lla="ls -la"
fi
#===============================================================================
#== bling: https://github.com/fastfetch-cli/fastfetch
if command -v fastfetch &>/dev/null; then
alias ff="fastfetch"
fi
#===============================================================================
#== fuzzy finder: https://github.com/junegunn/fzf
if command -v fzf &>/dev/null; then
export FZF_DEFAULT_COMMAND='fd --type f --hidden' #--ignore-file ~/.config/fd/ignore'
export FZF_DEFAULT_OPTS='--info=inline --reverse --border none --preview "bat {}" --preview-window down'
eval "$(fzf --$SHELL_NAME)"
function fzf_query() {
# passed in query
if [ -n "$1" ]; then
fzf --query "$1"
else
fzf
fi
}
function fzf_query_piped() {
# passed in query
if [ -n "$1" ]; then
fzf --query "$1" </dev/stdin
else
fzf </dev/stdin
fi
}
# prompt user to select from list of git directories under $2 and if user selects one, execute command $1 on it
function cd_on_git_rep() {
local root_folder="$1"
local query_value=${2:-""}
# find list of directories containing a .git subfolder, presumably a repo
local git_repos
git_repos=$(fd '^.git$' "$root_folder" -HItd --max-depth 4 | sed 's/.git\/$//' | fzf_query_piped $query_value)
[ -n "$git_repos" ] && cd "$git_repos" && lla
}
fzf_rg() {
if [ ! "$#" -gt 0 ]; then
echo "Need a string to search for!"
return 1
fi
# if FZF_PREVIEW_WINDOW is undefined, quoting it breaks the script
rg --files-with-matches --no-messages "$1" | fzf --preview-window "$FZF_PREVIEW_WINDOW" --preview "rg --ignore-case --pretty --context 10 '$1' {}"
}
alias fr="fzf_rg"
alias hubs="cd_on_git_rep \$GITHUB"
alias srcs="cd_on_git_rep \$SRC"
#terminal theming
if command -v wezterm &>/dev/null; then
function theme-wezterm() {
local color_scheme
color_scheme=$(fzf <"$XDG_CONFIG_HOME"/wezterm/color_schemes.txt)
# if color_scheme was selected, change the wezterm config file colorscheme value
[ -n "$color_scheme" ] && sd '^config.color_scheme.*$' "config.color_scheme = '${color_scheme}'" "$XDG_CONFIG_HOME"/wezterm/wezterm.lua
}
fi
if command -v alacritty &>/dev/null; then
alias theme=theme-alacritty
function theme-alacritty() {
local selected
selected=$(fd -i .toml $XDG_CONFIG_HOME/alacritty/themes/ | fzf | sd "$HOME" "~")
# if color_scheme was selected, change the alacritty config file colorscheme value
[ -n "$selected" ] && sd "^import.*" "import=[\"$selected\"]" $XDG_CONFIG_HOME/alacritty/alacritty.toml
}
fi
fi
#===============================================================================
#== GREP replacement: https://github.com/BurntSushi/ripgrep
if command -v rg &>/dev/null; then
export RIPGREP_CONFIG_PATH="$XDG_CONFIG_HOME/ripgrep/ripgreprc"
alias grep="rg"
alias ar="alias | rg"
alias er="env | rg"
alias fr="declare -f | rg"
if command -v sd &>/dev/null; then
function replace_text() {
if [ $# -eq 0 ]; then
echo "Usage: replace_text <search_pattern>"
return 1
fi
local search_pattern="$1"
# Find matches using `rg`
echo "Searching for '$search_pattern' in text files..."
rg --color=always --line-number "$search_pattern"
if [ $? -ne 0 ]; then
echo "No matches found for '$search_pattern'."
return 1
fi
# Prompt for replacement text
echo -n "Enter replacement text: "
read -r replacement_text
# Quit if no replacement text is entered
if [[ -z "$replacement_text" ]]; then
echo "No replacement text entered. Exiting."
return 1
fi
# Replace using `sd`
echo "Performing replacements..."
rg --files-with-matches "$search_pattern" | while read -r file; do
sd "$search_pattern" "$replacement_text" "$file"
echo "Replaced in: $file"
done
echo "Replacement completed."
}
fi
fi
#===============================================================================
#== Prompt : https://github.com/starship/starship
if command -v starship &>/dev/null; then
eval "$(starship init $SHELL_NAME)"
fi
#===============================================================================
#== dotfiles:
if command -v stow &>/dev/null; then
# cleans old links, and puts new links from a stow location ($1) to a root ($2)
function stow-location-sync() {
[ -d "$2" ] || mkdir -p "$2"
echo "Clean and Restore $1 into $2 (with stow)"
# clean old links
stow -D . --dir="$1" --target="$2"
# link to source (no-folding forces links at folder level)
stow . --dir="$1" --target="$2"
}
# sync all dotfile repos to appropriate home locations
function dsync() {
stow-location-sync "$DOTFILES" "$HOME"
# stow-location-sync "$JEFF/nvim_lazyvim" "$XDG_CONFIG_HOME/nvim_lazyvim"
# stow-location-sync "$JEFF/nvim_kickstart" "$XDG_CONFIG_HOME/nvim_kickstart"
# stow-location-sync "$JEFF/nvim_mini" "$XDG_CONFIG_HOME/nvim_mini"
}
fi
#===============================================================================
#== terminal multiplexer : https://github.com/tmux/tmux
if command -v tmux &>/dev/null; then
alias t="tmux"
alias tl="tmux ls" # list
alias ta="tmux attach" # attach all sessions or number
alias tan="tmux attach -t" # attach to named
alias tn="tmux new -s" # new with name
alias tk="tmux kill-session"
alias tkn="tmux kill-session -t"
fi
#===============================================================================
#== tui file manager: https://github.com/sxyazi/yazi
if command -v yazi &>/dev/null; then
# opens yazi either at the given directory or at the one zoxide suggests
yazi-zoxide() {
if [ "$1" != "" ]; then
if [ -d "$1" ]; then
yazi "$1"
else
yazi "$(zoxide query $1)"
fi
else
yazi
fi
return $?
}
# open yazi and shell change to the selected directory on exit
function yazi-change-dir() {
local tmp
tmp="$(mktemp -t "yazi-cwd.XXXXX")"
yazi "$@" --cwd-file="$tmp"
if cwd="$(cat -- "$tmp")" && [ -n "$cwd" ] && [ "$cwd" != "$PWD" ]; then
cd -- "$cwd" || return
fi
rm -f -- "$tmp"
}
alias y="yazi-zoxide"
alias yy="yazi-change-dir"
fi
if command -v zinit &>/dev/null; then
alias zclean="zinit delete --clean"
alias zup="zinit update --all"
alias zsync="zclean && zup"
fi
if command -v docker &>/dev/null; then
function docker_run_on_git_repo() {
local root_folder="$1"
local query_value=${2:-""}
# find list of directories containing a .git subfolder, presumably a repo
local git_repo
git_repo=$(fd '^.git$' "$root_folder" -HItd --max-depth 4 | sed 's/.git\/$//' | fzf_query_piped $query_value)
if [ -n "$git_repo" ]; then
local docker_image
docker_image=$(docker images --format '{{.Repository}}:{{.Tag}}' | fzf)
if [ -n "$docker_image" ]; then
echo "docker run -it -v $git_repo:/workspace $docker_image"
fi
fi
}
alias dcl='docker container ls'
alias dil='docker image ls'
alias dir='docker image rm -f'
alias dr='docker run -it '
alias srcsd="docker_run_on_git_repo \$SRC"
fi
#===============================================================================
# Functions
function exec-in-subdirs() {
for d in */; do
cd ./"$d" || return
echo "=== Executing $1 on $(pwd)"
eval "$1"
cd ..
done
}
function edit-via-fuzzy-search() {
cd "$1"
files=$(fzf_query ${2:-""})
$EDITOR "$files"
cd -
}
# combined "upgrade" my stuff function
function up() {
echo "==> Pull Important Repos"
dpl
echo "==> Dotfiles"
dsync
echo "==> Homebrew"
bup
echo "==> Zsh"
zup
}
#===============================================================================
# source machine specific aliases
if test -f "$HOME/.shellrc.local"; then
source "$HOME/.shellrc.local"
fi