From 02f6077bab0dd8f176f5a326756148fd1319446b Mon Sep 17 00:00:00 2001 From: tiyn Date: Fri, 23 Feb 2024 03:01:28 +0100 Subject: [PATCH] zsh: cleanup zsh config file --- .config/zsh/.zshrc | 194 +++++++++++++++++++++++++++------------------ 1 file changed, 116 insertions(+), 78 deletions(-) diff --git a/.config/zsh/.zshrc b/.config/zsh/.zshrc index f8da70b..491a5b5 100644 --- a/.config/zsh/.zshrc +++ b/.config/zsh/.zshrc @@ -1,21 +1,60 @@ -# Disable ctrl-s and ctrl-q. -stty -ixon +#################### +# HELPER FUNCTIONS # +#################### -setopt autocd autopushd - -# Enable colors and change prompt -autoload -U colors && colors -autoload -Uz vcs_info precmd_vcs_info() { vcs_info } + precmd_functions+=( precmd_vcs_info ) -zstyle ':vcs_info:*' enable git -zstyle ':vcs_info:git*:*' get-revision true -zstyle ':vcs_info:git*:*' check-for-changes true -zstyle ':vcs_info:git*' formats "(%s) %c%u %b%m" -zstyle ':vcs_info:git*' actionformats "(%s|%a) %12.12i %c%u %b%m" -setopt prompt_subst -# Show remote ref name and number of commits ahead-of or behind +zle-line-init() { + zle -K viins + echo -ne "\e[5 q" +} + +preexec() { echo -ne '\e[5 q' ;} + +local copy_widgets=( + vi-yank vi-yank-eol vi-delete vi-backward-kill-word vi-change-whole-line +) + +local paste_widgets=( + vi-put-{before,after} +) + +function zle-keymap-select { + if [[ ${KEYMAP} == vicmd ]] || + [[ $1 = 'block' ]]; then + echo -ne '\e[1 q' + elif [[ ${KEYMAP} == main ]] || + [[ ${KEYMAP} == viins ]] || + [[ ${KEYMAP} == '' ]]; then + echo -ne '\e[5 q' + fi +} + +function x11-clip-wrap-widgets() { + local copy_or_paste=$1 + shift + for widget in $@; do + if [[ $copy_or_paste == "copy" ]]; then + eval " + function _x11-clip-wrapped-$widget() { + zle .$widget + xclip -in -selection clipboard <<<\$CUTBUFFER + } + " + else + eval " + function _x11-clip-wrapped-$widget() { + CUTBUFFER=\$(xclip -out -selection clipboard) + zle .$widget + } + " + fi + zle -N $widget _x11-clip-wrapped-$widget + done +} + function +vi-git-st() { local ahead behind remote local -a gitstatus @@ -33,7 +72,7 @@ function +vi-git-st() { hook_com[branch]="${hook_com[branch]} [${remote}${(j:/:)gitstatus}]" fi } -# Show count of stashed changes + function +vi-git-stash() { local -a stashes if [[ -s ${hook_com[base]}/.git/refs/stash ]] ; then @@ -41,94 +80,87 @@ function +vi-git-stash() { hook_com[misc]+=" (${stashes} stashed)" fi } + +######### +# STYLE # +######### + +# Set style for version control +zstyle ':vcs_info:*' enable git +zstyle ':vcs_info:git*:*' get-revision true +zstyle ':vcs_info:git*:*' check-for-changes true +zstyle ':vcs_info:git*' formats "(%s) %c%u %b%m" +zstyle ':vcs_info:git*' actionformats "(%s|%a) %12.12i %c%u %b%m" +# Show remote ref name and number of commits ahead-of or behind +# Show count of stashed changes zstyle ':vcs_info:git*+set-message:*' hooks git-st git-stash -PS1='%B%{$fg[blue]%}[%{$fg[blue]%}%n%{$fg[blue]%}@%{$fg[blue]%}%M %{$fg[blue]%}%~%{$fg[blue]%}]%{$reset_color%}\$%b ' -RPROMPT='%B%{$fg[blue]%}$vcs_info_msg_0_%{$reset_color%}%b' +# Set style for completion options zstyle ':completion:*' menu select zstyle ':completion:*' matcher-list '' 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*' # Case insensitive completion zmodload zsh/complist _comp_options+=(globdots) -# disable sort when completing `git checkout` +# Disable sort when completing `git checkout` zstyle ':completion:*:git-checkout:*' sort false -# set descriptions format to enable group support -# don't use escape sequences here, fzf-tab will ignore them +# Set descriptions format to enable group support zstyle ':completion:*:descriptions' format '[%d]' -# set list-colors to enable filename colorizing +# Set list-colors to enable filename colorizing zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS} -# preview directory's content with eza when completing cd +# Preview directory's content with eza when completing cd zstyle ':fzf-tab:complete:cd:*' fzf-preview 'eza -1 --color=always $realpath' -# switch group using `,` and `.` +# Switch group using `,` and `.` zstyle ':fzf-tab:*' switch-group ',' '.' +# Set default Vim Cursor shape +zle -N zle-keymap-select +zle -N zle-line-init +echo -ne '\e[5 q' + +# Set default prompt +PS1='%B%{$fg[blue]%}[%{$fg[blue]%}%n%{$fg[blue]%}@%{$fg[blue]%}%M %{$fg[blue]%}%~%{$fg[blue]%}]%{$reset_color%}\$%b ' +RPROMPT='%B%{$fg[blue]%}$vcs_info_msg_0_%{$reset_color%}%b' +setopt prompt_subst + +# Copy to x11 clipboard +x11-clip-wrap-widgets copy $copy_widgets +x11-clip-wrap-widgets paste $paste_widgets + +# Enable colors +autoload -U colors && colors + +# Enable git info in prompt +autoload -Uz vcs_info + +############### +# KEYBINDINGS # +############### + # Enable vi mode bindkey -v export KEYTIMEOUT=1 + # Vim bindings in tab mode 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 bindkey -v '^?' backward-delete-char -# Vim Cursor shape -function zle-keymap-select { - if [[ ${KEYMAP} == vicmd ]] || - [[ $1 = 'block' ]]; then - echo -ne '\e[1 q' - elif [[ ${KEYMAP} == main ]] || - [[ ${KEYMAP} == viins ]] || - [[ ${KEYMAP} == '' ]]; then - echo -ne '\e[5 q' - fi -} -zle -N zle-keymap-select -zle-line-init() { - zle -K viins - echo -ne "\e[5 q" -} -zle -N zle-line-init -echo -ne '\e[5 q' -preexec() { echo -ne '\e[5 q' ;} - -# Vim copy and paste fix in terminal -function x11-clip-wrap-widgets() { - local copy_or_paste=$1 - shift - for widget in $@; do - if [[ $copy_or_paste == "copy" ]]; then - eval " - function _x11-clip-wrapped-$widget() { - zle .$widget - xclip -in -selection clipboard <<<\$CUTBUFFER - } - " - else - eval " - function _x11-clip-wrapped-$widget() { - CUTBUFFER=\$(xclip -out -selection clipboard) - zle .$widget - } - " - fi - zle -N $widget _x11-clip-wrapped-$widget - done -} +####################### +# ADDITIONAL FEATURES # +####################### -local copy_widgets=( - vi-yank vi-yank-eol vi-delete vi-backward-kill-word vi-change-whole-line -) -local paste_widgets=( - vi-put-{before,after} -) +# Load alias +[ -f "$HOME/.config/aliasrc" ] && source "$HOME/.config/aliasrc" # Load aliases -# NB: can atm. only wrap native widgets -x11-clip-wrap-widgets copy $copy_widgets -x11-clip-wrap-widgets paste $paste_widgets +# Disable ctrl-s and ctrl-q. +stty -ixon -[ -f "$HOME/.config/aliasrc" ] && source "$HOME/.config/aliasrc" # Load aliases +# Automatically cd if given a directory +setopt autocd autopushd +# Enable compinit autoload -Uz compinit && compinit # Plugin: command not found notice @@ -144,14 +176,20 @@ bindkey '^ ' autosuggest-accept # Plugin: syntax highlighting source /usr/share/zsh/plugins/fast-syntax-highlighting/fast-syntax-highlighting.plugin.zsh +# Open shell with tmux always if [[ -n "$PS1" ]] && [[ -z "$TMUX" ]] && [[ -n "$SSH_CONNECTION" ]]; then tmux attack-session -t $USER || tmux new-session -s $USER fi +# set up thefuck +eval $(thefuck --alias) + # python #eval "$(pyenv init -)" -# set up thefuck -eval $(thefuck --alias) +########################## +# COMMANDS BEFORE PROMPT # +########################## +# Display pfetch when starting zsh pfetch