commit e4bdd73c400a282dc3c6a7443f16386a39f802b5 from: vincent delft date: Sat Feb 14 20:34:48 2026 UTC initial import commit - /dev/null commit + e4bdd73c400a282dc3c6a7443f16386a39f802b5 blob - /dev/null blob + c021126866e18179fd398ef078b442324ed5f4a9 (mode 644) --- /dev/null +++ README.md @@ -0,0 +1,149 @@ +# Zsh Configuration README + +This README explains the configuration of the `.zshrc` file and the plugins listed in `zsh_plugins.txt`. It also provides instructions for setting up Antidote (the Zsh plugin manager) and updating the plugins. + +## Overview of `.zshrc` + +The `.zshrc` file is a configuration file for the Zsh shell, customizing the shell environment, prompt, key bindings, aliases, and plugins. Below is a breakdown of its sections and their purposes: + +### Sections in `.zshrc` + +1. **Colors**: + - Enables color support in the shell using `autoload -U colors && colors` to allow colored prompts and completion highlights. + +2. **Prompt & Right Prompt**: + - Configures a custom prompt using `setopt prompt_subst` to enable dynamic prompt substitution. + - The left prompt (`PROMPT`) shows: + - A custom symbol (`#` for root, `$` for non-root) with the last command’s exit code. + - The current directory (`%~`) and a connection status (`is_local` function). + - The right prompt (`RPROMPT`) displays: + - The current time in cyan. + - Battery status (via `get_battery` function) with icons indicating charging state and percentage. + +3. **Plugins (via Antidote)**: + - Uses Antidote to manage plugins listed in `zsh_plugins.txt`. + - Loads plugins with `source $HOME/.config/zsh/antidote/antidote.zsh` and `antidote load`. + +4. **Completion**: + - Initializes Zsh’s completion system with `autoload -Uz compinit && compinit`. + - Enables case-insensitive completion using `zstyle ':completion:*' matcher-list`. + - Colors completion items based on `LS_COLORS`. + +5. **History**: + - Configures history settings: + - `HISTSIZE=5000`: Stores up to 5000 history entries in memory. + - `HISTFILE=~/.zsh_history`: Saves history to a file. + - `SAVEHIST=$HISTSIZE`: Saves all history entries to the file. + - `HISTDUP=erase`: Removes duplicates. + - Options like `appendhistory`, `sharehistory`, and `hist_ignore_dups` ensure efficient history management. + - Sets `ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE` for styling autosuggestion text. + +6. **Key Bindings**: + - Binds up/down arrow keys to search through command history (`history-search-backward` and `history-search-forward`). + +7. **Aliases**: + - Adds `ls --color` alias for FreeBSD systems. + - Sets `v` as an alias for `nvim` (Neovim editor). + +8. **Environment**: + - Adds `$HOME/.local/bin` to the `PATH` for custom binaries. + - Sets `WINIT_X11_SCALE_FACTOR=1` to adjust Alacritty terminal scaling. + +9. **Functions**: + - `get_battery`: Displays battery percentage and charging status (used in `RPROMPT`). + - `is_local`: Customizes the prompt based on local or SSH connections. + - `chpwd`: Updates the terminal window title to show the current directory when it changes. + - `set_title`: Manually sets the terminal window title. + - `ssh_title`: Updates the terminal title for SSH sessions. + +10. **External Tools**: + - Integrates `zoxide` for intelligent directory navigation with the `cd` command alias. + +## Plugins in `zsh_plugins.txt` + +The `zsh_plugins.txt` file lists plugins managed by Antidote. Below is an explanation of each plugin’s purpose: + +- **zsh-users/zsh-syntax-highlighting**: + - Provides real-time syntax highlighting for commands as you type. + - Highlights valid commands in green, invalid ones in red, and other elements (e.g., paths, options) in different colors for better readability. + +- **zsh-users/zsh-autosuggestions**: + - Suggests commands based on your history as you type. + - Suggestions appear in a faint color (configured via `ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE`) and can be accepted with the right arrow key. + +- **zsh-users/zsh-history-substring-search**: + - Enables searching through command history using substrings. + - Bound to up/down arrow keys (via `^[[A` and `^[[B`) for navigating matching history entries. + +- **zsh-users/zsh-completions**: + - Adds additional completion definitions for various commands and tools. + - Enhances the completion system with more context-aware suggestions. + +## Setting Up Antidote + +Antidote is a lightweight Zsh plugin manager used to load the plugins listed in `zsh_plugins.txt`. Follow these steps to set it up: + +1. **Install Antidote**: + - Create the Antidote directory: + ```bash + mkdir -p $HOME/.config/zsh/antidote + ``` + - Clone the Antidote repository: + ```bash + git clone --depth=1 https://github.com/mattmc3/antidote.git $HOME/.config/zsh/antidote + ``` + +2. **Ensure `.zshrc` Loads Antidote**: + - The `.zshrc` file already includes: + ```bash + source $HOME/.config/zsh/antidote/antidote.zsh + antidote load $HOME/.config/zsh/zsh_plugins.txt + ``` + - If not present, add these lines to your `.zshrc`. + +3. **Create `zsh_plugins.txt`**: + - Ensure the `zsh_plugins.txt` file exists at `$HOME/.config/zsh/zsh_plugins.txt` with the plugin list: + ``` + zsh-users/zsh-syntax-highlighting + zsh-users/zsh-autosuggestions + zsh-users/zsh-history-substring-search + zsh-users/zsh-completions + ``` + +4. **Apply Changes**: + - Source your `.zshrc` to apply the configuration: + ```bash + source ~/.zshrc + ``` + +## Updating Plugins + +To update the plugins managed by Antidote, run the following command: + +```bash +antidote update +``` + +This command updates Antidote itself and all plugins listed in `zsh_plugins.txt` to their latest versions. After updating, source your `.zshrc` again to apply changes: + +```bash +source ~/.zshrc +``` + +## Additional Notes + +- **Dependencies**: + - Ensure `zoxide` is installed for the `cd` alias to work (`eval "$(zoxide init --cmd cd zsh)"`). + - On FreeBSD, `acpiconf` is used for battery status. Ensure it’s available or modify the `get_battery` function for other systems. + - Neovim (`nvim`) is required for the `v` alias. + +- **Customization**: + - Modify `ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE` to change the autosuggestion color. + - Adjust `HISTSIZE` and `SAVEHIST` for larger or smaller history storage. + - Customize the prompt colors or symbols in `PROMPT`, `RPROMPT`, or the `is_local` function. + +- **Troubleshooting**: + - If plugins fail to load, verify that Antidote is installed correctly and `zsh_plugins.txt` is in the right location. + - Check for missing dependencies (e.g., `acpiconf` or `zoxide`) if functions or aliases don’t work. + +This configuration provides a powerful, user-friendly Zsh environment with enhanced completion, history management, and a dynamic prompt. blob - /dev/null blob + 8d14ce826a2fbd651fc727e5bca43f8e69fcd9b6 (mode 644) Binary files /dev/null and screenshots/left-right prompts.png differ blob - /dev/null blob + 15b275d3b7d956fd699e1676cb158688f1c677f5 (mode 644) Binary files /dev/null and screenshots/title-dir.png differ blob - /dev/null blob + 875f0d43fc7015cd494e1853d719de8661933842 (mode 644) Binary files /dev/null and screenshots/title-prompt-ssh.png differ blob - /dev/null blob + 8b58035258818f2eaa4e90113654c4e75e493726 (mode 644) --- /dev/null +++ zsh_plugins.txt @@ -0,0 +1,5 @@ + +zsh-users/zsh-syntax-highlighting +zsh-users/zsh-autosuggestions +zsh-users/zsh-history-substring-search +zsh-users/zsh-completions blob - /dev/null blob + 9b7233727c4366a22afdeab08a154ecefef0c8fa (mode 644) --- /dev/null +++ zshrc @@ -0,0 +1,168 @@ +# ------------------------------------------------------------ +# Colors +# ------------------------------------------------------------ +autoload -U colors && colors + + +# ------------------------------------------------------------ +# differenciate normal user and root +# ------------------------------------------------------------ +setopt prompt_subst +local user_symbol='%# [%?]' + +# ------------------------------------------------------------ +# Prompt & Right Prompt +# ------------------------------------------------------------ +# Function to get battery percentage +get_battery() { + bat="" + if acpiconf -i 0 > /dev/null 2>&1 ; then + bat=$(acpiconf -i 0 | awk -F: '/Remaining capacity/ {print $2}' | xargs echo -n) + bat=${bat%*%} + state=$(acpiconf -i 0 | awk -F: '/State/ {print $2}' | xargs echo -n) + if [ -n "$bat" ]; then + if [ "$bat" -lt "20" ]; then + [ "$state" = "charging" ] && echo -n " $bat%%" + [ "$state" != "charging" ] && echo -n "󱊡 $bat%%" + else + [ "$state" = "charging" ] && echo -n " $bat%%" + [ "$state" != "charging" ] && echo -n "󱊢 $bat%%" + fi + fi + fi +} +is_local() { + # information: %F{231}%K{24}@%m%k%f + # warning: %F{232}%K{214}@%m%k%f + # critical %F{255}%K{160}@%m%k%f + if [ -n "$SSH_CLIENT" ]; then + # if user root, then red, other else, green + # must be a xterm 256 color code + echo "%B%(!.%F{202}.%F{10})%n%F{232}%K{214}@%m%k%f%b%{$reset_color%}" + else + #echo "%B%(!.%F{202}.%F{10})%n@%m%f%b%{$reset_color%}" + echo "%B%(!.%F{202}.%F{32})%n@%m%f%b%{$reset_color%}" + fi +} + +PROMPT='╭─$(is_local)$NVIM_SESSION_TXT: %~ +╰─%B${user_symbol}%b ' +RPROMPT='%F{cyan}[%D{%H:%M:%S}] $(get_battery)%f' + +# ------------------------------------------------------------ +# Plugins (via Antidote) +# ------------------------------------------------------------ +source $HOME/.config/zsh/antidote/antidote.zsh +antidote load $HOME/.config/zsh/zsh_plugins.txt + +# ------------------------------------------------------------ +# Completion +# ------------------------------------------------------------ +# load comptetions +autoload -Uz compinit && compinit +# Case-insensitive completion +zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}' +# Use LS_COLORS for completion colors +zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}" + +# ------------------------------------------------------------ +# Key Bindings +# ------------------------------------------------------------ +# search up arrow key +bindkey '^[[A' history-search-backward +# search down arrow key +bindkey '^[[B' history-search-forward + +# ------------------------------------------------------------ +# History +# ------------------------------------------------------------ +ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=#8b9193" +HISTSIZE=5000 +HISTFILE=~/.zsh_history +SAVEHIST=$HISTSIZE +HISTDUP=erase +setopt appendhistory +setopt sharehistory +setopt hist_ignore_space +setopt hist_ignore_all_dups +setopt hist_save_no_dups +setopt hist_ignore_dups +setopt hist_find_no_dups + +# ------------------------------------------------------------ +# Aliases +# ------------------------------------------------------------ +[ "$(uname -s)" = "FreeBSD" ] && alias ls='ls --color' +alias v='nvim' +alias ssh=ssh_title + +# ------------------------------------------------------------ +# Environment +# ------------------------------------------------------------ +export PATH="$HOME/.local/bin:$PATH" +# Because Alacritty has a scale factor of 1.6 +export WINIT_X11_SCALE_FACTOR=1 + + + + +# ------------------------------------------------------------ +# Functions +# ------------------------------------------------------------ +rm_nvim_session() { + local nvim_session_dir="$HOME/.config/nvim/sessions" + local cwd="${PWD}" + local nvim_session_name=$(echo "$cwd" | sed 's/\//%/g').vim + local nvim_session_file="$nvim_session_dir/$nvim_session_name" + rm "$nvim_session_file" + chpwd +} + + +# to change automatically the windows title +# chpwd is a hook in zsh that will be triggered when +# changing directory +chpwd() { + echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD/#$HOME/~}\007" + # Helper function to return a prompt indicator if a Neovim session exists + local nvim_session_dir="$HOME/.config/nvim/sessions" + local cwd="${PWD}" + local nvim_session_name=$(echo "$cwd" | sed 's/\//%/g').vim + local nvim_session_file="$nvim_session_dir/$nvim_session_name" + if [ -f "$nvim_session_file" ]; then + NVIM_SESSION_TXT="[nvim]" + else + NVIM_SESSION_TXT="" + fi +} + +# to adapt the title of the windows during an ssh session +# Function to set Alacritty window title +set_title() { + echo -ne "\033]0;${1}\007" +} +# SSH wrapper function to set title during SSH sessions +ssh_title() { + local host=$1 + shift + # Set initial title to user@hostname:~ + # set_title "${USER}@${host}:~" + # Run SSH with a command to track directory changes + if [ $# -eq 0 ]; then + /usr/bin/ssh -t "$host" 'cd $HOME; echo -ne "\033]0;'${host}':$PWD\007"; exec $SHELL -i' + else + /usr/bin/ssh -t "$host" 'cd $HOME; echo -ne "\033]0;'${host}':$PWD\007"; exec ' "$@" + fi + } + +# ------------------------------------------------------------ +# startup commands +# ------------------------------------------------------------ +# Trigger chpwd manually on shell startup +# so the prompt is adapted with nvim persistence session +if typeset -f chpwd >/dev/null; then + chpwd +fi +# External Tools +eval "$(zoxide init --cmd cd zsh)" +