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