1# Theming
2{{- if eq .theme_variant "dark" }}
3# https://github.com/catppuccin/zsh-syntax-highlighting
4source ~/.config/zsh/themes/everforest-hard-dark.zsh
5export BAT_THEME="ansi"
6{{- end }}
7{{- if eq .theme_variant "light" }}
8# https://github.com/catppuccin/zsh-syntax-highlighting
9source ~/.config/zsh/themes/everforest-soft-light.zsh
10export BAT_THEME="ansi"
11{{- end }}
12
13# Remove older command from the history if a duplicate is to be added.
14setopt HIST_IGNORE_ALL_DUPS
15# Prompt for spelling correction of commands.
16setopt CORRECT
17# Customize spelling correction prompt.
18SPROMPT='zsh: correct %F{red}%R%f to %F{green}%r%f [nyae]? '
19# Remove path separator from WORDCHARS.
20WORDCHARS=${WORDCHARS//[\/]}
21# Use degit instead of git as the default tool to install and update modules.
22zstyle ':zim:zmodule' use 'degit'
23# Set a custom prefix for the generated aliases. The default prefix is 'G'.
24#zstyle ':zim:git' aliases-prefix 'g'
25# Append `../` to your input for each `.` you type after an initial `..`
26zstyle ':zim:input' double-dot-expand no
27# Set a custom terminal title format using prompt expansion escape sequences.
28# See http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Simple-Prompt-Escapes
29# If none is provided, the default '%n@%m: %~' is used.
30zstyle ':zim:termtitle' hooks 'preexec' 'precmd'
31zstyle ':zim:termtitle:preexec' format '${${(A)=1}[1]}'
32zstyle ':zim:termtitle:precmd' format '%1~'
33# Customize the style that the suggestions are shown with.
34# Set what highlighters will be used.
35# See https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters.md
36ZSH_HIGHLIGHT_HIGHLIGHTERS=(main cursor)
37# Customize the main highlighter styles.
38# See https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters/main.md#how-to-tweak-it
39typeset -gA ZSH_HIGHLIGHT_STYLES
40
41# XDG variables
42export XDG_DATA_HOME="$HOME/.local/share"
43export XDG_CONFIG_HOME="$HOME/.config"
44export XDG_STATE_HOME="$HOME/.local/state"
45export XDG_CACHE_HOME="$HOME/.cache"
46export XDG_BIN_HOME="$HOME/.local/bin"
47
48# Moving dotfiles around
49zstyle ':completion:*' cache-path "$XDG_CACHE_HOME"/zsh/zcompcache
50export ZDOTDIR="$HOME"/.config/zsh
51export WINEPREFIX="$XDG_DATA_HOME"/wine
52export WORKON_HOME="$XDG_DATA_HOME/virtualenvs"
53export SQLITE_HISTORY="$XDG_CACHE_HOME"/sqlite_history
54export BUNDLE_USER_CONFIG="$XDG_CONFIG_HOME"/bundle
55export BUNDLE_USER_CACHE="$XDG_CACHE_HOME"/bundle
56export BUNDLE_USER_PLUGIN="$XDG_DATA_HOME"/bundle
57export NUGET_PACKAGES="$XDG_CACHE_HOME"/NuGetPackages
58export DVDCSS_CACHE="$XDG_DATA_HOME"/dvdcss
59export KDEHOME="$XDG_CONFIG_HOME"/kde
60export GOPATH="$XDG_DATA_HOME"/go
61export ZIM_HOME="$XDG_CONFIG_HOME"/.zim
62export GHCUP_USE_XDG_DIRS="ishouldjustbeabletoexportthisnotsetit"
63alias wget=wget --hsts-file="$XDG_DATA_HOME/wget-hsts"
64alias svn="svn --config-dir $XDG_CONFIG_HOME/subversion"
65
66fpath=($XDG_CONFIG_HOME/zsh/completions $fpath)
67
68# ------------------
69# Initialize modules
70# ------------------
71if [[ ! -e ${ZIM_HOME}/zimfw.zsh ]]; then
72 # Download zimfw script if missing.
73 command mkdir -p ${ZIM_HOME}
74 if (( ${+commands[curl]} )); then
75 command curl -fsSL -o ${ZIM_HOME}/zimfw.zsh https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh
76 else
77 command wget -nv -O ${ZIM_HOME}/zimfw.zsh https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh
78 fi
79fi
80if [[ ! ${ZIM_HOME}/init.zsh -nt ${ZDOTDIR:-${HOME}}/.zimrc ]]; then
81 # Install missing modules, and update ${ZIM_HOME}/init.zsh if missing or outdated.
82 source ${ZIM_HOME}/zimfw.zsh init -q
83fi
84source ${ZIM_HOME}/init.zsh
85
86# ------------------------------
87# Post-init module configuration
88# ------------------------------
89
90# Bind ^[[A/^[[B manually so up/down works both before and after zle-line-init
91bindkey '^[[A' history-substring-search-up
92bindkey '^[[B' history-substring-search-down
93
94# Bind up and down keys
95zmodload -F zsh/terminfo +p:terminfo
96if [[ -n ${terminfo[kcuu1]} && -n ${terminfo[kcud1]} ]]; then
97 bindkey ${terminfo[kcuu1]} history-substring-search-up
98 bindkey ${terminfo[kcud1]} history-substring-search-down
99fi
100
101bindkey '^P' history-substring-search-up
102bindkey '^N' history-substring-search-down
103bindkey -M vicmd 'k' history-substring-search-up
104bindkey -M vicmd 'j' history-substring-search-down
105# End configuration added by Zim install
106
107# Ruby crap
108eval "$(rbenv init - zsh)"
109export GEM_HOME="$(gem env user_gemhome)"
110export PATH="$PATH:$GEM_HOME/bin"
111eval "$(rbenv init -)"
112
113# Android crap
114export ANDROID_HOME="$XDG_DATA_HOME/Android"
115export ANDROID_SDK_ROOT="$ANDROID_HOME"
116export PATH="$PATH:$ANDROID_HOME/cmdline-tools/latest/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/emulator"
117
118# Executable paths
119export PATH="$PATH:$XDG_CONFIG_HOME/emacs/bin"
120export PATH="$PATH:$HOME/.gem/ruby/3.0.0/bin"
121export PATH="$PATH:$GOPATH/bin"
122export PATH="$PATH:$CACHE/yay/distrobox/pkg/distrobox/usr/bin"
123export PATH="$PATH:$XDG_DATA_HOME/cargo/bin"
124export PATH="$PATH:$HOME/.radicle/bin"
125export PATH="$HOME/.local/bin:$PATH"
126export PATH="$PATH:/usr/lib/kf6"
127export PATH="$PATH:/usr/lib/kf5"
128
129# Additional preferences
130export LANG=en_GB.UTF-8
131export EDITOR="zeditor --wait"
132export VISUAL="$EDITOR"
133export _JAVA_AWT_WM_NONREPARENTING=1
134export MANPAGER="nvim +Man!"
135export COLUMNS=80
136export MANWIDTH=80
137export CHARM_HOST=charm.secluded.site
138export RIPGREP_CONFIG_PATH="$HOME/.config/ripgreprc"
139
140# Partially in response to https://sourcehut.org/blog/2023-01-09-gomodulemirror/
141# Partially because GOPROXY bad
142export GOPROXY=direct
143
144CASE_SENSITIVE="false"
145
146# Disable dumb Python keyring integration
147export PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring
148
149# Authenticate to local Radicle (.xyz) node
150export RAD_PASSWORD='{{ onepasswordRead "op://Private/2ujzijel6ni3np2uj5k5syptzm/password" }}'
151
152export JIRA_API_TOKEN='{{ onepasswordRead "op://Private/Atlassian/jira key" }}'
153
154# LLM Crap
155export TOGETHER_API_KEY='{{ onepasswordRead "op://Private/pjn4bg6prgvta2ncdory42zrta/llm" }}'
156export OPENROUTER_API_KEY='{{ onepasswordRead "op://Private/OpenRouter/shell" }}'
157export OPENAI_API_BASE='https://api.together.xyz/v1'
158export OPENAI_API_KEY=$TOGETHER_API_KEY
159
160#HYPHEN_INSENSITIVE="true"
161# Uncomment the following line if pasting URLs and other text is messed up.
162# DISABLE_MAGIC_FUNCTIONS=true
163
164# Ranger conf
165export RANGER_LOAD_DEFAULT_RC=false
166
167# Bunch of aliases
168alias joe="joe-gitignore"
169alias ts="tailscale"
170alias tmateqr='tmate show-messages | tail -n 1 | qrencode -o - -t ANSIUTF8'
171alias info="info --vi-keys"
172alias datetime="date +%Y-%m-%d_%H%M%S_%Z"
173alias u="linx-client"
174alias clip="xclip -selection clipboard"
175alias tmp="cd $(mktemp -d) && export TEMP=$(pwd)"
176alias send="rsync -amzzP"
177alias bat="bat -n --tabs 2"
178alias mov="joshuto ~/Bulk/Media/Movies"
179alias tv="joshuto ~/Bulk/Media/TV\ Shows"
180alias yt="joshuto ~/Bulk/Media/YouTube"
181alias erase="shred -vzfun 32"
182alias dl="yt-dlp --write-sub --write-auto-sub --sub-lang en --sub-format srt/best --convert-subs srt --embed-subs -o '%(upload_date)s %(title)s.%(ext)s'"
183alias us="unsilence -t 15 -as 1.25"
184alias roll="roll -v"
185alias sxiv="sxiv -p"
186alias c="chezmoi"
187alias t="tea"
188alias j="just"
189alias ac="aicommits"
190alias tc="turbocommit"
191alias ai="sgpt"
192alias k="klog"
193alias ls="lsd"
194
195# Blog-related aliases
196alias secluded="cd ~/repos/personal/secluded && ./gen"
197alias drafts='e $(rg -l "draft: true" $HOME/repos/personal/secluded | fzf --preview "bat --color=always {}" --preview-window "~3")'
198
199# Custom functions
200function gi() {
201 curl -sLw https://www.toptal.com/developers/gitignore/api/$@ ;
202}
203
204
205sshedit() {
206 emulate -L zsh
207 setopt pipefail
208
209 if [[ $# -lt 2 ]]; then
210 echo "Usage: sshedit remote_host remote_file" >&2
211 return 1
212 fi
213
214 local tmpdir=$(mktemp -d)
215 chmod 700 "$tmpdir"
216
217 # Define cleanup function
218 function cleanup() {
219 [[ $PWD == $tmpdir ]] && popd >/dev/null 2>&1
220 [[ -d "$tmpdir" ]] && rm -rf "$tmpdir"
221 }
222 # Set trap but don't use EXIT which might cause terminal closure
223 trap cleanup INT TERM HUP
224
225 pushd "$tmpdir" > /dev/null || { echo "Failed to enter temp directory"; return 1; }
226
227 local remote_host="$1"
228 local remote_file="$2"
229 local local_file="${remote_file:t}" # ZSH way to get basename
230
231 # Check ssh connection
232 if ! ssh -q -o BatchMode=yes -o ConnectTimeout=5 "$remote_host" true; then
233 echo "Error: Cannot connect to $remote_host" >&2
234 cleanup
235 return 1
236 fi
237
238 if ! ssh "$remote_host" "test -f '$remote_file'"; then
239 echo -n "Remote file doesn't exist. Create it? [y/N] " >&2
240 read -q response || { echo "\nAborting" >&2; return 1 }
241 echo
242 ssh "$remote_host" "touch '$remote_file'" || {
243 echo "Failed to create remote file" >&2
244 return 1
245 }
246 fi
247
248 # Download file
249 if ! rsync -az "${remote_host}:${remote_file}" "$local_file"; then
250 echo "Error: Failed to download the remote file" >&2
251 cleanup
252 return 1
253 fi
254
255 local before_checksum=$(sha256sum "$local_file" 2>/dev/null | awk '{print $1}')
256
257 eval "${EDITOR:-vim}" "$local_file"
258 local edit_status=$?
259
260 if [[ $edit_status -ne 0 ]]; then
261 echo "Editor exited with status $edit_status" >&2
262 cleanup
263 return $edit_status
264 fi
265
266 local after_checksum=$(sha256sum "$local_file" 2>/dev/null | awk '{print $1}')
267
268 if [[ "$before_checksum" != "$after_checksum" ]]; then
269 if ! rsync -az "$local_file" "${remote_host}:${remote_file}"; then
270 echo "Error: Failed to upload edited file" >&2
271 cleanup
272 return 1
273 fi
274 echo "File successfully updated on $remote_host" >&2
275 else
276 echo "No changes made to file" >&2
277 fi
278
279 cleanup
280 trap - INT TERM HUP
281 return 0
282}
283
284
285# Navi integration
286_call_navi() {
287 local selected
288 if selected="$(printf "$(navi --print --path ${XDG_DATA_HOME}/navi/cheats </dev/tty)")"; then
289 LBUFFER="$selected"
290 fi
291 zle redisplay
292}
293zle -N _call_navi
294bindkey '^n' _call_navi
295
296zstyle ':completion:*' completer _expand _complete _ignored _correct _approximate
297zstyle ':completion:*' group-name ''
298zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
299zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s
300zstyle ':completion:*' menu select=1
301zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s
302zstyle :compinstall filename '/home/amolith/.config/zsh/.zshrc'
303
304# Additional completions
305eval "$(op completion zsh)"; compdef _op op
306source /usr/share/z/z.sh
307
308precmd_functions=(zvm_init "${(@)precmd_functions:#zvm_init}")
309precmd_functions+=(set-long-prompt)
310zvm_after_init_commands+=("zle -N zle-line-finish; zle-line-finish() { set-short-prompt }")
311
312set-long-prompt() {
313 PROMPT=$(starship prompt)
314 RPROMPT=""
315}
316
317export COLUMNS=$(($COLUMNS + ($COLUMNS*0.1)))
318set-short-prompt() {
319 # setting this doesn't seem to actually work
320 PROMPT="$(STARSHIP_KEYMAP=${KEYMAP:-viins} starship module character)"
321 RPROMPT=$'%{\e[999C%}\e[8D%F{8}%*%f ' # remove if you don't want right prompt
322 zle .reset-prompt 2>/dev/null # hide the errors on ctrl+c
323}
324
325zle-keymap-select() {
326 set-short-prompt
327}
328zle -N zle-keymap-select
329
330zle-line-finish() { set-short-prompt }
331zle -N zle-line-finish
332
333trap 'set-short-prompt; return 130' INT
334
335# try to fix vi mode indication (not working 100%)
336zvm_after_init_commands+=('
337 function zle-keymap-select() {
338 if [[ ${KEYMAP} == vicmd ]] ||
339 [[ $1 = "block" ]]; then
340 echo -ne "\e[1 q"
341 STARSHIP_KEYMAP=vicmd
342 elif [[ ${KEYMAP} == main ]] ||
343 [[ ${KEYMAP} == viins ]] ||
344 [[ ${KEYMAP} = "" ]] ||
345 [[ $1 = "beam" ]]; then
346 echo -ne "\e[5 q"
347 STARSHIP_KEYMAP=viins
348 fi
349 zle reset-prompt
350 }
351 zle -N zle-keymap-select
352
353 # Ensure vi mode is set
354 zle-line-init() {
355 zle -K viins
356 echo -ne "\e[5 q"
357 }
358 zle -N zle-line-init
359')
360
361# Configure history file
362export HISTFILE="$XDG_STATE_HOME"/zsh/history
363export HISTSIZE=1000000
364export SAVEHIST=1000000