Zsh 简单配置指南

最近虚拟机安的有点多,每次都要重新配置一遍 Zsh,所以记录一下配置过程

安装 Zsh 和 Git

1
2
sudo apt-get update
sudo apt-get install zsh git

安装 Oh My Zsh

Oh My Zsh 是一个社区驱动的框架,用于管理 Zsh 配置。它包含了许多有用的功能,助手,插件和主题

1
2
3
4
5
6
7
8
9
# 使用wget
sh -c "$(wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"
# 或者使用curl
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

# 若上面执行失败,可使用国内镜像
sh -c "$(curl -fsSL https://gitee.com/pocmon/ohmyzsh/raw/master/tools/install.sh)"
# 或者
sh -c "$(wget -O- https://gitee.com/pocmon/ohmyzsh/raw/master/tools/install.sh)"

对于弹出的提示,输入y即可

常用插件的安装

zsh-syntax-highlighting

语法高亮插件,可以在输入命令时,高亮显示命令的是否正确

1
2
3
4
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

# 国内加速链接
git clone https://gh.xmly.dev/https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

zsh-autosuggestions

自动补全插件,可以在输入命令时,自动补全命令

1
2
3
4
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

# 国内加速链接
git clone https://gh.xmly.dev/https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

其他插件

可在下述仓库中找到更多有意思的插件:
https://github.com/unixorn/awesome-zsh-plugins

zshrc 配置

修改插件

编辑~/.zshrc文件,找到plugins行,修改为

1
2
3
plugins=(git zsh-syntax-highlighting zsh-autosuggestions) # 按需添加已经安装的插件
# 下面是我的个人备份
plugins=(git zsh-autosuggestions zsh-syntax-highlighting z extract web-search)

修改主题

编辑~/.zshrc文件,找到ZSH_THEME行,修改为

1
ZSH_THEME="af-magic" # 修改为"af-magic主题

上面的是默认自带的主题,下面给出一个在 What’s the best theme for Oh My Zsh? 中排名靠前的主题 powerlevel10k

1
2
3
4
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

# 中国用户可以使用 gitee.com 上的官方镜像加速下载
git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

编辑~/.zshrc文件,找到ZSH_THEME行,修改为

1
ZSH_THEME="powerlevel10k/powerlevel10k"

使配置生效

每次修改配置文件后,都需要执行以下命令使配置生效

1
source ~/.zshrc

相关文档链接

https://www.haoyep.com/posts/zsh-config-oh-my-zsh/


Zsh 简单配置指南
http://ciliphen.github.io/Zsh简单配置指南/
作者
Felix
发布于
2025年1月15日
更新于
2025年5月7日
许可协议