tea in the snow 个人 zsh 配置整理

本文适合想参考一套轻量、快速启动的 zsh 配置的用户。你会学到:tea-in-the-snow 的 ~/.zshrc 完整结构——zinit 插件管理、Pure 提示符、9 个常用插件、代理快捷别名与 yazi 文件管理器集成。

仓库地址:https://github.com/tea-in-the-snow/terminal-configuration/blob/main/zsh/zshrc

📋 配置总览

  • 插件管理器:Zinit(轻量化、高性能、支持异步加载)
  • 终端主题:Pure 极简提示符
  • 插件数量:9个常用插件,兼顾功能和启动速度
  • 适用平台:Linux/macOS 通用

🔧 分模块说明

1. 基础配置

# 历史命令配置
HISTFILE=~/.zhistory
HISTSIZE=1000
SAVEHIST=1000
setopt nomatch
bindkey -v  # 使用Vim键位模式

# 补全初始化
autoload -Uz compinit
compinit
  • 历史命令保存1000条
  • 开启Vim模式,支持Vim快捷键操作命令行

2. 环境变量配置

# Rust环境
export PATH=$PATH:$HOME/.cargo/env
# Go环境
export PATH=$PATH:~/go/bin

# GCC彩色报错配置
export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
  • 预配置了Rust和Go开发环境路径
  • 开启GCC编译输出彩色显示,错误警告更醒目

3. 别名配置

基础操作别名

别名命令说明
cclear清屏
eexit退出终端
lglazygit启动LazyGit可视化Git工具
ttmux启动Tmux终端复用
hxhelix启动Helix编辑器
llls -l列表显示文件详情
lals -A显示所有文件(含隐藏文件)
lls -CF简洁显示文件列表

彩色输出别名

自动开启lsgrep等命令的彩色输出,不用额外配置。

代理快捷别名

别名功能
proxy_on开启终端HTTP/HTTPS代理(默认端口7890)
proxy_off关闭终端代理
git_proxy_on开启Git全局代理
git_proxy_off关闭Git全局代理

4. Zinit 插件管理器配置

### Zinit 安装引导(首次打开自动安装Zinit)
if [[ ! -f $HOME/.local/share/zinit/zinit.git/zinit.zsh ]]; then
    # 自动安装Zinit脚本...
fi

# 加载Zinit核心扩展
zinit light-mode for \
    zdharma-continuum/zinit-annex-as-monitor \
    zdharma-continuum/zinit-annex-bin-gem-node \
    zdharma-continuum/zinit-annex-patch-dl \
    zdharma-continuum/zinit-annex-rust
  • 首次打开终端如果没装Zinit会自动安装,零配置上手
  • 加载Zinit官方扩展,支持更多高级功能

5. 加载的插件列表

插件功能优化配置
sindresorhus/pure极简风终端提示符,颜值高性能好预编译优化
zsh-users/zsh-autosuggestions历史命令自动提示,按→补全异步延迟加载,不阻塞启动
zsh-users/zsh-completions丰富的命令补全库,支持主流工具补全立即加载
zsh-users/zsh-syntax-highlighting命令语法高亮,输错命令变红立即加载
mrjohannchang/zsh-interactive-cd支持fzf模糊搜索切换目录需先安装fzf
agkozak/zsh-z快速目录跳转,z 关键词直接跳转到常用目录立即加载
OMZ::plugins/gitGit常用命令别名和增强功能直接复用OMZ插件
OMZ::plugins/safe-paste粘贴长命令时不会自动执行,安全粘贴直接复用OMZ插件
OMZ::plugins/command-not-found输错命令时提示正确的包安装命令直接复用OMZ插件

6. 自定义工具函数

# yazi 文件管理器集成,退出时自动切换到选中目录
function yy() {
	local tmp="$(mktemp -t "yazi-cwd.XXXXXX")"
	yazi "$@" --cwd-file="$tmp"
	if cwd="$(cat -- "$tmp")" && [ -n "$cwd" ] && [ "$cwd" != "$PWD" ]; then
		builtin cd -- "$cwd"
	fi
	rm -f -- "$tmp"
}

输入yy启动yazi文件管理器,退出时会自动cd到你最后浏览的目录,非常实用。

📝 完整配置代码

# 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

# Lines configured by zsh-newuser-install
# HISTFILE=~/.histfile
HISTFILE=~/.zhistory
HISTSIZE=1000
SAVEHIST=1000
setopt nomatch
bindkey -v
# End of lines configured by zsh-newuser-install
# The following lines were added by compinstall
zstyle :compinstall filename '/home/tea-in-the-snow/.zshrc'

autoload -Uz compinit
compinit
# End of lines added by compinstall

# export HISTFILE = "~/.histfile"

# Homebrew config
# eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"

# Load starship
# eval "$(starship init zsh)"

# rust environment
export PATH=$PATH:$HOME/.cargo/env

# add the path of go and those installed by go
# export PATH=$PATH:/usr/local/go/bin
export PATH=$PATH:~/go/bin

# alias
alias c='clear'
alias e='exit'
alias lg='lazygit'
alias t='tmux'
alias hx='helix'

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
        test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
        alias ls='ls --color=auto'
        alias dir='dir --color=auto'
        alias vdir='vdir --color=auto'

        alias grep='grep --color=auto'
        alias fgrep='fgrep --color=auto'
        alias egrep='egrep --color=auto'
fi

# some more ls aliases
alias ll='ls -l'
alias la='ls -A'
alias l='ls -CF'

# colored GCC warnings and errors
export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'

### 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

# Load a few important annexes, without Turbo
# (this is currently required for annexes)
zinit light-mode for \
    zdharma-continuum/zinit-annex-as-monitor \
    zdharma-continuum/zinit-annex-bin-gem-node \
    zdharma-continuum/zinit-annex-patch-dl \
    zdharma-continuum/zinit-annex-rust

### End of Zinit's installer chunk

# zinit ice depth=1; zinit light romkatv/powerlevel10k

# some oh-my-zsh library
zinit snippet OMZ::lib/completion.zsh
zinit snippet OMZ::lib/git.zsh

# pure zsh prompt
zinit ice compile'(pure|async).zsh' pick'async.zsh' src'pure.zsh'
zinit light sindresorhus/pure

# plugins
zinit ice lucid wait='0' atload='_zsh_autosuggest_start'
zinit light zsh-users/zsh-autosuggestions
zinit light zsh-users/zsh-completions
zinit light zsh-users/zsh-syntax-highlighting
zinit light mrjohannchang/zsh-interactive-cd # need to install fzf
zinit light agkozak/zsh-z
zinit snippet OMZ::plugins/git/git.plugin.zsh
zinit snippet OMZ::plugins/safe-paste/safe-paste.plugin.zsh
zinit snippet OMZ::plugins/command-not-found/command-not-found.plugin.zsh

# zsh-autosuggestions config
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=#87afff"

# proxy
# export http_proxy=http://127.0.0.1:7890
# export https_proxy=http://127.0.0.1:7890
alias proxy_on='export http_proxy=http://127.0.0.1:7890 && export https_proxy=http://127.0.0.1:7890 && echo -e http and https proxy on'
alias proxy_off='unset https_proxy http_proxy && echo -e http and https proxy off'
alias git_proxy_on='git config --global http.proxy 127.0.0.1:7890 && git config --global https.proxy 127.0.0.1:7890 && echo -e git proxy on'
alias git_proxy_off='git config --global --unset http.proxy && git config --global --unset https.proxy && echo -e git proxy off'
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
# [[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

# yazi configuration
function yy() {
	local tmp="$(mktemp -t "yazi-cwd.XXXXXX")"
	yazi "$@" --cwd-file="$tmp"
	if cwd="$(cat -- "$tmp")" && [ -n "$cwd" ] && [ "$cwd" != "$PWD" ]; then
		builtin cd -- "$cwd"
	fi
	rm -f -- "$tmp"
}

验证与自查

  1. 新开终端执行 zinit times 能看到各插件加载耗时,启动无阻塞
  2. 输入历史命令前缀会出现灰色自动建议,按 补全
  3. 输入错误命令时显示红色高亮(syntax-highlighting 生效)
  4. z /tmp 能跳转目录;yy 打开 yazi 退出后自动 cd 到所选目录

参考