简介
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 # 替换为实际用户名
切换完成后会显示如下信息(安装 oh-my-zsh 成功后也会提示切换):
# sudo chsh -s /bin/zshChanging 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 shouldmake 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-zshUsing 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 的别名:
青鸟内测(手机app封装、托管系统)
注意:请在linux环境下测试或生产使用 青鸟内测是一个移动应用分发系统,支持安卓苹果应用上传与下载,并且还能快捷封装网址为应用。应用内测分发:一键上传APP应用包,自动生成下载链接和二维码,方便用户内测下载。应用封装:一键即可生成app,无需写代码,可视化编辑、 直接拖拽组件制作页面的高效平台。工具箱:安卓证书生成、提取UDID、Plist文件在线制作、IOS封装、APP图标在线制作APP分发:
0 查看详情
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 aliasesalias 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_zshAre you sure you want to remove Oh My Zsh? [y/N] Y
终端提示信息:
Removing ~/.oh-my-zshLooking for original zsh config...Found ~/.zshrc.pre-oh-my-zsh -- Restoring to ~/.zshrcFound ~/.zshrc -- Renaming to ~/.zshrc.omz-uninstalled-20170820200007Your original zsh config was restored. Please restart your session.Thanks for trying out Oh My Zsh. It's been uninstalled.
参考文献
https://www.php.cn/link/5122eee5a7e2768194775f68037e2ebchttps://www.php.cn/link/70df77ba22ee6052c7021b41f0bdf3dbhttps://www.php.cn/link/5d36b2dba58acec55fa2a9b197fb3e1dwiki/Themeshttps://www.php.cn/link/a5de315acb973b8e6da83458c9e456d3https://www.php.cn/link/d54be4ff5a9dad8e016206a562bb7915https://www.php.cn/link/37c429609aa5ffe35484714281ebcb23https://www.php.cn/link/6b027466c3ca21b6d1a1d594d6820833
以上就是Linux ZSH 更便捷的 shell 环境的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/942919.html
微信扫一扫
支付宝扫一扫