From b674c0a95ba5f400f939f4a4037e7cb6af906844 Mon Sep 17 00:00:00 2001 From: Peter Wood Date: Mon, 19 May 2025 16:26:36 -0400 Subject: [PATCH] Refactor alias management and improve bootstrap process for Zsh --- dotfiles/.zshrc | 7 +++ dotfiles/my-aliases.zsh | 6 +-- dotfiles/my-aliases.zsh.original | 16 +++++++ setup/bootstrap.sh | 11 ++++- setup/setup.sh | 81 +++++++++++++++++++++++++++++--- 5 files changed, 111 insertions(+), 10 deletions(-) create mode 100644 dotfiles/my-aliases.zsh.original diff --git a/dotfiles/.zshrc b/dotfiles/.zshrc index 78a3d51..b683017 100644 --- a/dotfiles/.zshrc +++ b/dotfiles/.zshrc @@ -76,8 +76,15 @@ eval "$(zoxide init zsh)" # For a full list of active aliases, run `alias`. # Load custom aliases +# Define ZSH_CUSTOM if not already defined +if [ -z "$ZSH_CUSTOM" ]; then + ZSH_CUSTOM="$HOME/.oh-my-zsh/custom" +fi + if [ -f "$ZSH_CUSTOM/aliases.zsh" ]; then source "$ZSH_CUSTOM/aliases.zsh" +elif [ -f "$HOME/.oh-my-zsh/custom/aliases.zsh" ]; then + source "$HOME/.oh-my-zsh/custom/aliases.zsh" fi # set directory to home diff --git a/dotfiles/my-aliases.zsh b/dotfiles/my-aliases.zsh index a0eea55..922f123 100644 --- a/dotfiles/my-aliases.zsh +++ b/dotfiles/my-aliases.zsh @@ -11,6 +11,6 @@ alias update="/home/acedanger/shell/update.sh" alias dcdn="docker compose down" alias dcupd="docker compose up -d" alias lzd="lazydocker" -alias cat="batcat" -alias fd="fdfind" -alias fzf="fzf --preview='batcat {}'" +alias cat="bat" +alias fd="fd" +alias fzf="fzf --preview='bat {}'" diff --git a/dotfiles/my-aliases.zsh.original b/dotfiles/my-aliases.zsh.original new file mode 100644 index 0000000..a0eea55 --- /dev/null +++ b/dotfiles/my-aliases.zsh.original @@ -0,0 +1,16 @@ +alias py=python3 +alias gpull="git pull" +alias gpush="git push" +alias gc="git commit" +alias gcm="git commit -m" + +alias ll="ls -laFh --group-directories-first --color=auto" +alias findzombie="ps -A -ostat,pid,ppid | grep -e '[zZ]'" +alias plex="/home/acedanger/shell/plex.sh" +alias update="/home/acedanger/shell/update.sh" +alias dcdn="docker compose down" +alias dcupd="docker compose up -d" +alias lzd="lazydocker" +alias cat="batcat" +alias fd="fdfind" +alias fzf="fzf --preview='batcat {}'" diff --git a/setup/bootstrap.sh b/setup/bootstrap.sh index a32c02a..a2fac2c 100755 --- a/setup/bootstrap.sh +++ b/setup/bootstrap.sh @@ -75,4 +75,13 @@ chmod +x "$DOTFILES_DIR/setup/setup.sh" # Run setup script "$DOTFILES_DIR/setup/setup.sh" -echo -e "${GREEN}Bootstrap completed! Please restart your terminal for changes to take effect.${NC}" +# Source the aliases file if it exists to make aliases available in the current session +ZSH_CUSTOM="$HOME/.oh-my-zsh/custom" +ALIASES_FILE="$ZSH_CUSTOM/aliases.zsh" +if [ -f "$ALIASES_FILE" ]; then + echo -e "${YELLOW}Loading aliases into current session...${NC}" + source "$ALIASES_FILE" +fi + +echo -e "${GREEN}Bootstrap completed! Your aliases have been set up.${NC}" +echo -e "${YELLOW}Tip: Run 'source ~/.zshrc' or start a new terminal session to use all zsh features.${NC}" diff --git a/setup/setup.sh b/setup/setup.sh index 399ba47..10b3200 100755 --- a/setup/setup.sh +++ b/setup/setup.sh @@ -39,7 +39,7 @@ determine_pkg_manager() { # Set up package management based on OS if [ "$OS_NAME" = "fedora" ]; then - echo -e "${YELLOW}Setting up Fedora repositories and package management...${NC}" + echo -e "${YELLOW}Setting up Fedora Tsrepositories and package management...${NC}" # Install prerequisites for Fedora sudo dnf install -y wget gpg @@ -135,7 +135,7 @@ mapfile -t pkgs < <(grep -v '^//' "$SCRIPT_DIR/packages.list" | grep -v -e '^$') # Map Debian/Ubuntu package names to Fedora equivalents if needed declare -A fedora_pkg_map fedora_pkg_map["bat"]="bat" -fedora_pkg_map["fd-find"]="fd-find" +fedora_pkg_map["fd-find"]="fd" # On Fedora, the package is called 'fd' # Add more package mappings as needed # Process the package list based on OS @@ -287,11 +287,74 @@ ln -sf "$DOTFILES_SUBDIR/.nanorc" "$HOME/.nanorc" 2>/dev/null || true ln -sf "$DOTFILES_SUBDIR/.profile" "$HOME/.profile" 2>/dev/null || true ln -sf "$DOTFILES_SUBDIR/.gitconfig" "$HOME/.gitconfig" 2>/dev/null || true -if [ "$OS_NAME" = "fedora" ]; then - echo -e "${YELLOW}Setting up Fedora-specific configurations...${NC}" - echo -e "${GREEN}Fedora-specific setup completed.${NC}" +# Create custom aliases based on available commands +echo -e "${YELLOW}Setting up custom aliases...${NC}" +ZSH_CUSTOM="$HOME/.oh-my-zsh/custom" +ALIASES_FILE="$ZSH_CUSTOM/aliases.zsh" +mkdir -p "$ZSH_CUSTOM" + +# Create a copy of the original aliases file for backup +cp "$DOTFILES_SUBDIR/my-aliases.zsh" "$ALIASES_FILE.bak" + +# First, copy all general aliases except those we'll modify based on OS +grep -v "alias cat=" "$DOTFILES_SUBDIR/my-aliases.zsh" | grep -v "alias fd=" | grep -v "alias fzf=" > "$ALIASES_FILE" + +# Function to check for command existence and add appropriate alias +add_conditional_alias() { + local cmd_name=$1 + local debian_cmd=$2 + local fedora_cmd=$3 + local alias_line=$4 + + echo -e "${YELLOW}Checking for $cmd_name command...${NC}" + + if command -v "$debian_cmd" &> /dev/null; then + echo "alias $cmd_name=\"$debian_cmd\"" >> "$ALIASES_FILE" + echo -e "${GREEN}Using $debian_cmd for $cmd_name alias${NC}" + return 0 + elif command -v "$fedora_cmd" &> /dev/null; then + echo "alias $cmd_name=\"$fedora_cmd\"" >> "$ALIASES_FILE" + echo -e "${GREEN}Using $fedora_cmd for $cmd_name alias${NC}" + return 0 + else + echo -e "${YELLOW}Neither $debian_cmd nor $fedora_cmd found. Skipping $cmd_name alias.${NC}" + return 1 + fi +} + +# Set up the cat/bat alias +add_conditional_alias "cat" "batcat" "bat" + +# Set up the fd alias +add_conditional_alias "fd" "fdfind" "fd" + +# Set up fzf preview with correct bat command +if command -v fzf &> /dev/null; then + if command -v batcat &> /dev/null; then + echo "alias fzf=\"fzf --preview='batcat {}'\"" >> "$ALIASES_FILE" + echo -e "${GREEN}Using batcat for fzf preview${NC}" + elif command -v bat &> /dev/null; then + echo "alias fzf=\"fzf --preview='bat {}'\"" >> "$ALIASES_FILE" + echo -e "${GREEN}Using bat for fzf preview${NC}" + else + echo "alias fzf=\"fzf\"" >> "$ALIASES_FILE" + echo -e "${YELLOW}No bat/batcat found for fzf preview. Using basic fzf alias.${NC}" + fi fi +# Also create a symlink from the custom aliases file back to the dotfiles directory for persistence +# This allows changes made to aliases.zsh to be tracked in the dotfiles repo +echo -e "${YELLOW}Creating symlink to save customized aliases back to dotfiles...${NC}" +if [ -f "$ALIASES_FILE" ]; then + # Save a copy of the original for reference + cp "$DOTFILES_SUBDIR/my-aliases.zsh" "$DOTFILES_SUBDIR/my-aliases.zsh.original" 2>/dev/null || true + # Replace the my-aliases.zsh with the new customized one + cp "$ALIASES_FILE" "$DOTFILES_SUBDIR/my-aliases.zsh" +fi + +# Make sure the aliases file gets sourced immediately in the current shell +echo -e "${GREEN}Custom aliases configured for $OS_NAME.${NC}" + # Set Zsh as default shell if not already set if [[ "$SHELL" != *"zsh"* ]]; then echo -e "${YELLOW}Setting Zsh as default shell...${NC}" @@ -309,5 +372,11 @@ if [[ "$SHELL" != *"zsh"* ]]; then fi fi -echo -e "${GREEN}Setup completed successfully for $OS_NAME $OS_VERSION!${NC}" +# Display useful information about the setup +echo -e "\n${GREEN}=== Setup Summary ===${NC}" +echo -e "${GREEN}OS: $OS_NAME $OS_VERSION${NC}" +echo -e "${GREEN}Package Manager: $PKG_MANAGER${NC}" +echo -e "${GREEN}Shell: $(basename "$SHELL") → zsh${NC}" + +echo -e "\n${GREEN}Setup completed successfully for $OS_NAME $OS_VERSION!${NC}" echo -e "${YELLOW}Note: You may need to log out and log back in for all changes to take effect.${NC}"