[TOC] #### 1. 前言 --- ##### Oh My Zsh 介绍 [Oh My Zsh](https://ohmyz.sh) 是一个开源的、社区驱动的框架,用于管理 `zsh`配置,它让终端使用变得更加简单、美观且功能强大 简单来说,如何你使用的是 MacOS 或 Linux,并且想让命令行界面更好看、更智能、更易用,它是最流行的选择之一 它是基于 `zsh` 命令行的一个扩展工具集,提供了丰富的扩展功能,核心功能: + 开箱即用的精美主题:内置了超过 150 种主题,让终端提示符变得色彩丰富(显示当前目录、git 分支状态等) + 强大的插件系统:拥有数百个社区贡献的插件,可以极大的扩展 Zsh 的功能 + 便捷的配置管理:所有的配置都集中在一个文件中(~/.zshrc),修改 plugins 和 theme 即可轻松切换主题和插件 + 自动检测并提示更新:确保用户始终拥有最新的功能和修复 ##### 为什么它这么流行 ? + 降低门槛:Zsh 本身功能极其强大,但原生配置非常复杂,Oh My Zsh 将最佳实践封装好,让新手也能有很好的体验 + 生态活跃:由于用户基数巨大,几乎任何开发工具都能找到对应的 Oh My Zsh 插件 + 美观性:对于需要长时间面对的终端的开发者,一个漂亮的界面能显著提升心情和效率 #### 2. Shell 类型修改为 Zsh --- 因为 Oh My Zsh 是用于管理 `Zsh` 配置的框架,所以在安装之前要先确认当前终端的 `Shell` 类型是否为 `Zsh` + 常见的 Linux 发行版系统终端 Shell 类型几乎都是 Bash,如:Centos、Ubuntu + 目前新版本的 MacOS 终端 Shell 类型是 Zsh,而之前的旧版本默认是 Bash ```bash # /bin/bash 代表是 bash 终端 echo $SHELL ``` 如果当前终端的 `Shell` 类型不是 `Zsh`,那么运行以下命令查找 `Zsh`,判读是否已经安装 `Zsh` ```bash # 有输出内容,比如:/usr/bin/zsh 代表已经安装 zsh # 没有输出内容,代表没有安装 zsh,需要先安装 zsh,再执行下一步 which zsh ``` 不同操作系统安装 `zsh` 的方式也不同,下面列举几种常见系统的安装方式: ```bash # MacOS 可以通过 homebrew 安装 Zsh brew install zsh # Ubuntu 可以通过 apt 包管理器安装 Zsh sudo apt update sudo apt install zsh -y ``` `Zsh` 安装成功后,运行以下命令,将终端的 `Shell` 类型修改为 `Zsh`: ```bash chsh -s $(which zsh) ``` 将终端的 `Shell` 类型修改后,重新打开终端窗口会有一个交互式操作(按下 `q` 键即可) + 打开新的终端窗口发现没有 zsh 的相关配置文件(.zshrc)会出现这个提示 + 我们不需要这里创建 .zshrc 文件,安装 Oh My Zsh 时会自动生成这个文件的 ```plaintext This is the Z Shell configuration function for new users, zsh-newuser-install. You are seeing this message because you have no zsh startup files (the files .zshenv, .zprofile, .zshrc, .zlogin in the directory ~). This function can help you with a few settings that should make your use of the shell easier. You can: (q) Quit and do nothing. The function will be run again next time. (0) Exit, creating the file ~/.zshrc containing just a comment. That will prevent this function being run again. (1) Continue to the main menu. (2) Populate your ~/.zshrc with the configuration recommended by the system administrator and exit (you will need to edit the file by hand, if so desired). --- Type one of the keys in parentheses --- ``` #### 3. Oh My Zsh 安装 --- Oh My Zsh 的安装非常简单,只需要在终端运行一行命令(前提:终端的 `Shell` 类型为 `Zsh`) 使用 `curl` 命令安装: ```bash sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" ``` 使用 `wget` 命令安装: ```bash sh -c "$(wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)" ``` 安装成功后,自动创建 `~/.zshrc` 文件,并且文件中有以下几行默认内容(其它都是注释说明): + ZSH_THEME 终端主题配置 + plugins 插件配置(多个插件使用空格分隔) ```plaintext export ZSH="$HOME/.oh-my-zsh" ZSH_THEME="robbyrussell" plugins=(git) source $ZSH/oh-my-zsh.sh ``` #### 4. Oh My Zsh 命令 --- 查看版本,输出的是 omz 仓库的 commit id ```bash $ omz version master (c8e600f) ``` 主题相关 ```bash # 查看所有主题 omz theme list # 设置主题,当前窗口生效 omz theme use <theme> # 设置主题,全局生效,会修改 .zshrc 文件 omz theme set <theme> ``` 插件相关 ```bash # 查看所有内置插件 omz plugin list ``` 更新 omz 版本 ```bash omz update ``` 重新加载当前 Zsh 会话 ```bash omz reload ``` #### 5. Oh My Zsh 变量 --- ```bash # omz 安装目录路径 echo $ZSH # 个性化配置目录路径 echo $ZSH_CUSTOM # 查看当前使用的主题 echo $ZSH_THEME ```