zsh 配置文件体系

zsh 通过一系列配置文件定制 shell 行为,根据作用范围可分为系统级和用户级,按执行时机又分为四类。

系统范围配置文件

对所有用户生效,存放在 /etc/ 目录下:

配置文件 执行时机 用途
/etc/zshenv 每次 zsh 启动 设置全局环境变量
/etc/zshrc 交互式启动 终端外观、别名、函数定义
/etc/zlogin 用户登录时 登录时的全局行为,如显示系统信息
/etc/zlogout 用户注销时 注销时的全局行为

用户级配置文件

仅对当前用户生效,存放目录可通过 echo $ZDOTDIR 查看:

配置文件 执行时机 用途
~/.zshenv 每次 zsh 启动 用户级环境变量和个人偏好
~/.zshrc 交互式启动 推荐使用,个人 shell 环境主配置
~/.zlogin 用户登录时 个人登录时需要执行的命令
~/.zlogout 用户注销时 个人注销时需要执行的命令

执行顺序一览

系统级配置文件优先于用户级同名单配置文件执行,从左到右依次加载:

配置阶段 Login Shell Interactive Shell 脚本
zshenv(系统 + 用户)
zshrc(系统 + 用户)
zlogin(系统 + 用户)
zlogout(系统 + 用户)

日常开发绝大多数场景都是交互式 Shell,因此 ~/.zshrc 是最重要的配置文件。


插件管理器:zinit

强烈推荐 zinit,启动速度极快,是目前最灵活的 zsh 插件管理器。

安装

1
2
3
4
5
6
# 下载安装脚本
bash -c "$(curl --fail --show-error --silent --location \
https://raw.githubusercontent.com/zdharma-continuum/zinit/HEAD/scripts/install.sh)"

# 编译 zinit,提升加载速度
zinit self-update

常用命令

加载插件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 标准加载(支持启动分析,便于排查插件性能)
zinit load [插件]

# 轻量加载(静默启动,速度最快)
zinit light [插件]

# 从任意 URL 加载插件
zinit snippet "https://github.com/maomao1996/dotfiles/blob/main/zsh/femm.plugin.zsh"

# 延迟加载 Oh My Zsh 插件
zinit ice svn
zinit lucid wait="1" for \
OMZL::clipboard.zsh \ # 延迟 1s 加载 clipboard
OMZP::git # 加载 git.plugin

更新:

1
2
3
zinit self-update        # 更新 zinit 本身
zinit update # 更新所有插件
zinit update --parallel # 并发更新所有插件

卸载:

1
2
3
zinit delete [插件]      # 卸载指定插件
zinit delete --all # 卸载所有插件
zinit delete --clean # 清理未使用的插件

为什么不用 oh-my-zsh?

oh-my-zsh 是最经典的 zsh 配置框架,支持主题配置和插件安装,但加载速度明显偏慢,日常使用能感觉到终端启动的延迟。以下是 oh-my-zsh 的基本用法,保留仅供了解:

1
2
3
4
5
6
7
# 安装
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# 国内镜像
sh -c "$(curl -fsSL https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh)"

# 更新
omz update

常用插件

autojump:目录快速跳转

基于频率统计的目录跳转工具,记录你最常访问的目录,用简短关键字秒跳。

1
brew install autojump

z:Oh My Zsh 内置跳转

与 autojump 功能类似,是 oh-my-zsh 内置插件。若使用 zinit,可通过 zinit lucid 延迟加载:

1
zinit lucid wait="1" for OMZP::git

fast-syntax-highlighting:命令语法高亮

实时高亮当前输入的命令,成功命令和历史命令以不同颜色显示,输入体验大幅提升。

1
zinit light zdharma-continuum/fast-syntax-highlighting

zsh-autosuggestions:命令自动补全

根据历史记录和补全结果,以灰色文字实时提示可补全的命令,按方向键 → 或回车即可确认。

1
2
zinit ice lucid wait="0" atload="_zsh_autosuggest_start"
zinit light zsh-users/zsh-autosuggestions

主题:Powerlevel10k

Powerlevel10k 是目前最流行的 zsh 主题,速度快、定制性强,启动时会引导交互式配置。

安装

1
2
zinit ice depth=1
zinit light romkatv/powerlevel10k

~/.zshrc 末尾添加:

1
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

配置

使用 iTerm2 时会自动安装所需字体。执行交互式配置向导:

1
p10k configure

修复 VS Code 终端图标乱码

在 VS Code 设置中修改 terminal.integrated.fontFamily

1
"terminal.integrated.fontFamily": "MesloLGS NF"

常用操作

1
2
3
4
5
6
7
8
9
10
11
# 更新主题
git -C ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k pull

# 查看当前配置中使用的图标名称
get_icon_names

# 自定义主题配置
code ~/.p10k.zsh
# 修改 POWERLEVEL9K_LEFT_PROMPT_ELEMENTS / POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS
# 例如显示当前 Node.js 版本
source ~/.zshrc

完整配置参考

以下是整合后的 ~/.zshrc 核心配置,基于 zinit + Powerlevel10k:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#--------------------------------------------------#
# 加载 Powerlevel10k 主题
#--------------------------------------------------#
zinit ice depth=1
zinit light romkatv/powerlevel10k
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

#--------------------------------------------------#
# 加载语法高亮和自动补全
#--------------------------------------------------#
zinit ice lucid wait="0" atload="_zsh_autosuggest_start"
zinit light zsh-users/zsh-autosuggestions
zinit light zdharma-continuum/fast-syntax-highlighting

#--------------------------------------------------#
# 延迟加载 Oh My Zsh 插件
#--------------------------------------------------#
zinit ice svn
zinit lucid wait="1" for \
OMZL::clipboard.zsh \
OMZL::completion.zsh \
OMZL::directories.zsh \
OMZL::history.zsh \
OMZL::key-bindings.zsh \
OMZL::theme-and-appearance.zsh \
OMZL::git.zsh \
OMZP::git

#--------------------------------------------------#
# 加载自定义插件
#--------------------------------------------------#
zinit snippet "https://github.com/maomao1996/dotfiles/blob/main/zsh/femm.plugin.zsh"

#--------------------------------------------------#
# 环境变量
#--------------------------------------------------#
HIST_STAMPS="yyyy-mm-dd"
export LESS="-R"

自定义插件详解

自定义插件托管在 maomao1996/dotfiles,核心内容如下:

常用别名:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 包管理工具
alias p="pnpm"
alias pnpx="pnpm dlx"
alias d="bun run dev"
alias s="bun run start"
alias b="bun run build"
alias t="bun run test"
alias dp="bun run deploy"

# 代码检查
alias lint="nr lint"
alias lintf="nr lint --fix"

# git 操作
alias glogp="git log --pretty='%C(yellow)%h%C(reset) %ad %C(green)%s%C(reset) %C(red)%d%C(reset) %C(bold blue)[%an]%C(reset)'"
alias gcl1="git clone --depth=1"
alias gpp="proxy && git push && unproxy"
alias glp="proxy && git pull && unproxy"
alias dmm="git checkout master && git pull && git merge develop"

# 工具别名
alias cat="bat"
alias commit="czg"

终端代理开关:

同时设置大写和小写环境变量,兼容不同应用对代理变量的读取差异:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function proxy() {
local http="http://127.0.0.1:7890"
local socks5="socks5://127.0.0.1:7890"

export http_proxy=$http
export HTTP_PROXY=$http
export https_proxy=$http
export HTTPS_PROXY=$http
export all_proxy=$socks5
export ALL_PROXY=$socks5

echo "\033[32m已开启终端代理\033[0m"
}

function unproxy() {
unset http_proxy HTTP_PROXY https_proxy HTTPS_PROXY all_proxy ALL_PROXY
echo "\033[32m已关闭终端代理\033[0m"
}

复制项目信息:

一键复制当前项目的名称和分支名,方便提测沟通:

1
2
3
4
5
6
7
8
9
10
11
12
13
function c() {
if [[ -d .git ]]; then
local data="项目名: $(basename $PWD)\n分支名: $(git_current_branch)"

if echo -n $data | pbcopy; then
echo -e "$data\n\033[32m复制成功\033[0m"
else
echo -e "$data\n\033[33m复制失败,请检查 pbcopy 是否可用\033[0m"
fi
else
echo "\033[33m当前目录不存在 .git 配置\033[0m"
fi
}

自动切换 Git 身份:

针对 GitHub 仓库自动切换用户名和邮箱,避免在公司项目中误用个人 GitHub 信息提交代码。需自行修改 githubNamegithubEmail 的值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if [[ -d .git ]]; then
local githubName="" # GitHub 用户名
local githubEmail="" # GitHub 邮箱
local url=$(git remote get-url origin)
local localName=$(git config user.name)
local localEmail=$(git config user.email)

if [[ $url =~ "github.com/maomao1996" ]]; then
echo -e "\033[34m当前为 GitHub 项目\033[0m"
if [[ $githubName && $githubName != $localName ]]; then
git config user.name "$githubName"
echo -e "已将当前仓库的 name 从 \033[33m$localName\033[0m 修改为 \033[32m$githubName\033[0m"
fi
if [[ $githubEmail && $githubEmail != $localEmail ]]; then
git config user.email "$githubEmail"
echo -e "已将当前仓库的 email 从 \033[33m$localEmail\033[0m 修改为 \033[32m$githubEmail\033[0m"
fi
fi
fi

本站由 sswfive 使用 Stellar 1.44.0 主题创建。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。

本站总访问量