# 启动时间统计
zmodload zsh/datetime
start_time=$EPOCHREALTIME

# 启用 Powerlevel10k 即时提示
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

# 基础路径配置
export PATH="$HOME/.local/bin:$PATH"

### Added by Zinit's installer
if [[ ! -f $HOME/.local/share/zinit/zinit.git/zinit.zsh ]]; then
    print -P "%F{33} %F{220}Installing %F{33}ZDHARMA-CONTINUUM%F{220} Initiative Plugin Manager (%F{33}zdharma-continuum/zinit%F{220})…%f"
    command mkdir -p "$HOME/.local/share/zinit" && command chmod g-rwX "$HOME/.local/share/zinit"
    command git clone <https://github.com/zdharma-continuum/zinit> "$HOME/.local/share/zinit/zinit.git" && \\
        print -P "%F{33} %F{34}Installation successful.%f%b" || \\
        print -P "%F{160} The clone has failed.%f%b"
fi

source "$HOME/.local/share/zinit/zinit.git/zinit.zsh"
autoload -Uz _zinit
(( ${+_comps} )) && _comps[zinit]=_zinit

# 加载主题
zinit ice depth=1; zinit light romkatv/powerlevel10k

# 快速加载并编译核心插件
zinit wait lucid for \\
    atinit"zicompinit; zicdreplay" \\
        zdharma-continuum/fast-syntax-highlighting \\
    atload"_zsh_autosuggest_start" \\
        zsh-users/zsh-autosuggestions \\
    blockf atpull'zinit creinstall -q .' \\
        zsh-users/zsh-completions \\
    atload"bindkey '^[[A' history-substring-search-up; bindkey '^[[B' history-substring-search-down" \\
        zsh-users/zsh-history-substring-search

# 加载其他插件
zinit wait lucid for \\
    hlissner/zsh-autopair \\
    MichaelAquilina/zsh-you-should-use \\
    agkozak/zsh-z

# fzf 配置
if [ -f /usr/share/doc/fzf/examples/key-bindings.zsh ]; then
    source /usr/share/doc/fzf/examples/key-bindings.zsh
fi
if [ -f /usr/share/doc/fzf/examples/completion.zsh ]; then
    source /usr/share/doc/fzf/examples/completion.zsh
fi

# fzf 配置
export FZF_DEFAULT_COMMAND='fd --type f --hidden --follow --exclude .git 2>/dev/null || find . -type f'
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
export FZF_DEFAULT_OPTS='--height 40% --layout=reverse --border'

# 如果安装了 bat,添加预览功能
if command -v bat >/dev/null; then
    export FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS --preview 'bat --style=numbers --color=always --line-range :500 {}'"
fi

# 基本选项设置
setopt AUTO_CD              # 允许直接输入目录切换目录
setopt EXTENDED_HISTORY     # 记录命令的时间戳
setopt SHARE_HISTORY       # 共享历史记录
setopt HIST_IGNORE_DUPS    # 忽略重复的命令
setopt HIST_FIND_NO_DUPS   # 查找历史记录时忽略重复
setopt HIST_REDUCE_BLANKS  # 删除历史记录中的空格

# 历史记录配置
HISTFILE=~/.config/zsh/cache/.zsh_history
HISTSIZE=10000
SAVEHIST=10000

# 按键绑定
bindkey '^R' fzf-history-widget
bindkey '^T' fzf-file-widget
bindkey '^[[1;5C' forward-word     # Ctrl+右箭头 向前移动一个词
bindkey '^[[1;5D' backward-word    # Ctrl+左箭头 向后移动一个词

# 新增实用快捷键
bindkey '^A' beginning-of-line     # Ctrl+A 移动到行首
bindkey '^E' end-of-line          # Ctrl+E 移动到行尾
bindkey '^K' kill-line            # Ctrl+K 删除从光标到行尾的内容
bindkey '^U' kill-whole-line      # Ctrl+U 删除整行
bindkey '^W' backward-kill-word   # Ctrl+W 删除前一个单词
bindkey '^H' backward-delete-char # Ctrl+H 删除前一个字符
bindkey '^[[3~' delete-char      # Delete 键删除字符
bindkey '^[[H' beginning-of-line # Home 键移动到行首
bindkey '^[[F' end-of-line      # End 键移动到行尾
bindkey '^P' up-line-or-history  # Ctrl+P 上一条历史命令
bindkey '^N' down-line-or-history # Ctrl+N 下一条历史命令

# exa 别名设置
if command -v exa >/dev/null; then
    alias ls='exa --icons --group-directories-first'
    alias ll='exa --icons --group-directories-first -l'
    alias la='exa --icons --group-directories-first -la'
    alias lt='exa --icons --group-directories-first -T'  # 树形显示
    alias lg='exa --icons --group-directories-first -l --git'  # 显示git状态
else
    # 保持原有的ls别名作为后备
    alias ls='ls --color=auto'
    alias ll='ls -lah'
    alias la='ls -A'
    alias l='ls -CF'
fi

# 别名设置
alias ..='cd ..'
alias ...='cd ../..'
alias c='clear'
alias h='history'
alias update='sudo apt update && sudo apt upgrade'
alias ports='netstat -tulanp'
alias grep='grep --color=auto'
alias df='df -h'
alias du='du -h'
alias free='free -h'
alias top='htop'
alias vim='nvim'

# 补全系统设置
zstyle ':completion:*' menu select
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'

# 定义一个函数来显示系统信息(仅在启动时运行一次)
function show_system_info() {
    # 计算启动时间
    if (( ${+start_time} )); then
        end_time=$EPOCHREALTIME
        load_time=$(( (end_time - start_time) * 1000 ))
        print -P "%F{yellow}ZSH 加载时间: ${load_time}ms%f"
    fi

    # 显示分隔线
    print -P "%F{blue}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━%f"

    # 显示系统信息
    if (( $+commands[neofetch] )); then
        neofetch
    fi

    # 显示分隔线
    print -P "%F{blue}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━%f"

    # 显示简短的提示信息
    print -P "%F{green}快捷键提示:%f %F{yellow}Ctrl+R%f 历史 %F{yellow}Ctrl+T%f 文件 %F{yellow}Alt+C%f 目录"
    print -P "使用 %F{yellow}z 关键词%f 可以快速跳转到常用目录"

    # 显示分隔线
    print -P "%F{blue}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━%f"
}

# 直接运行显示函数(仅在启动时运行一次)
show_system_info

# 加载 powerlevel10k 配置
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

怎么使用?

  1. 更新软件源,并安装基础工具

    sudo apt update
    sudo apt install -y zsh git curl wget neofetch fzf fd-find bat htop neovim
    
  2. 创建配置目录

    mkdir -p ~/.config/zsh/{plugins,cache}
    mkdir -p ~/.local/bin
    mkdir -p ~/.local/share/fonts/
    
  3. 软件软链接

    ln -s $(which fdfind) ~/.local/bin/fd
    ln -s $(which batcat) ~/.local/bin/bat
    
  4. 安装zinit

    bash -c "$(curl --fail --show-error --silent --location <https://raw.githubusercontent.com/zdharma-continuum/zinit/HEAD/scripts/install.sh>)"
    
  5. 安装字体, 给自建的系统安装. windows选择给所有用户安装

    <https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Regular.ttf>
    <https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Bold.ttf>
    <https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Italic.ttf>
    <https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Bold%20Italic.ttf>
    
  6. 将上方的配置复制到 .zshrc

    vim ~/.zshrc