zsh 是为交互式使用而设计的,与 bash 兼容的 shell,尽管它也是一种强大的脚本语言。zsh 融合了 bash、ksh 和 tcsh 的许多有用特性,并引入了许多独特的功能。
相较于 bash,zsh 具有以下优势:
Tab 补全功能强大,支持命令、命令参数和文件路径的补全。丰富的插件支持,允许快速输入先前使用的命令、快速跳转文件夹和显示系统负载等功能。主题丰富且可高度定制。更多关于 zsh 的信息,请访问 https://www.php.cn/link/5122eee5a7e2768194775f68037e2ebc。
macOS:
brew install zsh
Ubuntu:
sudo apt-get install zsh
安装完成后,使用 cat /etc/shells 查看系统可用的 shell:
$ cat /etc/shells # /etc/shells: valid login shells /bin/sh /bin/bash /usr/bin/bash /bin/rbash /usr/bin/rbash /bin/dash /usr/bin/dash /usr/bin/tmux /bin/zsh /usr/bin/zsh
查看当前默认 shell:
$ echo $SHELL /bin/bash
使用 chsh -s /bin/zsh 命令将 zsh 设置为系统默认 shell。
为 root 设置默认 shell:
sudo chsh -s /bin/zsh
为特定用户设置默认 shell:
sudo chsh -s /bin/zsh <username> # <username> 替换为实际用户名
切换完成后会显示如下信息(安装 oh-my-zsh 成功后也会提示切换):
# sudo chsh -s /bin/zsh Changing shell for root. Shell changed.
初次启动 zsh 会进入配置界面,输入 0 可以跳过:
This is the Z Shell configuration function for new users,
zsh-newuser-install.
You are seeing this message because you have no zsh startup files
(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory
~). This function can help you with a few settings that should
make your use of the shell easier.
You can:
(q) Quit and do nothing. The function will be run again next time.
(0) Exit, creating the file ~/.zshrc containing just a comment.
That will prevent this function being run again.
(1) Continue to the main menu.
(2) Populate your ~/.zshrc with the configuration recommended
by the system administrator and exit (you will need to edit
the file by hand, if so desired).
--- Type one of the keys in parentheses --- 0Oh My Zsh 的 GitHub 主页:https://www.php.cn/link/5d36b2dba58acec55fa2a9b197fb3e1d
由于 zsh 的配置复杂度较高,Oh My Zsh 极大地降低了使用门槛。
安装命令:
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" # 或者 sh -c "$(wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"
安装成功提示:
Looking for an existing zsh config...
Found /home/vvd/.zshrc. Backing up to /home/vvd/.zshrc.pre-oh-my-zsh
Using the Oh My Zsh template file and adding it to /home/vvd/.zshrc.
__ __
____ / /_ ____ ___ __ __ ____ _____/ /_
/ __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \
/ /_/ / / / / / / / / / / /_/ / / /_(__ ) / / /
\____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/
/____/ ....is now installed!
Before you scream Oh My Zsh! look over the `.zshrc` file to select plugins, themes, and options.
• Follow us on Twitter: @ohmyzsh
• Join our Discord community: Discord server
• Get stickers, t-shirts, coffee mugs and more: Planet Argon Shop
➜ ~使用 vim 编辑 ~/.zshrc:
vim ~/.zshrc
其中 ZSH_THEME 是主题字段,主题信息可在此查看。
例如,将主题字段修改为 jonathan:

也可以随机设置主题:
ZSH_THEME="random"
每次打开终端,主题将会随机变化。推荐尝试 powerlevel10k 主题,项目地址为:https://www.php.cn/link/12b865d68061b0c406c140adf7076aee
安装 powerlevel10k 主题的方法:
git clone --depth=1 https://www.php.cn/link/12b865d68061b0c406c140adf7076aee.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k编辑 .zshrc,添加以下内容并保存:
ZSH_THEME="powerlevel10k/powerlevel10k"
最后,执行 source ~/.zshrc 使配置生效,按照提示配置主题。
查看 git 的别名:
cat ~/.oh-my-zsh/plugins/git/git.plugin.zsh
......alias g='git'alias ga='git add'alias gaa='git add --all'alias gapa='git add --patch'alias gau='git add --update'alias gav='git add --verbose'alias gap='git apply'alias gapt='git apply --3way'......
自定义别名,直接在 ~/.zshrc 的最下面添加:
# Example aliases alias ll='ls -lahF --color --time-style=long-iso'
或者使用 echo 写入:
echo 'alias ll="ls -lahF --color --time-style=long-iso"' >> ~/.zshrc
Oh My Zsh 内置的自动补全功能包括:
cd 并按 tab 键,目录将自动列出,再按 tab 可以切换。/usr/local/bin,只需输入 cd /u/l/b 并按 tab 键自动补全。cd de 并按 tab 键自动补全。kubectl 并按 tab 键即可看到可用命令。kill 并按 tab 键会自动显示进程的 process id。额外的自动补全功能,通过以下命令克隆仓库:
git clone --depth=1 https://github.com/zsh-users/zsh-completions ${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins/zsh-completions在 .zshrc 中添加以下行,确保在 source "$ZSH/oh-my-zsh.sh" 之前:
fpath+=${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins/zsh-completions/src注意:将其作为常规的 Oh My Zsh 插件添加不会正常工作(参见 #603)。
zsh-autosuggestions
根据历史输入命令的记录即时提示,按 → 键即可补全:
git clone --depth=1 https://github.com/zsh-users/zsh-autosuggestions.git ${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins/zsh-autosuggestions编辑 ~/.zshrc,找到 plugins=(git) 这一行,修改为:
plugins=(
git
# other plugins...
zsh-autosuggestions
)Incremental completion on zsh
增强的实时自动命令补全插件:Incremental completion on zsh。
语法高亮插件
插件名称:zsh-syntax-highlighting
作用:命令错误会显示红色,直到输入正确才会变绿色,路径正确会显示下划线。
安装:
git clone --depth=1 https://www.php.cn/link/d54be4ff5a9dad8e016206a562bb7915.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting配置启用插件:
编辑 ~/.zshrc,在 plugins 部分添加插件名称:
plugins=([plugins...] zsh-syntax-highlighting)
开启新的 Shell 或执行 source ~/.zshrc,即可体验插件。
文件夹快捷跳转插件
z 是一个文件夹快捷跳转插件,对于曾经跳转过的目录,只需输入最终目标文件夹名称即可快速跳转,提高切换文件夹的效率。
由于 Oh My Zsh 内置了 z 插件,只需在 .zshrc 中将 z 加入插件列表:
plugins=(
# other plugins...
zsh-autosuggestions
zsh-syntax-highlighting
z
)打开终端输入:
upgrade_oh_my_zsh
终端输入:
uninstall_oh_my_zsh Are you sure you want to remove Oh My Zsh? [y/N] Y
终端提示信息:
Removing ~/.oh-my-zsh Looking for original zsh config... Found ~/.zshrc.pre-oh-my-zsh -- Restoring to ~/.zshrc Found ~/.zshrc -- Renaming to ~/.zshrc.omz-uninstalled-20170820200007 Your original zsh config was restored. Please restart your session. Thanks for trying out Oh My Zsh. It's been uninstalled.
以上就是Linux ZSH 更便捷的 shell 环境的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号