首页 > 运维 > linux运维 > 正文

Linux ZSH 更便捷的 shell 环境

絕刀狂花
发布: 2025-07-19 08:22:12
原创
708人浏览过

简介

zsh 是为交互式使用而设计的,与 bash 兼容的 shell,尽管它也是一种强大的脚本语言。zsh 融合了 bash、ksh 和 tcsh 的许多有用特性,并引入了许多独特的功能。

相较于 bash,zsh 具有以下优势:

Tab 补全功能强大,支持命令、命令参数和文件路径的补全。丰富的插件支持,允许快速输入先前使用的命令、快速跳转文件夹和显示系统负载等功能。主题丰富且可高度定制。更多关于 zsh 的信息,请访问 https://www.php.cn/link/5122eee5a7e2768194775f68037e2ebc

安装 zsh

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

查看当前默认 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 --- 0
登录后复制

安装 Oh My Zsh

Oh 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

Linux ZSH 更便捷的 shell 环境

也可以随机设置主题:

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 的别名:

智谱清言 - 免费全能的AI助手
智谱清言 - 免费全能的AI助手

智谱清言 - 免费全能的AI助手

智谱清言 - 免费全能的AI助手2
查看详情 智谱清言 - 免费全能的AI助手
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 键自动补全。
  • 自动大小写更正:访问 Desktop 文件夹,只需输入 cd de 并按 tab 键自动补全。
  • 自动命令补全:输入 kubectl 并按 tab 键即可看到可用命令。
  • 自动补全命令参数:输入 kill 并按 tab 键会自动显示进程的 process id。

zsh-completions

额外的自动补全功能,通过以下命令克隆仓库:

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
)
登录后复制

升级 Oh My Zsh

打开终端输入:

upgrade_oh_my_zsh
登录后复制

卸载 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中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号