更优雅的命令行输入工具 - Oh-My-ZSH
引言
自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…
效果展示
过程
下载
首先用zsh --version
检查zsh
是否存在以及已安装版本。
如不存在,可以根据平台自行选择安装版本,具体参考知乎zsh 安装与配置:9 步打造高效命令行.
需要注意的是,Oh-My-ZSH
需要 v4.3.9 及其以上。
如满足安装要求,可使用curl
, wget
或fetch
等工具下载。
1 | sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" |
如运行失败考虑是网络环境不好,自行解决。
插件
插件的安装使用有两种方法,一种是启用官方插件目录中的插件,一种是手动启用。
oh-my-zsh
的自带插件都储存在~/.oh-my-zsh/plugins
目录中,如果希望使用一个自带插件,可以在~/.zshrc
的plugins=(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 | plugins=( |
Start a new terminal session
Start a new terminal session
Manual (Git Clone)
- Clone this repository somewhere on your machine. This guide will assume
~/.zsh/zsh-autosuggestions
.
1 | # zsh-autosuggestions |
- Add the following to your
.zshrc
:
1 | # zsh-autosuggestions |
- Start a new terminal session.
另外,Homebrew
可提供了一些插件的下载,如:
1 | # Homebrew |
之后同样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"
.
Mac 终端中文乱码修复
1 | # 更改 |
更改内容:
1 | export LC_ALL=en_US.UTF-8 |
ubuntu 安装
需要提前安装 zsh, 其他与MacOS
一致。
1 | # apt安装软件 |
自动更新关闭
使用vim ~/.zshrc
更改配置文件。
具体讲就是加上被注释掉的DISABLE_AUTO_UPDATE="true"
.