-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.zshrc
More file actions
373 lines (310 loc) · 10.9 KB
/
.zshrc
File metadata and controls
373 lines (310 loc) · 10.9 KB
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
# Enable completion
autoload -Uz compinit add-zsh-hook
compinit
add-zsh-hook chpwd auto_activate_venv
# History
HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=10000
setopt HIST_IGNORE_ALL_DUPS
setopt SHARE_HISTORY
# Tab completion tweaks
zstyle ':completion:*' menu select
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'
zstyle ':completion:*' group-name ''
zstyle ':completion:*:descriptions' format '%F{green}%d%f'
zstyle ':completion:*' list-colors ''
# Options
setopt autocd # cd into directories without typing 'cd'
setopt correct # auto-correct commands
setopt no_beep # disable bell
setopt prompt_subst # allow command substitution in prompt
bindkey -e # enable emacs bindings
# Ctrl+Left/Right word movement
bindkey "^[[1;5D" backward-word # Ctrl+Left
bindkey "^[[1;5C" forward-word # Ctrl+Right
# Alternate keycodes for some terminals
bindkey "^[OD" backward-word
bindkey "^[OC" forward-word
bindkey "\e[H" beginning-of-line # Home
bindkey "\e[F" end-of-line # End
# Make Alt+Backspace behave like bash
bindkey '^[^?' backward-kill-word
WORDCHARS=''
# Make Shift+Tab go backwards for autocomplete
bindkey '^[[Z' reverse-menu-complete
fzf_git_files() {
local file
file=$(git diff --name-only | fzf --multi) || return
LBUFFER+="$file "
zle reset-prompt
}
zle -N fzf_git_files
bindkey '^D' fzf_git_files
# Prompt
ZSH_FIRST_PROMPT=1
autoload -Uz colors && colors
# Fast dirty check via git porcelain
parse_git_info() {
local branch dirty marks
command git rev-parse --is-inside-work-tree &>/dev/null || return
branch=$(git symbolic-ref --short HEAD 2>/dev/null)
[[ -z $branch ]] && return
marks=""
dirty=$(git status --porcelain 2>/dev/null)
[[ $dirty == *"M "* ]] && marks+="!"
[[ $dirty == *"?? "* ]] && marks+="?"
[[ $dirty == *"A "* ]] && marks+="+"
[[ $dirty == *"D "* ]] && marks+="x"
[[ $(git rev-list --count --left-only @{u}...HEAD 2>/dev/null) -gt 0 ]] && marks+="*"
echo " [$branch${marks:+ $marks}]"
}
# Get current terraform workspace
parse_terraform_workspace() {
local workspace
# Ignore directories that are not initialized with terraform.
[[ -d ".terraform" ]] || return
# Get the current workspace (suppress errors if terraform is not available or not in terraform dir)
workspace=$(terraform workspace show 2>/dev/null)
[[ -z $workspace ]] && return
echo " (tf:$workspace)"
}
activate_default_venv() {
source ~/code/onyx/.venv/bin/activate
}
# Automatically activate a Python virtual environment if one exists
auto_activate_venv() {
# Look for a virtual environment folder in the current directory
if [[ -f ".venv/bin/activate" ]]; then
# Only activate if not already active
if [[ -z "$VIRTUAL_ENV" || "$VIRTUAL_ENV" != "$PWD/.venv" ]]; then
source .venv/bin/activate
# Refresh autocomplete to pick up any new binaries.
compinit
fi
else
# Deactivate if leaving a directory with a venv
if [[ -n "$VIRTUAL_ENV" && "$VIRTUAL_ENV" == "$OLDPWD/.venv" ]]; then
deactivate
activate_default_venv
fi
fi
}
activate_default_venv
kube_context_info() {
local ctx short
ctx="$(kubectl config current-context 2>/dev/null)"
[[ -z "$ctx" ]] && return
short="${ctx##*/}"
echo " ⎈ $short"
}
precmd() {
local exit_code=$?
local git_info host_info terraform_info
git_info=$(parse_git_info)
terraform_info=$(parse_terraform_workspace)
kube_info=$(kube_context_info)
host_info=""
[[ -n $SSH_CONNECTION ]] && host_info=" (%{$fg[yellow]%}$(hostname)%{$reset_color%})"
local color=$fg[green]
(( exit_code != 0 )) && color=$fg[red]
# Only prepend newline if this is not the first prompt
local newline=""
(( ZSH_FIRST_PROMPT == 0 )) && newline=$'\n'
PROMPT="${newline}[%{$color%}$exit_code%{$reset_color%}] %{$fg[blue]%}%~%{$reset_color%}%{$fg[green]%}$git_info%{$reset_color%}%{$fg[cyan]%}$terraform_info%{$reset_color%}%{$fg[yellow]%}$kube_info%{$reset_color%}$host_info %D{%F %T}"
PROMPT+=$'\n'"${PROMPT_CHAR:-$([[ $EUID -eq 0 ]] && echo '#' || echo '$')} "
ZSH_FIRST_PROMPT=0
}
# Aliases
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias clear='clear && ZSH_FIRST_PROMPT=1'
alias gs='git status'
alias gl="git log --graph --pretty='%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'"
alias gr='git reset --soft HEAD~1 && git commit --amend --no-edit'
alias gg='git log --graph --oneline --all --decorate'
alias gb='git for-each-ref --sort=-committerdate refs/heads/'
alias gt='git log --no-walk --tags --pretty="%h %d %s" --decorate=full'
alias grep='grep --color=auto'
alias kc="kubectl"
alias nit='git commit -am "nit"'
alias kbdoff="sudo sys76-kb set -b 0"
alias less='less -R'
alias ls='ls --color=auto'
alias ll='ls -lh'
alias rg='rg --color=always'
alias power="upower -i /org/freedesktop/UPower/devices/battery_BAT0"
alias stop="pkill -STOP"
alias resume="pkill -CONT"
alias hostname="cat /etc/hostname"
alias update="sudo nixos-rebuild switch"
alias vim="nvim"
alias kitty="kitty --session ~/.config/kitty/startup-session.conf"
alias zwift="CONTAINER_TOOL='podman' zwift"
# TODO: Remove python3.12 once https://github.com/Aider-AI/aider/issues/3660 is resolved.
alias aider="uvx --python=3.12 --from=aider-chat aider"
alias ruff="uvx ruff"
alias openhands="uvx --python=3.12 --from=openhands-ai openhands"
alias pkillgrep='function _pg() { ps aux | grep "$1" | grep -v grep | awk "{print \$2}" | xargs -r kill; }; _pg'
alias enable="swaymsg output eDP-1 enable"
alias disable="swaymsg output eDP-1 disable"
alias snip="slurp | grim -g -"
alias snap="sleep 2 && swaymsg -t get_tree | jq -r '.. | select(.focused?) | .rect | \"\(.x),\(.y) \(.width)x\(.height)\"' | grim -g -"
alias claude="claude --dangerously-skip-permissions"
alias chat="ollama run mistral-small3.2:latest"
alias coder="ollama run qwen3-coder:latest"
alias weak="ollama run jqwen3:0.6b"
alias sober='echo $(( ($(date +%s) - $(date -d '2025-04-15' +%s)) / 86400 ))'
alias lights='smart-lights'
alias awslogin="aws sso login"
alias venv="uv venv .venv --python 3.11 --allow-existing && source .venv/bin/activate"
alias activate="source .venv/bin/activate"
alias dot='/usr/bin/git --git-dir=$HOME/.dotfiles --work-tree=$HOME'
# Functions
function home() {
export TEMP_HOME="$PWD"
}
function cd() {
HOME="${TEMP_HOME:=$HOME}" builtin cd "$@"
}
function rgplace() {
if [[ $# -lt 2 ]]; then
echo "Usage: rgplace <search_pattern> <replacement> [file_pattern]"
echo "Example: rgplace 'foo' 'bar' '*.txt'"
return 1
fi
local search_pattern=$1
local replacement=$2
local file_pattern=$3
if [ -z "$file_pattern" ]; then
file_pattern="*"
fi
rg --color=never --files-with-matches "$search_pattern" --glob "$file_pattern" | while read -r file; do
sed -i "s|$search_pattern|$replacement|g" "$file"
done
}
function ga() {
local message="$1"
if [ -z "$message" ]; then
>&2 echo "Commit message is required."
return 2
fi
git commit --amend -m "${message}"
}
function gsp() {
local subtree="${1:-}"
shift
__gsubtree push "$subtree" "$@"
}
function gspull() {
local subtree="${1}"
shift
__gsubtree pull "$subtree" --squash "$@"
}
function __gsubtree() {
local cmd="${1}"
shift
local subtree="${1:-}"
shift
local toplevel
toplevel="$(git rev-parse --show-toplevel)"
if [ -z "$subtree" ]; then
>&2 echo "Missing argument 'subtree'."
echo "Pick one of:"
# https://stackoverflow.com/a/18339297
git log | grep git-subtree-dir | tr -d ' ' | cut -d ":" -f2 | sort | uniq | xargs -I {} bash -c 'if [ -d $(git rev-parse --show-toplevel)/{} ] ; then echo " {}"; fi'
return 2
fi
git -C "$toplevel" subtree "$cmd" --prefix "$subtree" "git@github.com:jmelahman/$(basename "${subtree}").git" master "$@"
}
grb() {
local input="$1"
local remote="${input%%:*}"
local branch="${input#*:}"
if [[ -z "$remote" || -z "$branch" ]]; then
echo "Usage: grb user:branch"
return 1
fi
# infer repo URL from origin
local origin url repo_base new_remote_url
url=$(git remote get-url origin 2>/dev/null) || {
echo "Not a git repo or origin missing."
return 1
}
if [[ "$url" =~ github.com[:/](.*)/(.*)(\.git)?$ ]]; then
repo_base="${match[1]}"
repo_name="${match[2]}"
else
echo "Could not parse GitHub URL from origin: $url"
return 1
fi
new_remote_url="git@github.com:${remote}/${repo_name}"
# add remote if needed
if ! git remote get-url "$remote" >/dev/null 2>&1; then
echo "Adding remote $remote → $new_remote_url"
git remote add "$remote" "$new_remote_url"
fi
echo "Fetching $remote..."
git fetch "$remote" "$branch"
echo "Checking out $branch from $remote..."
git checkout -B "$branch" "$remote/$branch"
}
# Kitty init
KITTY_SHELL_INTEGRATION="${KITTY_INSTALLATION_DIR:=/usr/lib/kitty}/shell-integration/$(basename $SHELL)/kitty.zsh"
if [ -f "$KITTY_SHELL_INTEGRATION" ]; then
source "$KITTY_SHELL_INTEGRATION"
elif [ -x "$(command -v kitty)" ]; then
source <(kitty +kitten shell-integration)
fi
# FZF init
if [ -x "$(command -v fzf)" ] && [ -r /usr/share/fzf/key-bindings.zsh ]
then
source /usr/share/fzf/key-bindings.zsh
fi
# Node Version Manager init
if [ -r /usr/share/nvm/init-nvm.sh ]
then
source /usr/share/nvm/init-nvm.sh
fi
# Load env
if [ -f "$HOME/.env" ]; then
while read -r line; do
export "$line"
done < "$HOME/.env"
fi
export NVM_DIR="$HOME/.nvm"
[ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \. "/opt/homebrew/opt/nvm/nvm.sh" # This loads nvm
[ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && \. "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion
# Vim as default
export EDITOR="vim"
# Color LS output to differentiate between directories and files
export LS_OPTIONS="--color=auto"
export CLICOLOR="Yes"
export LSCOLOR=""
export GIT_QUIET=true
# Customize Path
export GOPATH="$HOME/.go"
export GOBIN="$GOPATH/bin"
export PATH=$HOME/code/monorepo/tools/bin:$HOME/.local/bin:$GOBIN:$PATH
export GRIM_DEFAULT_DIR="~/Pictures"
if [ -z "$SSH_AUTH_SOCK" ]; then
SSH_AUTH_SOCK=$(systemctl --user show-environment | grep SSH_AUTH_SOCK | cut -d= -f2)
export SSH_AUTH_SOCK=${SSH_AUTH_SOCK:-$XDG_RUNTIME_DIR/ssh-agent.socket}
fi
# https://wiki.archlinux.org/title/Docker#Rootless_Docker_daemon
export DOCKER_HOST="unix://$XDG_RUNTIME_DIR/docker.sock"
export DOCKER_SOCK_PATH="$XDG_RUNTIME_DIR/docker.sock"
if [ -f /.dockerenv ]; then
export IN_DOCKER=true
else
export IN_DOCKER=false
fi
export BUILDX_BAKE_ENTITLEMENTS_FS=0
# TODO: npm install changes the lockfile on Linux, https://github.com/onyx-dot-app/onyx/issues/7381
export SKIP=npm-install-check
export IMAGE_TAG=edge
export AWS_PROFILE="jamison"
export HOST_PORT_80="8888"
#export OLLAMA_HOST=http://ollama.home
#export OLLAMA_API_BASE="$OLLAMA_HOST"
# For torch with AMD GPU
export HSA_OVERRIDE_GFX_VERSION=11.0.0