在 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 的。

oh-my-zsh

本文转载自:

作者:sysin

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

链接:https://sysin.org/blog/linux-zsh/

查看当前默认shell

echo $SHELL

通常Linux默认shell为/bin/bash

安装 zsh

CentOS:

yum -y install zsh
# or
#dnf -y install zsh
yum -y install util-linux-user

Ubuntu:

sudo apt install zsh

设置默认 shell

  • 为 root 设置默认 shell
sudo chsh -s /bin/zsh
  • 为特定用户设置默认 shell
sudo chsh -s /bin/zsh <username>
# <username> 替换为实际用户名

安装git

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

# CentOS
yum -y instal git
# Ubuntu
sudo apt install git

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

安装 oh-my-zsh

oh-my-zsh 是一个zsh shell下的插件/主题管理器,可以很方便的添加插件和主题。

自动安装 oh-my-zsh

sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

注:raw.github.com 可能会因为访问不稳定而无法下载脚本,请自行尝试其他方法下载脚本。

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.

手动安装 oh-my-zsh

下载源码

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

复制配置

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

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

# 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 目录

插件

内置自动补全功能

默认 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-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
)

zsh-syntax-highlighting

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

安装:

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

配置启用插件:

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

plugins=(
    git
    # other plugins...
    zsh-syntax-highlighting
)
文章目录