feat(zsh): move configuration to zsh/

This commit is contained in:
Oscar Wallberg
2024-07-27 18:33:25 +02:00
parent b9e0cabe03
commit 02216e8a3e
5 changed files with 86 additions and 57 deletions
+59
View File
@@ -0,0 +1,59 @@
# vim: set ft=zsh:
# For help with expansion codes, see:
# - https://zsh.sourceforge.io/Doc/Release/Prompt-Expansion.html
# For help with parameters, see:
# - https://zsh.sourceforge.io/Doc/Release/Parameters.html
# shellcheck disable=SC2034
function prompt_warg_precmd() {
local -a _status=("${pipestatus[@]}")
RPS1=""
# Status code
if [[ ${#${_status//0/}} -gt 0 ]]; then
# shellcheck disable=SC2296
RPS1="$RPS1 %F{red}${(j:|:)_status}%f"
fi
# Python virtualenv
if [[ -n "$VIRTUAL_ENV" ]]; then
local _venv_name _venv_parent
local _pyvenv_file="${VIRTUAL_ENV}/pyvenv.cfg"
if [[ -f "$_pyvenv_file" ]]; then
while IFS=' = ' read -r key value; do
if [[ "$key" == "prompt" ]]; then
_venv_name="$value"
break
fi
done <"$_pyvenv_file"
fi
_venv_name="${_venv_name:-${VIRTUAL_ENV:t}}"
_venv_parent="${VIRTUAL_ENV:h:t}"
if [[ "$_venv_name" == "venv" || "$_venv_name" == ".venv" ]]; then
_venv_name="$_venv_parent"
fi
RPS1="$RPS1 %F{yellow}$_venv_name%f"
fi
# Timestamp
RPS1="$RPS1 %8F%D{%H:%M:%S}%f"
}
# shellcheck disable=SC2034
function prompt_warg_setup() {
VIRTUAL_ENV_DISABLE_PROMPT=1
prompt_opts=(bang cr sp percent subst)
# PS1="%n@%m %~ $ "
PS1='%(#.%F{red}.%12F)%n@%m%f %1~ %(#.#.$) '
PS2="> "
add-zsh-hook precmd prompt_warg_precmd
}
prompt_warg_setup "$@"
+4
View File
@@ -0,0 +1,4 @@
zsh-users/zsh-completions
belak/zsh-utils path:completion
belak/zsh-utils path:utility
zsh-users/zsh-autosuggestions
+162
View File
@@ -0,0 +1,162 @@
# vim: set ft=zsh:
_here="$(dirname -- "$(readlink -f -- "${HOME}/.zshrc")")"
###########
# Options #
###########
# Ref: https://zsh.sourceforge.io/Doc/Release/Options.html
setopt CORRECT
setopt INTERACTIVE_COMMENTS
unsetopt NOMATCH
# History
setopt BANG_HIST
setopt EXTENDED_HISTORY
setopt HIST_EXPIRE_DUPS_FIRST
setopt HIST_IGNORE_DUPS
setopt HIST_IGNORE_SPACE
setopt HIST_NO_STORE
setopt HIST_REDUCE_BLANKS
setopt HIST_VERIFY
setopt INC_APPEND_HISTORY_TIME
###################
# Shell Variables #
###################
# Ref: https://zsh.sourceforge.io/Doc/Release/Parameters.html
path=("${HOME}/.local/bin" "${path[@]}")
fpath=("${_here}/functions" "${fpath[@]}")
export PATH
export MAIL="/var/spool/mail/$USER"
export MAILCHECK=60
export HISTFILE="${HOME}/.zsh_history"
export HISTSIZE=100100
export SAVEHIST=100000
###################
# Other Variables #
###################
export VISUAL="nvim"
export EDITOR="nvim"
export DIFFPROG="nvim -d"
export NNN_TRASH=2
export NNN_PLUG=''
###########
# Plugins #
###########
if [[ ! -d "${_here}/.cache" ]]; then
mkdir "${_here}/.cache"
fi
_antidote="${_here}/.cache/.antidote"
_plugins="${_here}/plugins"
_plugins_cache="${_here}/.cache/plugins.zsh"
if [[ ! -d "$_antidote" ]]; then
git clone --depth=1 https://github.com/mattmc3/antidote.git "$_antidote"
fi
if [[ ! -f "${_plugins}" ]]; then
touch "${_plugins}"
fi
fpath=("${_antidote}/functions" "${fpath[@]}")
autoload -Uz antidote
if [[ ! "${_plugins_cache}" -nt "${_plugins}" ]]; then
antidote bundle <"${_plugins}" >"${_plugins_cache}"
fi
# shellcheck source=zsh/.cache/plugins.zsh
source "${_plugins_cache}"
unset _antidote _plugins _plugins_cache
#############
# Functions #
#############
# Ref: https://zsh.sourceforge.io/Doc/Release/Functions.html#Functions
function set_terminal_title() {
local title
if [ -z "$1" ]; then
title=$(basename "$(print -P "%~")")
else
title="$1"
fi
echo -ne "\033]2;$title\033\\"
}
function precmd() {
set_terminal_title
}
function ssh_with_title() {
local host=""
local skip_next="false"
for arg in "$@"; do
case "$arg" in
-[bcDeFIiLlmOopRSWw]?*)
# Option with argument immediately after, like -p22
skip_next="false"
;;
-[bcDeFIiLlmOopRSWw])
# Option with argument on next iteration
skip_next="true"
;;
-*)
# Any other option, assumed not to take an argument
skip_next="false"
;;
*)
if [ "$skip_next" = "true" ]; then
skip_next="false"
else
host="${arg#*@}"
fi
;;
esac
done
set_terminal_title "$host"
ssh "$@"
set_terminal_title
}
###########
# Aliases #
###########
# TODO: alias for safe rm and ask for replacing during mv/cp
alias ln='ln -fi'
alias rm='rm -I'
alias n='nnn -dHerU'
alias ssh='ssh_with_title'
##########
# Prompt #
##########
autoload -Uz promptinit
promptinit
prompt warg
########
# Misc #
########
# Emacs mode
bindkey -e
###########
# Cleanup #
###########
unset _here