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
 48export NODE_PATH="$XDG_DATA_HOME/npm-packages/lib/node_modules"
 49export PATH="$PATH:$XDG_DATA_HOME/npm-packages/bin"
 50
 51# Moving dotfiles around
 52zstyle ':completion:*' cache-path "$XDG_CACHE_HOME"/zsh/zcompcache
 53export ZDOTDIR="$HOME"/.config/zsh
 54export WINEPREFIX="$XDG_DATA_HOME"/wine
 55export WORKON_HOME="$XDG_DATA_HOME/virtualenvs"
 56export SQLITE_HISTORY="$XDG_CACHE_HOME"/sqlite_history
 57export BUNDLE_USER_CONFIG="$XDG_CONFIG_HOME"/bundle
 58export BUNDLE_USER_CACHE="$XDG_CACHE_HOME"/bundle
 59export BUNDLE_USER_PLUGIN="$XDG_DATA_HOME"/bundle
 60export NUGET_PACKAGES="$XDG_CACHE_HOME"/NuGetPackages
 61export DVDCSS_CACHE="$XDG_DATA_HOME"/dvdcss
 62export KDEHOME="$XDG_CONFIG_HOME"/kde
 63export GOPATH="$XDG_DATA_HOME"/go
 64export ZIM_HOME="$XDG_CONFIG_HOME"/.zim
 65export GHCUP_USE_XDG_DIRS="ishouldjustbeabletoexportthisnotsetit"
 66alias wget=wget --hsts-file="$XDG_DATA_HOME/wget-hsts"
 67alias svn="svn --config-dir $XDG_CONFIG_HOME/subversion"
 68
 69fpath=($XDG_CONFIG_HOME/zsh/completions $fpath)
 70
 71# ------------------
 72# Initialize modules
 73# ------------------
 74if [[ ! -e ${ZIM_HOME}/zimfw.zsh ]]; then
 75  # Download zimfw script if missing.
 76  command mkdir -p ${ZIM_HOME}
 77  if (( ${+commands[curl]} )); then
 78    command curl -fsSL -o ${ZIM_HOME}/zimfw.zsh https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh
 79  else
 80    command wget -nv -O ${ZIM_HOME}/zimfw.zsh https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh
 81  fi
 82fi
 83if [[ ! ${ZIM_HOME}/init.zsh -nt ${ZDOTDIR:-${HOME}}/.zimrc ]]; then
 84  # Install missing modules, and update ${ZIM_HOME}/init.zsh if missing or outdated.
 85  source ${ZIM_HOME}/zimfw.zsh init -q
 86fi
 87source ${ZIM_HOME}/init.zsh
 88
 89# ------------------------------
 90# Post-init module configuration
 91# ------------------------------
 92
 93# Bind ^[[A/^[[B manually so up/down works both before and after zle-line-init
 94bindkey '^[[A' history-substring-search-up
 95bindkey '^[[B' history-substring-search-down
 96
 97# Bind up and down keys
 98zmodload -F zsh/terminfo +p:terminfo
 99if [[ -n ${terminfo[kcuu1]} && -n ${terminfo[kcud1]} ]]; then
100  bindkey ${terminfo[kcuu1]} history-substring-search-up
101  bindkey ${terminfo[kcud1]} history-substring-search-down
102fi
103
104bindkey '^P' history-substring-search-up
105bindkey '^N' history-substring-search-down
106bindkey -M vicmd 'k' history-substring-search-up
107bindkey -M vicmd 'j' history-substring-search-down
108# End configuration added by Zim install
109
110# Ruby crap
111eval "$(rbenv init - zsh)"
112export GEM_HOME="$(gem env user_gemhome)"
113export PATH="$PATH:$GEM_HOME/bin"
114eval "$(rbenv init -)"
115
116# Android crap
117export ANDROID_HOME="$XDG_DATA_HOME/Android"
118export ANDROID_SDK_ROOT="$ANDROID_HOME"
119export PATH="$PATH:$ANDROID_HOME/cmdline-tools/latest/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/emulator"
120
121# Executable paths
122export PATH="$PATH:$XDG_CONFIG_HOME/emacs/bin"
123export PATH="$PATH:$HOME/.gem/ruby/3.0.0/bin"
124export PATH="$PATH:$GOPATH/bin"
125export PATH="$PATH:$CACHE/yay/distrobox/pkg/distrobox/usr/bin"
126export PATH="$PATH:$XDG_DATA_HOME/cargo/bin"
127export PATH="$PATH:$HOME/.radicle/bin"
128export PATH="$HOME/.local/bin:$PATH"
129export PATH="$PATH:/usr/lib/kf6"
130export PATH="$PATH:/usr/lib/kf5"
131
132# Additional preferences
133export LANG=en_GB.UTF-8
134export EDITOR="zeditor --wait"
135export VISUAL="$EDITOR"
136export _JAVA_AWT_WM_NONREPARENTING=1
137export MANPAGER="nvim +Man!"
138export COLUMNS=80
139export MANWIDTH=80
140export CHARM_HOST=charm.secluded.site
141export RIPGREP_CONFIG_PATH="$HOME/.config/ripgreprc"
142
143CASE_SENSITIVE="false"
144
145# Disable dumb Python keyring integration
146export PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring
147
148#HYPHEN_INSENSITIVE="true"
149# Uncomment the following line if pasting URLs and other text is messed up.
150# DISABLE_MAGIC_FUNCTIONS=true
151
152# Ranger conf
153export RANGER_LOAD_DEFAULT_RC=false
154
155# Bunch of aliases
156alias joe="joe-gitignore"
157alias ts="tailscale"
158alias tmateqr='tmate show-messages | tail -n 1 | qrencode -o - -t ANSIUTF8'
159alias info="info --vi-keys"
160alias datetime="date +%Y-%m-%d_%H%M%S_%Z"
161alias u="linx-client"
162alias clip="xclip -selection clipboard"
163alias tmp="cd $(mktemp -d) && export TEMP=$(pwd)"
164alias send="rsync -amzzP"
165alias bat="bat -n --tabs 2"
166alias mov="joshuto ~/Bulk/Media/Movies"
167alias tv="joshuto ~/Bulk/Media/TV\ Shows"
168alias yt="joshuto ~/Bulk/Media/YouTube"
169alias erase="shred -vzfun 32"
170alias 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'"
171alias us="unsilence -t 15 -as 1.25"
172alias roll="roll -v"
173alias sxiv="sxiv -p"
174alias c="chezmoi"
175alias t="tea"
176alias j="just"
177alias k="klog"
178alias ls="lsd"
179
180# Blog-related aliases
181alias secluded="cd ~/repos/personal/secluded && ./gen"
182alias drafts='e $(rg -l "draft: true" $HOME/repos/personal/secluded | fzf --preview "bat --color=always {}" --preview-window "~3")'
183
184# Custom functions
185function gi() {
186	curl -sLw https://www.toptal.com/developers/gitignore/api/$@ ;
187}
188
189
190sshedit() {
191    emulate -L zsh
192    setopt pipefail
193
194    if [[ $# -lt 2 ]]; then
195        echo "Usage: sshedit remote_host remote_file" >&2
196        return 1
197    fi
198
199    local tmpdir=$(mktemp -d)
200    chmod 700 "$tmpdir"
201
202    # Define cleanup function
203    function cleanup() {
204        [[ $PWD == $tmpdir ]] && popd >/dev/null 2>&1
205        [[ -d "$tmpdir" ]] && rm -rf "$tmpdir"
206    }
207    # Set trap but don't use EXIT which might cause terminal closure
208    trap cleanup INT TERM HUP
209
210    pushd "$tmpdir" > /dev/null || { echo "Failed to enter temp directory"; return 1; }
211
212    local remote_host="$1"
213    local remote_file="$2"
214    local local_file="${remote_file:t}"  # ZSH way to get basename
215
216    # Check ssh connection
217    if ! ssh -q -o BatchMode=yes -o ConnectTimeout=5 "$remote_host" true; then
218        echo "Error: Cannot connect to $remote_host" >&2
219        cleanup
220        return 1
221    fi
222
223    if ! ssh "$remote_host" "test -f '$remote_file'"; then
224        echo -n "Remote file doesn't exist. Create it? [y/N] " >&2
225        read -q response || { echo "\nAborting" >&2; return 1 }
226        echo
227        ssh "$remote_host" "touch '$remote_file'" || {
228            echo "Failed to create remote file" >&2
229            return 1
230        }
231    fi
232
233    # Download file
234    if ! rsync -az "${remote_host}:${remote_file}" "$local_file"; then
235        echo "Error: Failed to download the remote file" >&2
236        cleanup
237        return 1
238    fi
239
240    local before_checksum=$(sha256sum "$local_file" 2>/dev/null | awk '{print $1}')
241
242    eval "${EDITOR:-vim}" "$local_file"
243    local edit_status=$?
244
245    if [[ $edit_status -ne 0 ]]; then
246        echo "Editor exited with status $edit_status" >&2
247        cleanup
248        return $edit_status
249    fi
250
251    local after_checksum=$(sha256sum "$local_file" 2>/dev/null | awk '{print $1}')
252
253    if [[ "$before_checksum" != "$after_checksum" ]]; then
254        if ! rsync -az "$local_file" "${remote_host}:${remote_file}"; then
255            echo "Error: Failed to upload edited file" >&2
256            cleanup
257            return 1
258        fi
259        echo "File successfully updated on $remote_host" >&2
260    else
261        echo "No changes made to file" >&2
262    fi
263
264    cleanup
265    trap - INT TERM HUP
266    return 0
267}
268
269
270# Navi integration
271_call_navi() {
272  local selected
273  if selected="$(printf "$(navi --print --path ${XDG_DATA_HOME}/navi/cheats </dev/tty)")"; then
274    LBUFFER="$selected"
275  fi
276  zle redisplay
277}
278zle -N _call_navi
279bindkey '^n' _call_navi
280
281zstyle ':completion:*' completer _expand _complete _ignored _correct _approximate
282zstyle ':completion:*' group-name ''
283zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
284zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s
285zstyle ':completion:*' menu select=1
286zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s
287zstyle :compinstall filename '/home/amolith/.config/zsh/.zshrc'
288
289# Additional completions
290eval "$(op completion zsh)"; compdef _op op
291source /usr/share/z/z.sh
292
293precmd_functions=(zvm_init "${(@)precmd_functions:#zvm_init}")
294precmd_functions+=(set-long-prompt)
295zvm_after_init_commands+=("zle -N zle-line-finish; zle-line-finish() { set-short-prompt }")
296
297set-long-prompt() {
298    PROMPT=$(starship prompt)
299    RPROMPT=""
300}
301
302export COLUMNS=$(($COLUMNS + ($COLUMNS*0.1)))
303set-short-prompt() {
304    # setting this doesn't seem to actually work
305    PROMPT="$(STARSHIP_KEYMAP=${KEYMAP:-viins} starship module character)"
306    RPROMPT=$'%{\e[999C%}\e[8D%F{8}%*%f ' # remove if you don't want right prompt
307    zle .reset-prompt 2>/dev/null # hide the errors on ctrl+c
308}
309
310zle-keymap-select() {
311    set-short-prompt
312}
313zle -N zle-keymap-select
314
315zle-line-finish() { set-short-prompt }
316zle -N zle-line-finish
317
318trap 'set-short-prompt; return 130' INT
319
320# try to fix vi mode indication (not working 100%)
321zvm_after_init_commands+=('
322  function zle-keymap-select() {
323    if [[ ${KEYMAP} == vicmd ]] ||
324       [[ $1 = "block" ]]; then
325      echo -ne "\e[1 q"
326      STARSHIP_KEYMAP=vicmd
327    elif [[ ${KEYMAP} == main ]] ||
328         [[ ${KEYMAP} == viins ]] ||
329         [[ ${KEYMAP} = "" ]] ||
330         [[ $1 = "beam" ]]; then
331      echo -ne "\e[5 q"
332      STARSHIP_KEYMAP=viins
333    fi
334    zle reset-prompt
335  }
336  zle -N zle-keymap-select
337
338  # Ensure vi mode is set
339  zle-line-init() {
340    zle -K viins
341    echo -ne "\e[5 q"
342  }
343  zle -N zle-line-init
344')
345
346# Configure history file
347export HISTFILE="$XDG_STATE_HOME"/zsh/history
348export HISTSIZE=1000000
349export SAVEHIST=1000000