Configure zsh

This commit is contained in:
Alexander Hess 2022-08-08 23:59:31 +02:00
parent 47c92124e5
commit 8dbb94f75a
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
11 changed files with 1901 additions and 7 deletions

View file

@ -1,6 +1,3 @@
# Executed by bash when a login shell exits
# Clear the screen to increase privacy
if [ "$SHLVL" = 1 ]; then
[ -x /usr/bin/clear ] && /usr/bin/clear || [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
fi
source "$HOME/.config/shell/logout.sh"

View file

@ -1,3 +1,3 @@
# Shell-related Configuration
This folder contains further files that are sourced by `bash`.
This folder contains further files that are sourced by `bash` and `zsh`.

View file

@ -1,14 +1,34 @@
# Shell aliases for bash
# Shell aliases for bash and zsh
_command_exists() {
command -v "$1" 1>/dev/null 2>&1
}
_in_zsh() {
[ -n "$ZSH_VERSION" ]
}
# Re-run last command with sudo privileges
if _in_zsh; then
alias ,,='sudo $(fc -ln -1)'
else
alias ,,='sudo $(history -p !!)'
fi
# Convenient piping with zsh
if _in_zsh; then
alias -g B='| bat'
alias -g F='| fzf'
alias -g G='| grep'
alias -g H='| head'
alias -g L='| less'
alias -g T='| tail'
alias -g NE='2 > /dev/null'
alias -g NUL='> /dev/null 2>&1'
fi
# (Non-)obvious synonyms

6
.config/shell/logout.sh Normal file
View file

@ -0,0 +1,6 @@
# This file is sourced by a login shell upon logout
# Clear the screen to increase privacy
if [ "$SHLVL" = 1 ]; then
[ -x /usr/bin/clear ] && /usr/bin/clear || [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
fi

View file

@ -5,6 +5,10 @@ _command_exists() {
command -v "$1" 1>/dev/null 2>&1
}
_in_zsh() {
[ -n "$ZSH_VERSION" ]
}
# Configure the keyboard:
@ -209,11 +213,17 @@ _update_repositories() {
cwd=$(pwd)
cd $REPOS
# Otherwise the for-loop waites for manual input
# if it cd's into a folder with a ".env" file
ZSH_DOTENV_FILE='.do_not_run_dotenv'
for dir in */; do
cd "$REPOS/$dir" && echo "Fetching $REPOS/$dir"
git fetch --all --prune
done
ZSH_DOTENV_FILE='.env'
_command_exists pass && echo "Fetching $HOME/.password-store" && pass git pull
_update_dotfiles
@ -232,6 +242,44 @@ _update_dotfiles() {
}
_update_zsh() {
_in_zsh || return
echo 'Updating zsh'
_update_omz_fork
_update_zplug
}
_update_omz_fork() {
_command_exists omz || return
# In a nutshell, `omz update` pulls the latest origin/master
# from the original "oh-my-zsh" repo
omz update
cwd=$(pwd)
cd $ZSH
git checkout --quiet forked # most likely already the case
# Keep our personal "oh-my-zsh" fork up-to-date
# See: https://gitlab.webartifex.biz/alexander/oh-my-zsh
git rebase --quiet master
git push --quiet fork forked
git push --quiet fork master
cd $cwd
}
_update_zplug() {
_command_exists zplug || return
zplug update
zplug install # ensure newly added plugins in ~/.zshrc are never forgotten
zplug load
}
update-machine() {
sudo --validate || return
@ -240,6 +288,7 @@ update-machine() {
_command_exists flatpak && sudo flatpak update -y
_command_exists snap && sudo snap refresh && _remove_old_snaps
_update_repositories
_update_zsh
sudo --reset-timestamp
}

1622
.p10k.zsh Normal file

File diff suppressed because it is too large Load diff

View file

@ -27,6 +27,9 @@ prepend-to-path "$HOME/.local/bin"
# Shell-specific stuff
# zsh-specific stuff is automatically sourced from
# ~/.zshenv, ~/.zprofile, ~/.zlogin, and ~/.zshrc
# Source ~/.bashrc if we are running inside a bash shell
# because it is NOT automatically sourced by bash
if [ -n "$BASH_VERSION" ]; then

3
.zlogout Normal file
View file

@ -0,0 +1,3 @@
# Executed by zsh when a login shell exits
source "$HOME/.config/shell/logout.sh"

5
.zprofile Normal file
View file

@ -0,0 +1,5 @@
# Executed by zsh when a login shell starts
# Unify ~/.profile and ~/.zprofile conceptually
# (~/.zlogin is skipped here as it is sourced after ~/.zshrc)
source "$HOME/.profile"

35
.zshenv Normal file
View file

@ -0,0 +1,35 @@
# This file is sourced by zsh before ~/.zprofile and ~/.zshrc
# (it's kind of a zsh-only ~/.profile file)
export ZSH="$HOME/.oh-my-zsh"
export ZPLUG_HOME="$HOME/.zplug"
# Use <Up> key to auto-complete a partially typed command
# TODO: the coloring does not work when zsh-syntax-highlighting is loaded simultaniously
# Source: https://github.com/zsh-users/zsh-history-substring-search/issues/131
export HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND="fg=#ffffff,bg=#38761d,bold"
export HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND="fg=#ffffff,bg=#990000,bold"
# Notify about shorter aliases for typed commands
# Source: https://github.com/MichaelAquilina/zsh-you-should-use
export YSU_MESSAGE_POSITION="before"
export YSU_MODE="BESTMATCH"
# Suggest commands as one types
# Source: https://github.com/zsh-users/zsh-autosuggestions
export ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=#666666,bg=bold"
export ZSH_AUTOSUGGEST_STRATEGY=(history completion)
# Temporary files should go into ~/.cache
export ZSH_COMPDUMP="${XDG_CACHE_HOME:-$HOME/.cache}/.zcompdump-$HOST-$ZSH_VERSION"
# Automatically source ".env" files in folders
# Source: https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/dotenv
export ZSH_DOTENV_FILE=".env"

154
.zshrc Normal file
View file

@ -0,0 +1,154 @@
# Executed by zsh when a (non-)login shell starts
# Ensure zsh is running interactively
[[ $- != *i* ]] && return
# Enable Powerlevel10k instant prompt
if [ -r "${XDG_CACHE_HOME:-$HOME/.cache}/zsh/p10k-instant-prompt-${(%):-%n}.zsh" ]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/zsh/p10k-instant-prompt-${(%):-%n}.zsh"
fi
# Enable XON/XOFF software flow control
stty -ixon
# Enable colors and change prompt
autoload -Uz colors
colors
# Enable VI mode
bindkey -v
# If an entered command does not exist per se
# but is the name of a folder instead, go there
setopt AUTO_CD
# Treat "#", "~", and "^" as part of patterns for filename generation
setopt EXTENDED_GLOB
# Warn if there are no matches
setopt NO_MATCH
# Silence the shell
setopt NO_BEEP
# Report status of background jobs immediately
setopt NOTIFY
# Remove all "built-in" aliases
unalias -a
# Set these environment variables here (and not in ~/.profile)
# due to conflict/overlap with bash
export HISTFILE="$HOME/.zhistory" # default name by oh-my-zsh
export HISTSIZE=999999 # number of lines kept in memory
export SAVEHIST=999999 # number of lines kept in $HISTFILE
# Append to the $HISTFILE rather than overwrite it
setopt APPEND_HISTORY
# Initialize oh-my-zsh's plugins
plugins=(
command-not-found
dirhistory
dotenv # config in ~/.zshenv; `_update_repositories` temporarily disables this
git-escape-magic
jsontools
z
)
source "$ZSH/oh-my-zsh.sh"
# Initialize zplug's plugins
source "$HOME/.zplug/init.zsh" # config in ~/.zshenv
# Must use double quotes in this section
# Source: https://github.com/zplug/zplug#example
# Make zplug manage itself like a plugin
# Source: https://github.com/zplug/zplug#let-zplug-manage-zplug
zplug "zplug/zplug", hook-build:"zplug --self-manage"
zplug "MichaelAquilina/zsh-you-should-use" # config in ~/.zshenv
zplug "zsh-users/zsh-autosuggestions" # config in ~/.zshenv
zplug "zsh-users/zsh-history-substring-search" # config in ~/.zshenv; there are key bindings below
zplug "zsh-users/zsh-syntax-highlighting"
zplug "romkatv/powerlevel10k", as:theme, depth:1
zplug load
# Initialize various utilities and aliases
source "$HOME/.config/shell/utils.sh"
source "$HOME/.config/shell/aliases.sh"
# Initialize zsh's completions
# This is already done via ~/.oh-my-zsh.sh above
# autoload -Uz compinit
# compinit -u -d "$ZSH_COMPDUMP"
# Enable match highlighting and scrolling through long lists,
# and provide a different style of menu completion
zmodload zsh/complist
# Include hidden files in tab completion
_comp_options+=(GLOB_DOTS)
# Enable arrow-key driven interface
zstyle ':completion:*' menu select
# Make compinit find new executables right away
zstyle ':completion:*' rehash true
# Enable grouping and group headers
zstyle ':completion:*:descriptions' format '%B%d%b'
zstyle ':completion:*:messages' format '%d'
zstyle ':completion:*:warnings' format 'No matches for: %d'
zstyle ':completion:*' group-name ''
# Define key bindings
# zsh-autosuggestions plugin
bindkey "^ " autosuggest-accept
# Enable Ctrl-R
bindkey "^R" history-incremental-search-backward
# Use VI keys to navigate the completions in the menu
bindkey -M menuselect 'h' vi-backward-char
bindkey -M menuselect 'k' vi-up-line-or-history
bindkey -M menuselect 'l' vi-forward-char
bindkey -M menuselect 'j' vi-down-line-or-history
# history-substring-search plugin
# Source: https://github.com/zsh-users/zsh-history-substring-search#usage
# Normal mode
bindkey "$terminfo[kcuu1]" history-substring-search-up
bindkey "$terminfo[kcud1]" history-substring-search-down
# VI mode
bindkey -M vicmd 'k' history-substring-search-up
bindkey -M vicmd 'j' history-substring-search-down
# Enable Powerlevel10k "full" prompt
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh