private_dot_zshrc.tmpl

  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
143# Partially in response to https://sourcehut.org/blog/2023-01-09-gomodulemirror/
144# Partially because GOPROXY seems not great
145export GOPROXY="direct"
146
147CASE_SENSITIVE="false"
148
149# Disable dumb Python keyring integration
150export PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring
151
152# Authenticate to local Radicle (.xyz) node
153export RAD_PASSWORD='{{ onepasswordRead "op://Private/2ujzijel6ni3np2uj5k5syptzm/password" }}'
154
155export JIRA_API_TOKEN='{{ onepasswordRead "op://Private/Atlassian/jira key" }}'
156
157# LLM Crap
158export OPENAI_API_BASE='https://openrouter.ai/api/v1'
159export OPENROUTER_API_KEY='{{ onepasswordRead "op://Private/OpenRouter/shell" }}'
160export OPENROUTER_KEY="$OPENROUTER_API_KEY"
161export OPENAI_API_KEY="$OPENROUTER_API_KEY"
162
163export KAGI_API_KEY='{{ onepasswordRead "op://Shared/Kagi/api_key" }}'
164
165#HYPHEN_INSENSITIVE="true"
166# Uncomment the following line if pasting URLs and other text is messed up.
167# DISABLE_MAGIC_FUNCTIONS=true
168
169# Ranger conf
170export RANGER_LOAD_DEFAULT_RC=false
171
172# Bunch of aliases
173alias joe="joe-gitignore"
174alias ts="tailscale"
175alias tmateqr='tmate show-messages | tail -n 1 | qrencode -o - -t ANSIUTF8'
176alias info="info --vi-keys"
177alias datetime="date +%Y-%m-%d_%H%M%S_%Z"
178alias u="linx-client"
179alias clip="xclip -selection clipboard"
180alias tmp="cd $(mktemp -d) && export TEMP=$(pwd)"
181alias send="rsync -amzzP"
182alias bat="bat -n --tabs 2"
183alias mov="joshuto ~/Bulk/Media/Movies"
184alias tv="joshuto ~/Bulk/Media/TV\ Shows"
185alias yt="joshuto ~/Bulk/Media/YouTube"
186alias erase="shred -vzfun 32"
187alias 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'"
188alias us="unsilence -t 15 -as 1.25"
189alias roll="roll -v"
190alias sxiv="sxiv -p"
191alias c="chezmoi"
192alias t="tea"
193alias j="just"
194alias k="klog"
195alias ls="lsd"
196
197# Blog-related aliases
198alias secluded="cd ~/repos/personal/secluded && ./gen"
199alias drafts='e $(rg -l "draft: true" $HOME/repos/personal/secluded | fzf --preview "bat --color=always {}" --preview-window "~3")'
200
201# Custom functions
202function gi() {
203	curl -sLw https://www.toptal.com/developers/gitignore/api/$@ ;
204}
205
206
207sshedit() {
208    emulate -L zsh
209    setopt pipefail
210
211    if [[ $# -lt 2 ]]; then
212        echo "Usage: sshedit remote_host remote_file" >&2
213        return 1
214    fi
215
216    local tmpdir=$(mktemp -d)
217    chmod 700 "$tmpdir"
218
219    # Define cleanup function
220    function cleanup() {
221        [[ $PWD == $tmpdir ]] && popd >/dev/null 2>&1
222        [[ -d "$tmpdir" ]] && rm -rf "$tmpdir"
223    }
224    # Set trap but don't use EXIT which might cause terminal closure
225    trap cleanup INT TERM HUP
226
227    pushd "$tmpdir" > /dev/null || { echo "Failed to enter temp directory"; return 1; }
228
229    local remote_host="$1"
230    local remote_file="$2"
231    local local_file="${remote_file:t}"  # ZSH way to get basename
232
233    # Check ssh connection
234    if ! ssh -q -o BatchMode=yes -o ConnectTimeout=5 "$remote_host" true; then
235        echo "Error: Cannot connect to $remote_host" >&2
236        cleanup
237        return 1
238    fi
239
240    if ! ssh "$remote_host" "test -f '$remote_file'"; then
241        echo -n "Remote file doesn't exist. Create it? [y/N] " >&2
242        read -q response || { echo "\nAborting" >&2; return 1 }
243        echo
244        ssh "$remote_host" "touch '$remote_file'" || {
245            echo "Failed to create remote file" >&2
246            return 1
247        }
248    fi
249
250    # Download file
251    if ! rsync -az "${remote_host}:${remote_file}" "$local_file"; then
252        echo "Error: Failed to download the remote file" >&2
253        cleanup
254        return 1
255    fi
256
257    local before_checksum=$(sha256sum "$local_file" 2>/dev/null | awk '{print $1}')
258
259    eval "${EDITOR:-vim}" "$local_file"
260    local edit_status=$?
261
262    if [[ $edit_status -ne 0 ]]; then
263        echo "Editor exited with status $edit_status" >&2
264        cleanup
265        return $edit_status
266    fi
267
268    local after_checksum=$(sha256sum "$local_file" 2>/dev/null | awk '{print $1}')
269
270    if [[ "$before_checksum" != "$after_checksum" ]]; then
271        if ! rsync -az "$local_file" "${remote_host}:${remote_file}"; then
272            echo "Error: Failed to upload edited file" >&2
273            cleanup
274            return 1
275        fi
276        echo "File successfully updated on $remote_host" >&2
277    else
278        echo "No changes made to file" >&2
279    fi
280
281    cleanup
282    trap - INT TERM HUP
283    return 0
284}
285
286
287# Navi integration
288_call_navi() {
289  local selected
290  if selected="$(printf "$(navi --print --path ${XDG_DATA_HOME}/navi/cheats </dev/tty)")"; then
291    LBUFFER="$selected"
292  fi
293  zle redisplay
294}
295zle -N _call_navi
296bindkey '^n' _call_navi
297
298zstyle ':completion:*' completer _expand _complete _ignored _correct _approximate
299zstyle ':completion:*' group-name ''
300zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
301zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s
302zstyle ':completion:*' menu select=1
303zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s
304zstyle :compinstall filename '/home/amolith/.config/zsh/.zshrc'
305
306# Additional completions
307eval "$(op completion zsh)"; compdef _op op
308source /usr/share/z/z.sh
309
310precmd_functions=(zvm_init "${(@)precmd_functions:#zvm_init}")
311precmd_functions+=(set-long-prompt)
312zvm_after_init_commands+=("zle -N zle-line-finish; zle-line-finish() { set-short-prompt }")
313
314set-long-prompt() {
315    PROMPT=$(starship prompt)
316    RPROMPT=""
317}
318
319export COLUMNS=$(($COLUMNS + ($COLUMNS*0.1)))
320set-short-prompt() {
321    # setting this doesn't seem to actually work
322    PROMPT="$(STARSHIP_KEYMAP=${KEYMAP:-viins} starship module character)"
323    RPROMPT=$'%{\e[999C%}\e[8D%F{8}%*%f ' # remove if you don't want right prompt
324    zle .reset-prompt 2>/dev/null # hide the errors on ctrl+c
325}
326
327zle-keymap-select() {
328    set-short-prompt
329}
330zle -N zle-keymap-select
331
332zle-line-finish() { set-short-prompt }
333zle -N zle-line-finish
334
335trap 'set-short-prompt; return 130' INT
336
337# try to fix vi mode indication (not working 100%)
338zvm_after_init_commands+=('
339  function zle-keymap-select() {
340    if [[ ${KEYMAP} == vicmd ]] ||
341       [[ $1 = "block" ]]; then
342      echo -ne "\e[1 q"
343      STARSHIP_KEYMAP=vicmd
344    elif [[ ${KEYMAP} == main ]] ||
345         [[ ${KEYMAP} == viins ]] ||
346         [[ ${KEYMAP} = "" ]] ||
347         [[ $1 = "beam" ]]; then
348      echo -ne "\e[5 q"
349      STARSHIP_KEYMAP=viins
350    fi
351    zle reset-prompt
352  }
353  zle -N zle-keymap-select
354
355  # Ensure vi mode is set
356  zle-line-init() {
357    zle -K viins
358    echo -ne "\e[5 q"
359  }
360  zle -N zle-line-init
361')
362
363# Configure history file
364export HISTFILE="$XDG_STATE_HOME"/zsh/history
365export HISTSIZE=1000000
366export SAVEHIST=1000000