【原创】MacOS 上 oh-my-zsh 安装与卸载

前言

1、概述

zsh(Z Shell)是 *nix 系中的 shell 的一种,一般的发行版中都已集成了 zsh,比如 MacOS 中你可以使用 which zsh 查看到 /bin/zsh 的存在。

但是 zsh 的使用率却是相对较低,因为大部分用户使用 shell 环境都使用 bash shell。直到 oh-my-zsh 的出现...

oh-my-zsh 是一款开款的工具,它用来帮助你配置、管理你的 zsh 的功能、插件、主题等等的信息,让你的 zsh 使用更多方便,实用。

2、安装环境

  • MacOS 10.15.6

  • zsh 5.7.1

  • git 2.24.3

  • curl 7.64.1

  • wget 1.20.3

  • fetch

3、注意事项

安装开始前,请确保你的 MacOS 上已经有 curl 与 git 工具。正常情况下这两个工具都是 MacOS 自带的,不需要你进行安装。

安装步骤

1、使用官方方式安装 oh-my-zsh

下面的这些命令都是 oh-my-zsh 官方给出的一键式便捷安装命令,可以非常方便地一键安装 oh-my-zsh。

打开 MacOS 中的命令行终端(聚焦搜索 "terminal"),执行如下命令即可开始安装 oh-my-zsh

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

 

也可以 wget 方式

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

还可以使用 fetch 的方式

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

如果你使用上面这些官方给出的一键安装命令能够正常安装(如果你不在国内或者有科学的上网~),那么你可以跳过第 2 步的其他安装方法。

2、其他安装方法

如果执行上面的命令官方给的一键安装命令时出现类似如下问题,表明你所有地区的 raw.github.com raw.githubusercontent.com 已经被墙了~

Failed to connect to raw.github.com port 443: Connection refused

有两种解决方法:

(1) 第一种就是使用 github 的镜像克隆

可以是自己私有的克隆,也可以是别人提供的公有的镜像,下面以公有镜像为例:

sh -c "$(curl -fsSLhttps://raw.staticdn.net/ohmyzsh/ohmyzsh/master/tools/install.sh)"

(2) 第二种就是先把源码克隆到本地,再本地化安装

在用户可读写的任意目录执行如下命令即可进行安装,基本等于一键安装。

git clone https://github.com/ohmyzsh/ohmyzsh; sh ohmyzsh/tools/install.sh

示例:

$ git clone https://github.com/ohmyzsh/ohmyzsh; sh ohmyzsh/tools/install.sh
Cloning into 'ohmyzsh'...
remote: Enumerating objects: 25383, done.
remote: Total 25383 (delta 0), reused 0 (delta 0), pack-reused 25383
Receiving objects: 100% (25383/25383), 7.18 MiB | 726.00 KiB/s, done.
Resolving deltas: 100% (12771/12771), done.
...
Cloning Oh My Zsh...
Cloning into '/Users/jiangzhuolin/.oh-my-zsh'...
remote: Enumerating objects: 1173, done.
remote: Counting objects: 100% (1173/1173), done.
remote: Compressing objects: 100% (1142/1142), done.
remote: Total 1173 (delta 19), reused 1091 (delta 15), pack-reused 0
Receiving objects: 100% (1173/1173), 807.79 KiB | 169.00 KiB/s, done.
Resolving deltas: 100% (19/19), done.
...
Looking for an existing zsh config...
Using the Oh My Zsh template file and adding it to ~/.zshrc.
...
         __                                     __
  ____  / /_     ____ ___  __  __   ____  _____/ /_
 / __ \/ __ \   / __ `__ \/ / / /  /_  / / ___/ __ \
/ /_/ / / / /  / / / / / / /_/ /    / /_(__  ) / / /
\____/_/ /_/  /_/ /_/ /_/\__, /    /___/____/_/ /_/
                        /____/                       ....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

建议使用第二种方法进行安装,因为第一种方法使用第三方提供的镜像不知道是否稳定,说不定哪天就不支持了,而自己从 github 同步到国内的 gitee 或者自己私有的 gitlab 源再进行安装,相对来说操作步骤多且繁琐。

综合来看,推荐使用第二种方法。

3、zsh-autosuggestions 安装

zsh-autosuggestions 是一个 zsh 的插件,推荐安装,不用必须安装。该插件的作用就是按照你的输入字符自动提示最近执行的命令,可以减少部分重复命令的输入时间。

如果你不需要这个插件,直接跳过该步骤进入第 3 步即可。

(1) 使用 git 下载 zsh-autosuggestions 插件到 ~/.oh-my-zsh/plugins 目录

git clone git://github.com/zsh-users/zsh-autosuggestions 

示例:

cd ~/.oh-my-zsh/plugins; git clone git://github.com/zsh-users/zsh-autosuggestions
git clone git://github.com/zsh-users/zsh-autosuggestions
Cloning into 'zsh-autosuggestions'...
remote: Enumerating objects: 11, done.
remote: Counting objects: 100% (11/11), done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 2396 (delta 2), reused 3 (delta 1), pack-reused 2385
Receiving objects: 100% (2396/2396), 545.28 KiB | 10.00 KiB/s, done.
Resolving deltas: 100% (1533/1533), done.

注:此处需要 MacOS 上已经安装 git,如果没有安装,请先安装 git

(2) 配置 zsh-autosuggestions 插件

编辑 vi ~/.zshrc 文件,找到 plugins=(xxx) 这一行,添加 zsh-autosuggestions,如下示例

plugins=(git zsh-autosuggestions)

(3) 使用配置生效

source ~/.zshrc

3、将 shell 设置为 zsh,使用如下命令切换

正常情况下,安装成功 oh-my-zsh 后,会自动将 shell 切换到 zsh。

如果你在安装 oh-my-zsh 的时候没有选择切换 shell 到 zsh,那么你需要执行如下命令来切换。

chsh -s /bin/zsh

总结

1、安装校验

打开终端,看见 -> ~ 的符号表示 zsh 已经生效了,输入 git,发现自动提示了上一次使用 git 的命令,zsh-autosuggestions 也已经生效。

2、卸载 oh-my-zsh

执行如下命令即可进行卸载。

uninstall_oh_my_zsh

如果执行上面的命令报错找不到 uninstall_oh_my_zsh,那么请你确保已经 source ~/.zshrc

附录

oh my zsh 官网:

https://ohmyz.sh/

oh my zsh github 地址

https://github.com/ohmyzsh/ohmyzsh

You may also like...

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注