#!/bin/bash # ~/.bashrc: executed by bash(1) for non-login shells. # 优化版本 - 2025-03-19 (分离式设计) # ------------------------------------------ # 基础设置 # ------------------------------------------ # 非交互模式直接退出 [[ $- != *i* ]] && return # ------------------------------------------ # 历史记录配置 # ------------------------------------------ HISTCONTROL=ignoreboth:erasedups # 忽略重复和空格开头命令 HISTSIZE=100000 # 历史记录条数 HISTFILESIZE=200000 # 历史文件大小 HISTTIMEFORMAT="%F %T " # 历史记录时间戳 shopt -s histappend # 追加历史记录 shopt -s checkwinsize # 窗口大小自适应 # ------------------------------------------ # Shell 行为设置 - 增强版 # ------------------------------------------ # 禁用 Ctrl+D 多次退出(防误触) IGNOREEOF=2 # 需要按两次 Ctrl+D 才能退出 # 文件权限掩码 umask 022 # 设置文件权限掩码 # 增强的Shell选项 shopt -s autocd # 输入目录名可直接cd shopt -s cdspell # 自动修正cd命令拼写错误 shopt -s dirspell # Tab补全时自动修正目录名拼写错误 shopt -s globstar # 启用**递归匹配 # ------------------------------------------ # 环境变量设置 # ------------------------------------------ export EDITOR="vim" export VISUAL="vim" export PAGER="less" export LANG="en_US.UTF-8" export LC_ALL="en_US.UTF-8" # ------------------------------------------ # 颜色与提示符 (Git 相关修改区域) # ------------------------------------------ # 自适应颜色配置 case "$TERM" in xterm*|rxvt*|linux*|screen*|tmux*|alacritty*) color_prompt=yes;; *) color_prompt=no;; esac # 提示符颜色定义 (修改:移除 \[ 和 \],使用 Raw ANSI codes) C_USER_RAW="\033[38;5;45m" # 用户 (Raw ANSI code) C_HOST_RAW="\033[38;5;45m" # 主机 (Raw ANSI code) C_PATH_RAW="\033[38;5;228m" # 路径 (Raw ANSI code) C_GIT_RAW="\033[38;5;198m" # Git 分支 (Raw ANSI code) C_JOBS_RAW="\033[38;5;208m" # Jobs 颜色 (Raw ANSI code) C_PROMPT_RAW="\033[38;5;85m" # 提示符 (Raw ANSI code) C_RESET_RAW="\033[0m" # 重置 (Raw ANSI code) # --- Git 提示符功能 --- # Git 分支显示函数 (修改:优化实现并输出 Raw ANSI codes) ps1_git_branch_custom() { if ! command -v git >/dev/null 2>&1; then return fi local branch=$(git symbolic-ref --short HEAD 2>/dev/null) if [[ -z "$branch" ]]; then local detached=$(git rev-parse --short HEAD 2>/dev/null) if [[ -n "$detached" ]]; then branch="detached@${detached}" else return # Not in a git repo or initial commit fi fi local status_symbol="" # Check for unstaged changes if ! git diff --quiet HEAD 2>/dev/null; then status_symbol="*" # Unstaged fi # Check for staged changes (append or set) if ! git diff --cached --quiet HEAD 2>/dev/null; then status_symbol+="+" # Staged fi # Output Raw ANSI codes + info echo -ne "${C_GIT_RAW}(${branch}${status_symbol})${C_RESET_RAW}" } # ------------------------------------------ # Git提示符优化 - 大型仓库性能优化 # ------------------------------------------ # 为ps1_git_branch_custom添加缓存机制 __git_status_cache_key="" __git_status_cache_val="" __git_status_cache_time=0 ps1_git_branch_custom_cached() { local now=$(date +%s) local key="$PWD-$(git rev-parse --git-dir 2>/dev/null)" # 如果目录变化或缓存超过2秒,重新计算 if [[ "$__git_status_cache_key" != "$key" || $((now - __git_status_cache_time)) -gt 2 ]]; then __git_status_cache_key="$key" __git_status_cache_val="$(ps1_git_branch_custom)" __git_status_cache_time=$now fi echo -ne "$__git_status_cache_val" } # (推荐) 尝试使用 Git 官方提供的 __git_ps1 (如果可用) GIT_PROMPT_SCRIPT="" if [[ -f /usr/share/bash-completion/completions/git ]]; then GIT_PROMPT_SCRIPT="/usr/share/bash-completion/completions/git" elif [[ -f /usr/share/git-core/contrib/completion/git-prompt.sh ]]; then GIT_PROMPT_SCRIPT="/usr/share/git-core/contrib/completion/git-prompt.sh" elif [[ -f /etc/bash_completion.d/git-prompt ]]; then GIT_PROMPT_SCRIPT="/etc/bash_completion.d/git-prompt" fi if [[ -n "$GIT_PROMPT_SCRIPT" && -f "$GIT_PROMPT_SCRIPT" ]]; then # shellcheck source=/dev/null . "$GIT_PROMPT_SCRIPT" # Configure __git_ps1 behavior (optional) export GIT_PS1_SHOWDIRTYSTATE=1 # Show * unstaged, + staged export GIT_PS1_SHOWUNTRACKEDFILES=1 # Show % untracked (optional) export GIT_PS1_SHOWSTASHSTATE=1 # Show $ stashed (optional) export GIT_PS1_SHOWUPSTREAM="auto" # Show upstream status < > = <> export GIT_PS1_HIDE_IF_PWD_IGNORED=1 # Hide if PWD is ignored # Wrapper function using __git_ps1 ps1_git() { __git_ps1 "${C_GIT_RAW}(%s)${C_RESET_RAW}" # Format string for __git_ps1 } else # Fallback to custom function if __git_ps1 is not available ps1_git() { ps1_git_branch_custom_cached } fi # 作业数量显示函数 (修改:输出 Raw ANSI codes) ps1_jobs() { local running=$(jobs -r | wc -l) local stopped=$(jobs -s | wc -l) local jobs_info="" if [[ $running -gt 0 && $stopped -gt 0 ]]; then jobs_info=" ${C_JOBS_RAW}[${running}r/${stopped}s]${C_RESET_RAW}" elif [[ $running -gt 0 ]]; then jobs_info=" ${C_JOBS_RAW}[${running}r]${C_RESET_RAW}" elif [[ $stopped -gt 0 ]]; then jobs_info=" ${C_JOBS_RAW}[${stopped}s]${C_RESET_RAW}" fi # Output colored info (with a leading space) echo -ne "$jobs_info" } # 设置提示符 (修改:正确使用 \[...\] 包围颜色代码和函数调用) if [[ "$color_prompt" == "yes" ]]; then PS1="\n\[${C_USER_RAW}\]\u@\h\[${C_RESET_RAW}\] \[${C_PATH_RAW}\]\w\[${C_RESET_RAW}\]\[\$(ps1_git)\]\[\$(ps1_jobs)\]\n\[${C_PROMPT_RAW}\]➤ \[${C_RESET_RAW}\]" else PS1="\n\u@\h \w \$(ps1_git)\n➤ " # Plain prompt with git info fi unset color_prompt # Clean up temporary variable # ------------------------------------------ # 插件加载 # ------------------------------------------ # 自动补全增强 if ! shopt -oq posix; then if [ -f /usr/share/bash-completion/bash_completion ]; then # shellcheck source=/dev/null . /usr/share/bash-completion/bash_completion elif [ -f /etc/bash_completion ]; then # shellcheck source=/dev/null . /etc/bash_completion fi fi # ------------------------------------------ # 加载 Shell 增强功能 # ------------------------------------------ if [ -f "$HOME/.bash_shell_enhancements" ]; then # shellcheck source=/dev/null . "$HOME/.bash_shell_enhancements" else # 如果找不到增强文件,显示基本欢迎信息 echo -e "\033[38;5;213m" echo "Welcome Back!" echo -e "\033[0m" echo "注意: Shell增强工具未找到,可通过创建 ~/.bash_shell_enhancements 来启用额外功能" fi # ------------------------------------------ # 其他配置文件 # ------------------------------------------ if [ -f ~/.bash_aliases ]; then # shellcheck source=/dev/null . ~/.bash_aliases fi # 加载本地特定配置(如果存在) if [ -f ~/.bashrc.local ]; then # shellcheck source=/dev/null . ~/.bashrc.local fi