Linux Zsh 使用 oh-my-zsh 打造高效便捷的 shell 环境

Zsh 完全兼容 bash,支持强大的智能命令补全功能,支持大量的界面主题和插件,功能和效率都极大的增强和提高。

Posted by sysin on 2022-06-01
Estimated Reading Time 8 Minutes
Words 1.8k In Total
更新日期:Wed Jun 01 2022 10:30:31 GMT+0800,阅读量:

请访问原文链接:Linux Zsh 使用 oh-my-zsh 打造高效便捷的 shell 环境,查看最新版。原创作品,转载请保留出处。

作者主页:sysin.org


无耻抄袭者 Yu Tao,请立遁!!!

OMZLogo

在 Unix/Linux 的世界里,人机交互的工具就是 shell 了,由于各个发行版的系统默认 shell 都是 bash,所以对 bash 的熟悉度是最高的。其实还有一款效率远远超过 bash 的 shell,叫做 zsh。其实 zsh 在 1990 年的时候就出现了 (sysin),只比 bash 晚一年,而且 zsh 在 bash 的基础上做了很多功能上、性能上的改进。

zsh 有很多 bash 所没有的功能,但是 zsh 的初期配置太过繁琐,流行率一直不高,直到 github 上 oh-my-zsh 项目的出现,使大家使用 zsh 的便捷性大大提高,从 macOS Catalina 版开始默认使用 zsh,使得 zsh 得到了更加广泛的关注。现在 Kail Linux 也默认是使用 zsh。

很重要的一点,zsh 完全兼容 bash,如果你的 bash 脚本开头是以 !#/bin/bash 的话,在 zsh 下还是完全兼容 bash 的。

Ubuntu 20.04 内置了如下几种 shell:

1
2
3
4
5
6
7
8
9
10
11
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
/usr/bin/screen

CentOS 8 内置了如下几种 shell:

1
2
3
4
5
cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash

1. 查看当前 shell

1
echo $SHELL

返回结果:

1
/bin/bash

2. 安装 zsh

CentOS:

1
2
3
4
yum -y install zsh
# or
#dnf -y install zsh
yum -y install util-linux-user

Ubuntu:

1
sudo apt install zsh

3. 设置默认 shell

  • 为 root 设置默认 shell

    1
    sudo chsh -s /bin/zsh
  • 为特定用户设置默认 shell

    1
    2
    sudo chsh -s /bin/zsh <username>
    # <username> 替换为实际用户名

返回结果如下,表示切换完成(下载安装 oh-my-zsh 成功后也会提示切换)

1
2
3
# sudo chsh -s /bin/zsh
Changing shell for root.
Shell changed.

4. 安装 git

自动和手动安装都需要安装 git,执行

1
2
3
4
# CentOS
yum -y instal git
# Ubuntu
sudo apt install git

安装完成后选择一种 oh-my-zsh 的安装方式(自动安装或者手动安装)。

5. 安装 oh-my-zsh

自动安装 oh-my-zsh

1
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
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
Cloning Oh My Zsh...
Cloning into '/root/.oh-my-zsh'...
remote: Enumerating objects: 1158, done.
remote: Counting objects: 100% (1158/1158), done.
remote: Compressing objects: 100% (1127/1127), done.
remote: Total 1158 (delta 20), reused 1059 (delta 15), pack-reused 0
Receiving objects: 100% (1158/1158), 778.08 KiB | 218.00 KiB/s, done.
Resolving deltas: 100% (20/20), done.

Looking for an existing zsh config...
Using the Oh My Zsh template file and adding it to ~/.zshrc.

Time to change your default shell to zsh:
Do you want to change your default shell to zsh? [Y/n] Y
Changing the shell...
Changing shell for root.
Shell changed.
Shell successfully changed to '/usr/bin/zsh'.

__ __
____ / /_ ____ ___ __ __ ____ _____/ /_
/ __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \
/ /_/ / / / / / / / / / / /_/ / / /_(__) / / /
\____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/
/____/ ....is now installed!


Before you scream Oh My Zsh! please look over the ~/.zshrc file to select plugins, themes, and options.

• Follow us on Twitter: https://twitter.com/ohmyzsh
• Join our Discord server: https://discord.gg/ohmyzsh
• Get stickers, shirts, coffee mugs and other swag: https://shop.planetargon.com/collections/oh-my-zsh

手动安装 oh-my-zsh

下载源码

1
git clone --depth=1 https://github.com/ohmyzsh/ohmyzsh.git ~/.oh-my-zsh

复制配置

1
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc

6. 修改主题

1
vi ~/.zshrc

ZSH_THEME 字段就是主题 (sysin),例如修改为 “ys”,主题样式这里 查看。

1
2
3
4
5
6
7
8
9
10
11
12
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

# Path to your oh-my-zsh installation.
export ZSH="/root/.oh-my-zsh"

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
#ZSH_THEME="robbyrussell"
ZSH_THEME="ys"

注:主题文件在 ~/.oh-my-zsh/themes 目录

快速修改:

1
sed -i '/^ZSH_THEME=.*/c ZSH_THEME="ys"' ~/.zshrc

修改配置后,通过 source ~/.zshrc 或者退出重新登录使配置生效。

7. 别名配置

我们看下 git 的别名:

1
cat ~/.oh-my-zsh/plugins/git/git.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
......

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 中,最下面直接写即可。

1
2
# Example aliases
alias ll='ls -lahF --color --time-style=long-iso'

或者直接使用 echo 写入。

1
echo 'alias ll="ls -lahF --color --time-style=long-iso"' >> ~/.zshrc

更强大的 alias 命令,比如下面命令,当你在 zsh 环境下输入 hello.py 即可直接用 vim 打开文件编辑,一个 tgz 的文件即可自动解压缩。

1
2
3
alias -s py=vim
alias -s conf=vim
alias -s tgz='tar zxvf'

8. 命令自动补全

内置自动补全功能

默认 oh-my-zsh 命令自动补全功能如下:

  • 自动列出目录

    输入 cd 按 tab 键,目录将自动列出,在按 tab 可以切换

  • 自动目录名简写补全

    要访问 /usr/local/bin 这个长路径,只需要 cd /u/l/b 按 tab 键自动补全

  • 自动大小写更正

    要访问 Desktop 文件夹,只需要 cd de 按 tab 键自动补全,或者查看 README.md,只需要 cat rea 自动更正补全

  • 自动命令补全

    输入 kubectl 按 tab 键即可看到可用命令

  • 自动补全命令参数

    输入 kill 按 tab 键会自动显示出进程的 process id

小技巧:

可以忽略 cd 命令,输入 .. 或者 ... 和当前目录名都可以跳转。

上述功能不需要额外的插件。

zsh-completions

额外的自动补全功能,该项目将在完善时合并到 zsh,补充 zsh 中尚不支持的命令补全 (sysin)。

  • Clone the repository inside your oh-my-zsh repo:

    1
    git clone --depth=1 https://github.com/zsh-users/zsh-completions ${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins/zsh-completions
  • Add it to FPATH in your .zshrc by adding the following line before source "$ZSH/oh-my-zsh.sh":

    1
    fpath+=${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins/zsh-completions/src

Note: adding it as a regular Oh My ZSH! plugin will not work properly (see #603).

另外还有一些插件来增强命令补全,可根据需要启用:

zsh-autosuggestions

作用是根据历史输入命令的记录即时的提示(建议补全),然后按 → 键即可补全。

1
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) 这一行,修改为:

1
2
3
4
5
plugins=(
git
# other plugins...
zsh-autosuggestions
)

Incremental completion on zsh

增强的实时自动命令补全插件:Incremental completion on zsh

该插件对性能似乎有一点点影响,请根据需要启用。

作用如图:

incr

1
2
3
4
mkdir $ZSH_CUSTOM/plugins/incr
curl -fsSL https://mimosa-pudica.net/src/incr-0.2.zsh -o $ZSH_CUSTOM/plugins/incr/incr.zsh
echo 'source $ZSH_CUSTOM/plugins/incr/incr.zsh' >> ~/.zshrc
source ~/.zshrc

9. 语法高亮插件

插件名称:zsh-syntax-highlighting

作用:命令错误会显示红色,直到你输入正确才会变绿色,另外路径正确会显示下划线。

安装:

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

配置启用插件:

编辑 ~/.zshrc,以下部分添加插件的名字

1
plugins=([plugins...] zsh-syntax-highlighting)

10. oh-my-zsh 官网


捐助本站 ❤️ Donate

点击访问官方网站


文章用于推荐和分享优秀的软件产品及其相关技术,所有软件默认提供官方原版(免费版或试用版),免费分享。对于部分产品笔者加入了自己的理解和分析,方便学习和测试使用。任何内容若侵犯了您的版权,请联系作者删除。如果您喜欢这篇文章或者觉得它对您有所帮助,或者发现有不当之处,欢迎您发表评论,也欢迎您分享这个网站,或者赞赏一下作者,谢谢!

支付宝赞赏 微信赞赏

赞赏一下


☑️ 评论恢复,欢迎留言❗️
敬请注册!点击 “登录” - “用户注册”(已知不支持 21.cn/189.cn 邮箱)。请勿使用联合登录(已关闭)