perf(zsh): Load time decreased from 350ms to 110ms

- Read .profile in ~/.zshenv: ensuring non-login & non-interactive shell
  gets env set
- Comments on every zsh and zinit config
- Async load Zinit plugins
- Remove unnecessary plugins
- Create completion dumpfile once a day max
This commit is contained in:
Pratik Tripathy
2024-12-10 20:00:46 +05:30
parent 5c9929e1a0
commit 39bd3442d3
2 changed files with 65 additions and 59 deletions

10
common/.zshenv Normal file
View File

@@ -0,0 +1,10 @@
#!/bin/zsh
# https://blog.patshead.com/2011/04/improve-your-oh-my-zsh-startup-time-maybe.html
skip_global_compinit=1
# https://github.com/sorin-ionescu/prezto/blob/master/runcoms/zshenv
# Ensure that a non-login, non-interactive shell has a defined environment.
if [[ ( "$SHLVL" -eq 1 && ! -o LOGIN ) && -s "${ZDOTDIR:-$HOME}/.profile" ]]; then
source "${ZDOTDIR:-$HOME}/.profile"
fi

View File

@@ -1,39 +1,36 @@
#!/bin/zsh #!/bin/zsh
# I'm not using a separate .zprofile; reuse the .profile instead
[[ ! -f "$HOME/.profile" ]] || source "$HOME/.profile"
# ZSH Options # ZSH Options # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
bindkey -v # enable vi-mode bindkey -v # enable vi-mode
setopt +o nomatch setopt +o nomatch # Unmatched glob patterns like bash
ZSH_STATE_HOME="$XDG_STATE_HOME/shell" setopt noglobalrcs # Don't source global rc files from /etc/z*
ZSH_STATE_HOME="$XDG_STATE_HOME/shell"
ZSH_THEME="powerlevel10k/powerlevel10k" ZSH_THEME="powerlevel10k/powerlevel10k"
CASE_SENSITIVE="true" CASE_SENSITIVE="true"
DISABLE_UPDATE_PROMPT="true" DISABLE_UPDATE_PROMPT="true"
COMPLETION_WAITING_DOTS="true" COMPLETION_WAITING_DOTS="true"
HIST_STAMPS="dd.mm.yyyy" # see 'man strftime' for details.
# ZSH History # ZSH History
export HISTSIZE=10000000 export HISTSIZE=10000000
export SAVEHIST=$HISTSIZE export SAVEHIST=$HISTSIZE
export HIST_STAMPS="dd.mm.yyyy" # see 'man strftime' for details.
export HISTFILE="$ZSH_STATE_HOME/zsh_history" export HISTFILE="$ZSH_STATE_HOME/zsh_history"
setopt appendhistory # Append rather than overwriting setopt APPENDHISTORY # Append rather than overwriting
setopt sharehistory # Share history between all sessions. setopt SHAREHISTORY # Share history between all sessions.
setopt extended_history # Write the history file in the ":start:elapsed;command" format. setopt EXTENDED_HISTORY # Write the history file in the ":start:elapsed;command" format.
setopt inc_append_history # Write to the history file immediately, not when the shell exits. setopt INC_APPEND_HISTORY # Write to the history file immediately, not when the shell exits.
setopt hist_reduce_blanks # Remove superfluous blanks before recording entry. setopt HIST_REDUCE_BLANKS # Remove superfluous blanks before recording entry.
setopt hist_verify # Don't execute immediately upon history expansion. setopt HIST_VERIFY # Don't execute immediately upon history expansion.
setopt hist_ignore_space # Don't add commands that start with whitespace to history setopt HIST_IGNORE_SPACE # Don't add commands that start with whitespace to history
setopt hist_find_no_dups # Don't show duplicate commands when searching history setopt HIST_FIND_NO_DUPS # Don't show duplicate commands when searching history
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc. # Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi fi
# Zinit ZSH Plugin Manager # Zinit: ZSH Plugin Manager # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
ZINIT_HOME="${XDG_DATA_HOME}/shell/zinit/zinit.git" ZINIT_HOME="${XDG_DATA_HOME}/shell/zinit/zinit.git"
if [[ ! -d "$ZINIT_HOME" ]]; then if [[ ! -d "$ZINIT_HOME" ]]; then
mkdir -p "$(dirname $ZINIT_HOME)" mkdir -p "$(dirname $ZINIT_HOME)"
@@ -42,69 +39,68 @@ fi
source "${ZINIT_HOME}/zinit.zsh" source "${ZINIT_HOME}/zinit.zsh"
# Zinit plugin settings # Zinit plugin settings
TIMER_PRECISION=3; TIMER_FORMAT='[%d]'
VI_MODE_SET_CURSOR=true VI_MODE_SET_CURSOR=true
VI_MODE_CURSOR_INSERT=3 VI_MODE_CURSOR_INSERT=3
# Having HISTORY_BASE after per-directory-history plugin install does NOT work TIMER_PRECISION=3; TIMER_FORMAT='[%d]'
HISTORY_BASE="$ZSH_STATE_HOME/per-directory-history" HISTORY_BASE="$ZSH_STATE_HOME/per-directory-history"
# Zinit Plugins # Zinit Plugins
zinit ice depth=1; zinit light romkatv/powerlevel10k zinit ice depth=1; zinit light romkatv/powerlevel10k
zinit light zdharma-continuum/fast-syntax-highlighting zinit ice depth=1; zinit snippet https://github.com/ohmyzsh/ohmyzsh/blob/master/plugins/per-directory-history/per-directory-history.zsh
zinit light zsh-users/zsh-completions
zinit light zsh-users/zsh-autosuggestions
# Oh-my-zsh plugins zinit ice wait lucid depth=1; zinit light zsh-users/zsh-completions
zi snippet OMZP::aliases zinit ice wait lucid depth=1; zinit light zsh-users/zsh-autosuggestions
# zi snippet OMZP::gitfast zinit ice wait lucid depth=1; zinit light zdharma-continuum/fast-syntax-highlighting
zi snippet OMZP::aws
zi snippet OMZP::azure
zi snippet OMZP::brew
zi snippet OMZP::colored-man-pages
zi snippet OMZP::command-not-found
zi snippet OMZP::dotnet
zi snippet OMZP::dotenv
zi snippet OMZP::git
zi snippet OMZP::gitignore
zi snippet OMZP::kitty
zi snippet OMZP::podman
zi snippet OMZP::sudo
zi snippet OMZP::systemd
zi snippet OMZP::timer
zi snippet OMZP::ubuntu
zi snippet OMZP::urltools
zi snippet OMZP::vi-mode
zi snippet https://github.com/ohmyzsh/ohmyzsh/blob/master/plugins/per-directory-history/per-directory-history.zsh
zi ice as"completion" zinit ice wait lucid depth=1; zinit snippet OMZP::colored-man-pages
#zi snippet OMZP::fd/_fd zinit ice wait lucid depth=1; zinit snippet OMZP::command-not-found
zinit ice wait lucid depth=1; zinit snippet OMZP::dotenv
zinit ice wait lucid depth=1; zinit snippet OMZP::gitignore
zinit ice wait lucid depth=1; zinit snippet OMZP::sudo
zinit ice wait lucid depth=1; zinit snippet OMZP::timer
zinit ice wait lucid depth=1; zinit snippet OMZP::urltools
zinit ice wait lucid depth=1; zinit snippet OMZP::vi-mode
# ZSH Auto-completion settings # Completions
autoload -Uz compinit zinit ice wait lucid depth=1; zinit snippet OMZP::podman
_comp_options+=(globdots) # Include hidden files zinit ice wait lucid depth=1; zinit snippet OMZP::dotnet
zmodload zsh/complist zinit ice wait lucid depth=1; zinit snippet OMZP::fzf
# TIP: execute `zinit update --all` quarterly
# Add brew provided autocompletions to path
[[ ! -d "${HOMEBREW_PREFIX}/share/zsh/site-functions" ]] || FPATH="${HOMEBREW_PREFIX}/share/zsh/site-functions:$FPATH"
# ZSH Auto-completion settings: Do it AFTER plugin load, so all fpaths are included
autoload -Uz compinit # Initialized zsh completion
(( ${+_comps} )) && _comps[zinit]=_zinit # Sets up completion for Zinit
_comp_options+=(globdots) # Include hidden files
zmodload zsh/complist # Add enhancements to zsh completion system
# Completion files: Use XDG dirs # Completion files: Use XDG dirs
ZCOMP_CACHE_HOME="${XDG_CACHE_HOME}/zsh" ZCOMP_CACHE_HOME="${XDG_CACHE_HOME}/zsh"
ZCOMP_CACHE_FILE="${ZCOMP_CACHE_HOME}/zcompcache" ZCOMP_CACHE_FILE="${ZCOMP_CACHE_HOME}/zcompcache"
[ -d "${ZCOMP_CACHE_HOME}" ] || mkdir -p "${ZCOMP_CACHE_HOME}" [ -d "${ZCOMP_CACHE_HOME}" ] || mkdir -p "${ZCOMP_CACHE_HOME}"
zstyle ':completion:*' cache-path "$ZCOMP_CACHE_FILE"
compinit -d "${ZCOMP_CACHE_HOME}/zcompdump-${ZSH_VERSION}"
zinit cdreplay -q # Must be AFTER compinit -d # Run compinit only once daily
if [[ -n $ZCOMP_CACHE_HOME/zcompdump-$ZSH_VERSION(#qN.mh+24) ]]; then
compinit -d "$ZCOMP_CACHE_HOME/zcompdump-$ZSH_VERSION"
else
compinit -C # Generate dumpfile only if outdated
fi
zinit cdreplay -q # Replay stored compdef. Must be AFTER compinit -d
# Completion styling # Completion styling
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}' # Case INsensitive completion match zstyle ':completion:*' cache-path "$ZCOMP_CACHE_FILE" # Sets path for completion cache files
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}" # Add color to completion suggestions zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}' # Case INsensitive completion match
zstyle ':completion::complete:*' gain-privileges 1 menu select cache-path "$ZCOMP_CACHE_FILE" zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}" # Add color to completion suggestions
zstyle ':completion::complete:*' gain-privileges 1 menu select cache-path "$ZCOMP_CACHE_FILE" # Enable completion for sudo commands
# To customize prompt, run `p10k configure` # To customize prompt, run `p10k configure`
[[ ! -f "$XDG_CONFIG_HOME/shell/p10k.zsh" ]] || source "$XDG_CONFIG_HOME/shell/p10k.zsh" [[ ! -f "$XDG_CONFIG_HOME/shell/p10k.zsh" ]] || source "$XDG_CONFIG_HOME/shell/p10k.zsh"
# User configuration # User configuration # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Add brew provided autocompletions to path
[[ ! -d "/home/linuxbrew/.linuxbrew/share/zsh/site-functions" ]] || FPATH="/home/linuxbrew/.linuxbrew/share/zsh/site-functions:$FPATH"
# [ctrl+r]: Search command history # [ctrl+r]: Search command history
# [ctrl+t]: fzf & over the files & directories under the current one & paste it to prompt # [ctrl+t]: fzf & over the files & directories under the current one & paste it to prompt