-
Notifications
You must be signed in to change notification settings - Fork 0
/
.shellrc
448 lines (386 loc) · 12.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
#!/usr/bin/env bash
SHELL_NAME=${1:-"zsh"}
# 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="$DOTFILES/.config/nvim/lua/plugins/"
# Jump Directories
alias .....="cd ../../../../"
alias ....="cd ../../../"
alias ...="cd ../../"
alias ..="cd .."
alias hub="cd \$GITHUB"
alias icloud="cd \$ICLOUD"
alias jeff="cd \$JEFF"
alias src="cd \$SRC && lla"
# ===============================================================================
# Functions
function exec-in-subdirs() {
for d in */; do
cd ./"$d" || return
echo "=== Executing $1 on $(pwd)"
eval "$1"
cd ..
done
}
# ===============================================================================
# Package Managers
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
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
# ===============================================================================
if command -v asdf &>/dev/null; then
# Extendable version manager : https://github.com/asdf-vm/asdf
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
# ===============================================================================
# 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() {
exec_on_git_repos cd "$1" "$2"
lla
}
function exec_on_git_repos() {
local root_folder="$2"
local exec_command="$1"
local query_value=${3:-""}
# 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" ] && "$exec_command" "$git_repos"
}
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"
alias ten="cd \$SRC/github.com/steveshogren/10-minute-vim-exercises/ && fzf | xargs nvim && cd -"
alias startpage="cd \$SRC/github.com/jeffwindsor/startpage/ && nvim index.html && cd -"
#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
# ===============================================================================
# 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)'
# 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"; }
fi
# ===============================================================================
# editor: https://github.com/helix-editor/helix
if command -v hx &>/dev/null; then
alias h="hx"
fi
# ===============================================================================
if command -v lazygit &>/dev/null; then
alias gg="lazygit"
alias lg="lazygit"
fi
# ===============================================================================
# editor: https://github.com/neovim/neovim
if command -v nvim &>/dev/null; then
function nvim-clean-caches() {
rm -rf "$HOME"/.local/share/nvim
rm -rf "$HOME"/.local/state/nvim
rm -rf "$HOME"/.cache/nvim
}
function fuzzy-search() {
cd "$1"
files=$(fzf_query ${2:-""})
nvim $files
cd -
}
alias v="nvim"
alias vim="nvim"
alias vi="nvim"
alias v.="nvim ."
alias vup="nvim --headless \"+Lazy! sync\" +qa"
alias dot="cd \$DOTFILES"
alias dots="fuzzy-search \$DOTFILES"
alias dotn="cd \$DOTFILES_NIXOS"
alias dotsn="fuzzy-search \$DOTFILES_NIXOS"
alias dotv="cd \$DOTFILES_NVIM"
alias dotsv="fuzzy-search \$DOTFILES_NVIM"
alias exo="cd \$EXOCORTEXT"
alias exos="fuzzy-search \$EXOCORTEXT"
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"
fi
# ===============================================================================
# Prompt : https://github.com/starship/starship
if command -v starship &>/dev/null; then
eval "$(starship init $SHELL_NAME)"
fi
# ===============================================================================
# dotfiles: https://github.com/TheLocehiliosan/yadm
if command -v stow &>/dev/null; then
alias dg="lazygit --path \$DOTFILES"
function dpl() {
git -C $DOTFILES pull -v
git -C $DOTFILES_NIXOS pull -v
git -C $DOTFILES_GNOME pull -v
}
function dsync() {
# clean current links
#stow -D . --dir=$DOTFILES --target=$HOME
# put stowed into $HOME
stow . --dir=$DOTFILES --target=$HOME
}
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
# ===============================================================================
# source machine specific aliases
if test -f "$HOME/.shellrc.local"; then
source "$HOME/.shellrc.local"
fi
function up() {
echo "==> Pull Important Repos"
dpl
git -C $JEFF/startpage pull
echo "==> UP homebrew database and packages"
bup
echo "==> UP zinit"
zup
echo "==> UP lazyvim"
vup
}