Skip to content

Instantly share code, notes, and snippets.

@jrcharney
Created May 23, 2025 23:20
Show Gist options
  • Select an option

  • Save jrcharney/d52308c2a33d0109df43cc5ef5e72e5a to your computer and use it in GitHub Desktop.

Select an option

Save jrcharney/d52308c2a33d0109df43cc5ef5e72e5a to your computer and use it in GitHub Desktop.
Zsh configuration with autocomplete enhancements
# File: ~/.zshrc
# Created by Jason Charney (https://github.com/jrcharney)
# Last updated: 23 May 2025
# Description: Dot-file for Zsh built for auto-completion
# 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
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
# Ensure fpath is unique before any modifications
# In other words, remove any dupicate paths in fpath
typeset -U fpath
# Clean up any duplicates in $PATH (espeically good if you are using Tmux)
# NOTE: `typeset -U PATH` will not work. We need to use lowercase.
typeset -U path
# Let's add some other paths if they exist.
[[ -d "$HOME/bin" ]] && path+=("$HOME/bin") # User bin directory (typical)
[[ -d "$HOME/.local/bin" ]] && path+=("$HOME/.local/bin") # Not sure who uses this
[[ -d "/opt/homebrew/bin" ]] && path+=("/opt/homebrew/bin") # Homebrew path (for macOS support)
[[ -d "$HOME/scripts" ]] && path+=("$HOME/scripts") # Custom scripts
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:$HOME/.local/bin:/usr/local/bin:$PATH
export PATH="${(j.:.)path}"
# Path to your Oh My Zsh installation.
export ZSH="$HOME/.oh-my-zsh"
# Set name of the theme to load --- if set to "random", it will
# load a random theme each time Oh My Zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
# ZSH_THEME="robbyrussell"
ZSH_THEME="powerlevel10k/powerlevel10k"
# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in $ZSH/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )
# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"
# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"
# Uncomment one of the following lines to change the auto-update behavior
# zstyle ':omz:update' mode disabled # disable automatic updates
# zstyle ':omz:update' mode auto # update automatically without asking
# zstyle ':omz:update' mode reminder # just remind me to update when it's time
# Uncomment the following line to change how often to auto-update (in days).
# zstyle ':omz:update' frequency 13
# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS="true"
# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"
# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"
# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"
# Uncomment the following line to display red dots whilst waiting for completion.
# You can also set it to another string to have that shown instead of the default red dots.
# e.g. COMPLETION_WAITING_DOTS="%F{yellow}waiting...%f"
# Caution: this setting can cause issues with multiline prompts in zsh < 5.7.1 (see #5765)
# COMPLETION_WAITING_DOTS="true"
# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"
# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"
# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder
# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git fzf fzf-tab zsh-autosuggestions zsh-completions zsh-syntax-highlighting)
# NOTE: zsh-completions CANNOT be loaded like a standard plugin
# We'll need to make sure we have everything it needs to work first before running it.
# Define a reusable custom, plugin, and completions directories
ZSH_CUSTOM_DIR="${ZSH_CUSTOM:-${ZSH:-$HOME/.oh-my-zsh}/custom}"
ZSH_PLUGINS_DIR="${ZSH_CUSTOM_DIR}/plugins"
ZSH_COMPLETIONS_DIR="${ZSH_PLUGINS_DIR}/zsh-completions"
ZSH_COMPLETIONS_SRC="${ZSH_COMPLETIONS_DIR}/src"
# Ensure zsh-completions plugin is installed
# We recommend using zsh-completion from source rather than the local distribution package
if [[ ! -d "$ZSH_COMPLETIONS_DIR" ]]; then
git clone https://github.com/zsh-users/zsh-completions.git "$ZSH_COMPLETIONS_DIR"
fi
# Add to fpath if missing
if ! print -l "${fpath[@]}" | grep -qx "$ZSH_COMPLETIONS_SRC"; then
fpath+=("$ZSH_COMPLETIONS_SRC")
fi
# Remove duplicate entries in fpath
fpath=("${(@u)fpath}")
# There's a lot of stuff we need to test to make sure that it is there before we enable our zstyles
# We will also recommend installing some stuff to enable these features.
should_configure_zstyles() {
local missing=()
command -v git &>/dev/null || missing+=("git")
command -v fzf &>/dev/null || missing+=("fzf")
[[ -d "${ZSH_PLUGINS_DIR}/fzf-tab" ]] || missing+=("fzf-tab")
[[ -d "${ZSH_PLUGINS_DIR}/zsh-autosuggestions" ]] || missing+=("zsh-autosuggestions")
[[ -d "${ZSH_PLUGINS_DIR}/zsh-completions" ]] || missing+=("zsh-completions")
[[ -d "${ZSH_PLUGINS_DIR}/zsh-syntax-highlighting" ]] || missing+=("zsh-syntax-highlighting")
if (( ${#missing[@]} )); then
echo "Skipping zstyle setup. Missing dependencies:"
for item in "${missing[@]}"; do
echo " - $item"
done
echo "Please install the above items before enabling fzf-enhanced completions."
return 1
fi
return 0
}
# NOTE: This should be done BEFORE compinit
if should_configure_zstyles; then
# Add fzf shell enhancements.
# NOTE: Be sure to install `fzf` from your package manager
# and to run these two commands first before refreshing ~/.zshrc
# git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
# ~/.fzf/install
#
#[-f ~/.fzf.zsh] && source ~/.fzf.zsh
#
# alternatively, you can add the `fzf` plugin to the list of oh-my-zsh plugins. Do that instead.
# Add fzf-tab behavior
# NOTE: Be sure to add `fzf` and `fzf-tab` to plugins first.
# fzf-tab configuration
# Show tab-completion menu when using completion (enables navigation via arrow keys)
zstyle ':completion:*' menu yes
# Use `fzf` for tab completion display
zstyle ':fzf-tab:*' fzf-command fzf
# Show the group of suggestions in completion
zstyle ':fzf-tab:*' show-group full
# Preview with `bat` or `cat` (`bat` is a modern `cat` command with syntax highlighting)
zstyle ':fzf-tab:complete:*' fzf-preview '[[ -f $realpath ]] && (bat --style=numbers --line-range :500 "$realpath" || cat "$realpath") 2>/dev/null'
# Enable the use of `LS_COLORS` for proper color schemes on output
zstyle ':completion:*:*:*:*:*' list-colors ${(s.:.)LS_COLORS}
# Git-aware completion for files and branches
zstyle ':completion:*' git yes
zstyle ':completion:*:*:*:*:*' git-status yes
zstyle ':fzf-tab:complete:*' git-branch yes
zstyle ':fzf-tab:complete:*' preview 'git show :%s'
# Enable icons in completion if using Nerd Fonts
zstyle ':completion:*' format '%F{blue}%C%f' # Group headers color (blue)
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' # Case-insenstive completions
# Group and sort completion suggestions
zstyle ':completion:*' group-name ''
zstyle ':completion:*' sort true
# Show descriptions where available
zstyle ':completion:*:descriptions' format '%F{green}%d%f'
# Automatically rehash completion cach when new commands are installed
zstyle ':completions:*' rehash true
# Color scheme for fzf-tab (fzf colors)
export FZF_DEFAULT_OPTS='
--color=bg+:24,bg:235,hl+:209,hl:240,fg+:255,fg:240
--color=info:108,prompt:215,spinner:102,pointer:203,marker:208
--ansi
--height=40%
--layout=reverse
--border
'
# TODO: List all of what those flags do.
fi
# Completion system initilization
# NOTE: zstyle commands should be define BEFORE compinit.
# NOTE: compinit should be BEFORE we source $ZSH/oh-my-zsh.sh.
autoload -U compinit && compinit
# Source oh-my-zsh
source $ZSH/oh-my-zsh.sh
# User configuration
# export MANPATH="/usr/local/man:$MANPATH"
# export MANPAGER="bat -l man -p"
# You may need to manually set your language environment
# export LANG=en_US.UTF-8
# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
# export EDITOR='vim'
# else
# export EDITOR='nvim'
# fi
# NOTE: `bat` (if installed) will be used as the `man` pager,
# but these exports will fix `less` to not clear the screen
export PAGER="less"
export LESS="-eFMXR"
# Compilation flags
# export ARCHFLAGS="-arch $(uname -m)"
# Set personal aliases, overriding those provided by Oh My Zsh libs,
# plugins, and themes. Aliases can be placed here, though Oh My Zsh
# users are encouraged to define aliases within a top-level file in
# the $ZSH_CUSTOM folder, with .zsh extension. Examples:
# - $ZSH_CUSTOM/aliases.zsh
# - $ZSH_CUSTOM/macos.zsh
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
#
# Better yet, just load them from `~/.bash_aliases`
[ -f ~/.bash_aliases ] && source ~/.bash_aliases
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment