Last active
April 14, 2025 13:00
-
-
Save walteh/3ec0ced4eeed52c7912e043d2d77f48f to your computer and use it in GitHub Desktop.
zsh-plugin-aliasrc - dynamic project specific aliases for cli commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################## | |
# aliasrc - Directory-specific command aliases | |
# | |
# This plugin enables directory-specific command aliases by looking | |
# for executable files in a .aliasrc directory in the current directory | |
# or any parent directory. When found, commands that match executable | |
# names in that directory will be replaced with the path to the executable. | |
############################################## | |
# Load required zsh modules | |
autoload -U add-zsh-hook | |
autoload -Uz add-zle-hook-widget | |
# Configuration options (can be set before sourcing the plugin) | |
enable_aliasrc=${enable_aliasrc:-1} # Master toggle (1=enabled, 0=disabled) | |
enable_aliasrc_debug=${enable_aliasrc_debug:-0} # Enable debug output (1=enabled, 0=disabled) | |
enable_aliasrc_prompt_emoji=${enable_aliasrc_prompt_emoji:-1} # Show emoji in prompt (1=enabled, 0=disabled) | |
enable_aliasrc_command_highlight=${enable_aliasrc_command_highlight:-1} # Highlight matched commands (1=enabled, 0=disabled) | |
# Customizable settings | |
aliasrc_prompt_emoji=${aliasrc_prompt_emoji:-"🔮"} # Emoji shown in prompt when aliasrc is active | |
aliasrc_command_highlight=${aliasrc_command_highlight:-"green,bold,underline,italic"} # Styling for highlighted commands | |
aliasrc_dir=${aliasrc_dir:-".aliasrc"} # Name of directory containing aliases | |
# Print debug messages when debugging is enabled | |
# Args: | |
# $1: Debug message to print | |
aliasrc_debug() { | |
if [[ "$enable_aliasrc_debug" -gt 0 ]]; then | |
echo "[aliasrc debug] $1" >&2 | |
fi | |
} | |
# Extract the first argument (command) from a command line | |
# Args: | |
# $1: Command line string | |
# Returns: | |
# The first word (command) from the input string | |
aliasrc_first_arg() { | |
local cmd=$(echo "$1" | awk '{print $1}') | |
echo "$cmd" | |
} | |
# Check if the first argument in a command line is an aliasrc alias | |
# Args: | |
# $1: Command line string | |
# Returns: | |
# Path to the alias executable if found, empty string otherwise | |
aliasrc_first_arg_is_alias() { | |
local cmd=$(aliasrc_first_arg "$1") | |
local dir="$PWD" | |
local found="" | |
while [[ "$dir" != "/" ]]; do | |
if [[ -x "$dir/$aliasrc_dir/$cmd" ]]; then | |
found="$dir/$aliasrc_dir/$cmd" | |
break | |
fi | |
dir="${dir:h}" # Get parent directory (zsh-specific) | |
done | |
# Return relative path if in current directory | |
echo "$found" | sed "s|$PWD/||" | |
} | |
# Find and set the current .aliasrc directory | |
# Called when changing directories and at initialization | |
aliasrc_precmd() { | |
local dir="$PWD" | |
local found="" | |
# Search up the directory tree for a .aliasrc directory | |
while [[ "$dir" != "/" ]]; do | |
if [[ -d "$dir/$aliasrc_dir" ]]; then | |
found="$dir/$aliasrc_dir" | |
break | |
fi | |
dir="${dir:h}" # zsh-specific way to get parent directory | |
done | |
# Only update if the aliasrc directory has changed | |
if [[ "$found" != "$ALIASRC_ACTIVE_DIR" ]]; then | |
export ALIASRC_ACTIVE_DIR="$found" | |
aliasrc_debug "Set ALIASRC_ACTIVE_DIR to '$ALIASRC_ACTIVE_DIR'" | |
else | |
aliasrc_debug "No change: still using '$ALIASRC_ACTIVE_DIR'" | |
fi | |
aliasrc_debug "leaving aliasrc_precmd" | |
} | |
# Handle command line acceptance, replacing aliasrc commands | |
# with their executable paths | |
aliasrc_accept_line() { | |
if [[ -n "$ALIASRC_ACTIVE_DIR" ]]; then | |
alias_command=$(aliasrc_first_arg_is_alias "$BUFFER") | |
first_buffer_arg=$(echo "$BUFFER" | awk '{print $1}') | |
if [[ -n "$alias_command" ]] && [[ -n "$first_buffer_arg" ]]; then | |
aliasrc_debug "alias_command found: '$alias_command', replacing '$first_buffer_arg'" | |
# Replace the first arg of the command with the alias path | |
BUFFER=$(echo "$BUFFER" | awk -v alias_command="$alias_command" '{gsub($1, alias_command); print}') | |
aliasrc_debug "Replacing '$first_buffer_arg' with '$alias_command'" | |
aliasrc_debug "BUFFER: '$BUFFER'" | |
fi | |
fi | |
zle .accept-line | |
} | |
# Set prompt marker if .aliasrc is active in the current directory | |
aliasrc_set_active_prompt_icon() { | |
local prompt_marker="%F{magenta}${aliasrc_prompt_emoji}%f " | |
if [[ -n "$ALIASRC_ACTIVE_DIR" ]]; then | |
if [[ "$PROMPT" != *"${prompt_marker}"* ]]; then | |
PROMPT="${PROMPT}${prompt_marker}" | |
fi | |
else | |
PROMPT="${PROMPT//${prompt_marker}/}" | |
fi | |
} | |
# Highlight aliasrc commands in the command line | |
highlight_aliasrc_command() { | |
alias_command=$(aliasrc_first_arg_is_alias "$BUFFER") | |
if [[ -n "$alias_command" ]]; then | |
alias_command_name=$(basename "$alias_command") | |
local len=${#alias_command_name} | |
region_highlight+=("0 $len fg=$aliasrc_command_highlight") | |
fi | |
} | |
# Initialize the plugin if enabled | |
if [[ "$enable_aliasrc" -gt 0 ]]; then | |
# Set up hooks for directory changes and command line handling | |
add-zsh-hook chpwd aliasrc_precmd | |
zle -N accept-line aliasrc_accept_line | |
# Add prompt indication if enabled | |
if [[ "$enable_aliasrc_prompt_emoji" -gt 0 ]]; then | |
add-zsh-hook precmd aliasrc_set_active_prompt_icon | |
aliasrc_set_active_prompt_icon | |
fi | |
# Set up command highlighting if enabled | |
if [[ "$enable_aliasrc_command_highlight" -gt 0 ]]; then | |
add-zle-hook-widget line-pre-redraw highlight_aliasrc_command | |
fi | |
# Initialize on plugin load | |
aliasrc_precmd | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment