引言

macOS Catalina起, zsh 已取代 bash 成为新版操作系统中的默认 shell 。既然有这个条件,不如试试更优雅的命令行输入吧。

Oh My Zsh是一个基于zsh命令行,提供了主题配置,插件机制,并内置了许多便捷操作的命令行输入工具。正如Oh My Zsh官网描述的Unleash your terminal like never before., 他确实给我带来了全新的输入体验。

其中最另我不能脱离的功能有二。

  • 根据历史记录自动补全
  • 不用cd直接进入文件夹

Oh My Zsh is a delightful, open source, community-driven framework for managing your Zsh configuration. It comes bundled with thousands of helpful functions, helpers, plugins, themes, and a few things that make you shout…

效果展示

Thame - robbyrussell

过程

下载

首先用zsh --version检查zsh是否存在以及已安装版本。

如不存在,可以根据平台自行选择安装版本,具体参考知乎zsh 安装与配置:9 步打造高效命令行.

需要注意的是,Oh-My-ZSH需要 v4.3.9 及其以上。

如满足安装要求,可使用curl, wgetfetch等工具下载。

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

如运行失败考虑是网络环境不好,自行解决。

插件

插件的安装使用有两种方法,一种是启用官方插件目录中的插件,一种是手动启用。

oh-my-zsh 的自带插件都储存在~/.oh-my-zsh/plugins目录中,如果希望使用一个自带插件,可以在~/.zshrcplugins=(xxx, xxx, ...)这一行里加入插件名称。

当插件不包含在官方库中时,可以自行下载并启用,以zsh-autosuggestions: zsh-autosuggestions - github为例。

zsh-syntax-highlighting 是一个命令语法校验插件,在输入命令的过程中,若指令不合法,则指令显示为红色,若指令合法就会显示为绿色。效果如下:

Oh My Zsh

Clone this repository

Clone this repository into $ZSH_CUSTOM/plugins (by default ~/.oh-my-zsh/custom/plugins)

1
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
Add the plugin to the list of plugins

Add the plugin to the list of plugins for Oh My Zsh to load (inside ~/.zshrc)

1
2
3
4
plugins=(
# other plugins...
zsh-autosuggestions
)
Start a new terminal session

Start a new terminal session

Manual (Git Clone)

  1. Clone this repository somewhere on your machine. This guide will assume ~/.zsh/zsh-autosuggestions.
1
2
3
4
# zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-autosuggestions ~/.zsh/zsh-autosuggestions
# zsh-syntax-highlighting
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ~/.zsh/zsh-syntax-highlighting
  1. Add the following to your .zshrc:
1
2
3
4
# zsh-autosuggestions
source ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh
# zsh-syntax-highlighting
source ~/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
  1. Start a new terminal session.

另外,Homebrew可提供了一些插件的下载,如:

1
2
# Homebrew
brew install zsh-syntax-highlighting

之后同样source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh.

主题

Oh My Zsh支持许多主题的设置,编辑 ~/.zshrc 文件即可体验。

官方提供了许多默认主题,可以在oh-my-zsh 的 Wiki查看截图,并在~/.zshrc中更改ZSH_THEME="xxx"更换。如使用alanpeabody主题就是ZSH_THEME="alanpeabody".

alanpeabody - Themes

Mac 终端中文乱码修复

详见Mac 终端使用 oh-my-zsh 中文乱码.

1
2
3
4
5
# 更改
vim ~/.zshrc

# 更改后
source ~/.zshrc

更改内容:

1
2
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

ubuntu 安装

需要提前安装 zsh, 其他与MacOS一致。

1
2
3
4
5
6
# apt安装软件
sudo apt-get install zsh
# 查看系统可以用的 shell
cat /etc/shells
# 使用命令将 zsh 设置为系统默认 shell
chsh -s /bin/zsh

自动更新关闭

使用vim ~/.zshrc更改配置文件。

具体讲就是加上被注释掉的DISABLE_AUTO_UPDATE="true".

参考

  1. GitHub - ohmyzsh/ohmyzsh
  2. 打造一个简单好看实用的终端:Oh-My-Zsh 安装,配置以及设置终端代理
  3. 关闭 Oh my zsh 自动更新