macOS、Linux安装Zsh和Oh My Zsh教程

macOS、Linux安装Zsh和Oh My Zsh教程

前言

Linux系统提供多种不同的Shell以供选择。常用的有 Bourne Shell(简称sh)、C-Shell(简称csh)、Korn Shell(简称ksh)和 Bourne Again Shell (简称 bash )等。对于经常使用终端的用户,我推荐大家都安装Zsh,因为体验更好。

Zsh

什么是Zsh

Zsh官网是这么介绍的

Zsh is a shell designed for interactive use, although it is also a powerful scripting language. More information can be found on the “Zsh Web Pages” sites.
Zsh是一种为交互使用而设计的shell,尽管它也是一种强大的脚本语言。更多信息可在“Zsh网页”网站上找到。

Zsh Web Pages是这么介绍的

Zsh is a shell designed for interactive use, although it is also a powerful scripting language. Many of the useful features of bash, ksh, and tcsh were incorporated into Zsh; many original features were added. The introductory document details some of the unique features of Zsh. It assumes basic knowledge of the standard UNIX shells; the intent is to show a reader already familiar with one of the other major shells what makes Zsh more useful or more powerful. This document is not at all comprehensive; read the manual entry for a description of the shell that is complete, concise and up-to-date, although somewhat overwhelming and devoid of examples. Alternatively, the user guide offers wordy explanations of many of the shell’s features. If you want to read something that has been polished by a professional editor you can buy the book.
Zsh是一种为交互使用而设计的shell,尽管它也是一种功能强大的脚本语言。bash、ksh和tcsh的许多有用特性都被合并到Zsh中;添加了许多原始的特性。介绍性文档详细介绍了Zsh的一些独特特性。它假定您具有标准UNIX shell的基本知识;其目的是向已经熟悉其他主要shell之一的读者展示使Zsh更有用或更强大的原因。这份文件一点也不全面;阅读手册条目,了解完整、简洁和最新的shell描述

Zsh也称为Z shell,是Bourne Shell(sh)的扩展版本,具有大量新功能,并支持插件和主题由于它基于与Bash相同的shell,因此Zsh具有许多相同的功能,并且切换是轻而易举的。

Zsh主要功能

  • 自动cd:只需输入目录的名称即可
  • 递归路径扩展:例如“/ u / lo / b”扩展为“/ usr / local / bin”
  • 拼写更正和近似完成:如果您输入目录名称时出现轻微错误,Zsh将为您解决此问题
  • 插件和主题支持: Zsh包含许多不同的插件框架

Linux安装Zsh

  • 查看下剩余空间
    1
    2
    3
    4
    5
    6
    7
    8
    [admin@Main ~]$ df -h
    Filesystem Size Used Avail Use% Mounted on
    /dev/vda1 40G 29G 8.5G 78% /
    devtmpfs 1.9G 0 1.9G 0% /dev
    tmpfs 1.9G 0 1.9G 0% /dev/shm
    tmpfs 1.9G 185M 1.7G 10% /run
    tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
    tmpfs 380M 0 380M 0% /run/user/1000
  • 查看Linux信息
    1
    2
    3
    4
    5
    6
    [admin@Main ~]$ lsb_release -a
    LSB Version: :core-4.1-amd64:core-4.1-noarch
    Distributor ID: CentOS
    Description: CentOS Linux release 7.0.1406 (Core)
    Release: 7.0.1406
    Codename: Core
  • 查看当前使用的Shell
    1
    2
    [admin@Main ~]$ echo $SHELL
    /bin/bash
  • 查看安装的Shell
    1
    2
    3
    4
    5
    6
    7
    8
    9
    [admin@Main ~]$ cat /etc/shells 
    /bin/sh
    /bin/bash
    /sbin/nologin
    /usr/bin/sh
    /usr/bin/bash
    /usr/sbin/nologin
    /bin/tcsh
    /bin/csh
    可以看到CentOS7默认没有安装Zsh
  • 安装Zsh
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    [admin@Main ~]$ sudo yum install zsh
    [sudo] password for admin:
    Loaded plugins: langpacks
    Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
    base | 3.6 kB 00:00:00
    epel | 4.7 kB 00:00:00
    extras | 2.9 kB 00:00:00
    https://repo.mongodb.org/yum/redhat/7/mongodb-org/3.4/x86_64/repodata/repomd.xml: [Errno 12] Timeout on https://repo.mongodb.org/yum/redhat/7/mongodb-org/3.4/x86_64/repodata/repomd.xml: (28, 'Operation timed out after 3024 milliseconds with 0 out of 0 bytes received')
    Trying other mirror.
    mongodb-org-3.4 | 2.5 kB 00:00:00
    updates | 2.9 kB 00:00:00
    (1/8): epel/x86_64/group_gz | 95 kB 00:00:00
    (2/8): epel/x86_64/updateinfo | 1.0 MB 00:00:00
    (3/8): base/7/x86_64/group_gz | 153 kB 00:00:00
    (4/8): base/7/x86_64/primary_db | 6.1 MB 00:00:00
    (5/8): epel/x86_64/primary_db | 6.9 MB 00:00:00
    (6/8): extras/7/x86_64/primary_db | 206 kB 00:00:00
    (7/8): updates/7/x86_64/primary_db | 3.8 MB 00:00:00
    (8/8): mongodb-org-3.4/7/primary_db | 99 kB 00:00:02
    Resolving Dependencies
    --> Running transaction check
    ---> Package zsh.x86_64 0:5.0.2-34.el7_8.2 will be installed
    --> Finished Dependency Resolution

    Dependencies Resolved

    =========================================================================================================================================================
    Package Arch Version Repository Size
    =========================================================================================================================================================
    Installing:
    zsh x86_64 5.0.2-34.el7_8.2 updates 2.4 M

    Transaction Summary
    =========================================================================================================================================================
    Install 1 Package

    Total download size: 2.4 M
    Installed size: 5.6 M
    Is this ok [y/d/N]: y
    Downloading packages:
    zsh-5.0.2-34.el7_8.2.x86_64.rpm | 2.4 MB 00:00:00
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
    Installing : zsh-5.0.2-34.el7_8.2.x86_64 1/1
    Verifying : zsh-5.0.2-34.el7_8.2.x86_64 1/1

    Installed:
    zsh.x86_64 0:5.0.2-34.el7_8.2

    Complete!
  • 查看Zsh是否已安装
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [admin@Main ~]$ cat /etc/shells
    /bin/sh
    /bin/bash
    /sbin/nologin
    /usr/bin/sh
    /usr/bin/bash
    /usr/sbin/nologin
    /bin/tcsh
    /bin/csh
    /bin/zsh
  • 切换到Zsh
    1
    2
    3
    4
    [admin@Main ~]$ chsh -s /bin/zsh
    Changing shell for admin.
    Password:
    Shell changed.
  • reboot
    1
    2
    3
    4
    5
    6
    7
    8
    [admin@Main ~]$ reboot
    ==== AUTHENTICATING FOR org.freedesktop.login1.reboot ===
    Authentication is required for rebooting the system.
    Authenticating as: root
    Password:
    ==== AUTHENTICATION COMPLETE ===
    Connection xxx closed by remote host.
    Connection to xxx closed.
    重启在进入Linux,提示如下信息:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    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.

    --- Type one of the keys in parentheses ---

按 1 进入主菜单
然后提示如下信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Please pick one of the following options:

(1) Configure settings for history, i.e. command lines remembered
and saved by the shell. (Recommended.)

(2) Configure the new completion system. (Recommended.)

(3) Configure how keys behave when editing command lines. (Recommended.)

(4) Pick some of the more common shell options. These are simple "on"
or "off" switches controlling the shell's features.

(0) Exit, creating a blank ~/.zshrc file.

(a) Abort all settings and start from scratch. Note this will overwrite
any settings from zsh-newuser-install already in the startup file.
It will not alter any of your other settings, however.

(q) Quit and do nothing else. The function will be run again next time.
--- Type one of the keys in parentheses ---

直接输入一个 0 会在 ~ 目录下生成一个空的 .zshrc 配置文件。
然后提示如下信息:

1
2
3
4
5
6
7
8
9
10
11
The function will not be run in future, but you can run
it yourself as follows:
autoload -Uz zsh-newuser-install
zsh-newuser-install -f

The code added to ~/.zshrc is marked by the lines
# Lines configured by zsh-newuser-install
# End of lines configured by zsh-newuser-install
You should not edit anything between these lines if you intend to
run zsh-newuser-install again. You may, however, edit any other part
of the file.
  • 查看是否切换为Zsh
    1
    2
     [admin@Main]~% echo $SHELL
    /bin/zsh

Oh My Zsh

什么是Oh My Zsh

首先看下Oh My Zsh 官网的介绍

Oh My Zsh is an open source, community-driven framework for managing your zsh configuration. Sounds boring. Let’s try again. Oh My Zsh will not make you a 10x developer…but you may feel like one. Once installed, your terminal shell will become the talk of the town or your money back! With each keystroke in your command prompt, you’ll take advantage of the hundreds of powerful plugins and beautiful themes. Strangers will come up to you in cafés and ask you, “that is amazing! are you some sort of genius?” Finally, you’ll begin to get the sort of attention that you have always felt you deserved
Oh My Zsh是一个用于管理Zsh配置的开源、社区驱动的框架。听起来无聊。让我们再试一次。哦,我的Zsh不会让你成为一个10倍的开发人员…但你可能觉得自己就是。一旦安装,您的终端外壳将成为讨论城镇或您的钱回来!在命令提示符中的每一次按键,您将利用数百个强大的插件和美丽的主题。在咖啡馆里,陌生人会走到你面前问你:“太棒了!”你是什么天才吗?”最后,你会开始得到你一直认为自己应该得到的关注

简单来说就是配置很麻烦,Oh My Zsh这个框架开源简化Zsh的配置,并且提供了Plugins、Community、Themes

下图为官网介绍的Oh My Zsh三大功能

安装Oh My Zsh

官方提供了两种安装方式

  • 通过curl
    1
    sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  • 通过weget
    1
    sh -c "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"
    我这里使用curl安装
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    [admin@Main]~% sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
    Cloning Oh My Zsh...
    Cloning into '/home/admin/.oh-my-zsh'...
    remote: Enumerating objects: 1156, done.
    remote: Counting objects: 100% (1156/1156), done.
    remote: Compressing objects: 100% (1124/1124), done.
    remote: Total 1156 (delta 21), reused 1067 (delta 16), pack-reused 0
    Receiving objects: 100% (1156/1156), 775.41 KiB | 16.00 KiB/s, done.
    Resolving deltas: 100% (21/21), done.

    Looking for an existing zsh config...
    Found ~/.zshrc. Backing up to /home/admin/.zshrc.pre-oh-my-zsh
    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
  • 安装前和安装后对比
    这里以ll命令来对比


    很明显能够看出变化了

安装zsh-syntax-highlighting语法高亮插件

  1. Clone this repository in oh-my-zsh’s plugins directory
    1
    git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
  2. Activate the plugin in ~/.zshrc:
    1
    vim ~/.zshrc
    plugins添加zsh-syntax-highlighting
    1
    2
    3
    4
    plugins=(
    git,
    zsh-syntax-highlighting
    )
  3. 执行source ~/.zshrc

相较于纯白字的命令,添加这个插件后,现在我们的命令语法高亮了

安装zsh-autosuggestions历史记录插件

  1. Clone this repository in oh-my-zsh’s plugins directory
    1
    git clone git://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions
  2. Activate the plugin in ~/.zshrc:
    1
    vim ~/.zshrc
    plugins添加zsh-autosuggestions
    1
    2
    3
    4
    5
    plugins=(
    git,
    zsh-syntax-highlighting,
    zsh-autosuggestions
    )
  3. 执行source ~/.zshrc

快看有历史记录提示了,按下右方向键即可自动选中

安装incr自动提示插件

incr官网
incr是一款自动提示插件,功能非常强大,官网演示demo,感受一下

  1. 创建incr目录
    mkdir $ZSH_CUSTOM/plugins/incr
  2. 下载incr
    wget http://mimosa-pudica.net/src/incr-0.2.zsh -O $ZSH_CUSTOM/plugins/incr/incr.plugin.zsh
  3. Activate the plugin in ~/.zshrc:
    1
    vim ~/.zshrc
    plugins添加incr
    1
    2
    3
    4
    5
    6
    plugins=(
    git,
    zsh-syntax-highlighting,
    zsh-autosuggestions,
    incr
    )
  4. 执行source ~/.zshrc

修改主题

默认主题是robbyrussell
我习惯用的主题是ys,接下来我将默认主题修改ys

  1. vim ~/.zshrc
  2. 找到ZSH_THEME="robbyrussell"将robbyrussell修改为ys
  3. 执行source ~/.zshrc

主题效果图如下:

错误

insecure directories, run compaudit for list.

  • 当ssh连接到Linux时
  • 当执行source ~/.zshrc

会报如下错误提示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[oh-my-zsh] Insecure completion-dependent directories detected:
drwxrwxr-x 2 admin admin 4096 Aug 13 13:11 /home/admin/.oh-my-zsh/custom/plugins/incr
drwxrwxr-x 7 admin admin 4096 Aug 13 12:56 /home/admin/.oh-my-zsh/custom/plugins/zsh-autosuggestions
drwxrwxr-x 7 admin admin 4096 Aug 13 12:37 /home/admin/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting

[oh-my-zsh] For safety, we will not load completions from these directories until
[oh-my-zsh] you fix their permissions and ownership and restart zsh.
[oh-my-zsh] See the above list for directories with group or other writability.

[oh-my-zsh] To fix your permissions you can do so by disabling
[oh-my-zsh] the write permission of "group" and "others" and making sure that the
[oh-my-zsh] owner of these directories is either root or your current user.
[oh-my-zsh] The following command may help:
[oh-my-zsh] compaudit | xargs chmod g-w,o-w

[oh-my-zsh] If the above didn't help or you want to skip the verification of
[oh-my-zsh] insecure directories you can set the variable ZSH_DISABLE_COMPFIX to
[oh-my-zsh] "true" before oh-my-zsh is sourced in your zshrc file.

zsh compinit: insecure directories, run compaudit for list.
Ignore insecure directories and continue [y] or abort compinit [n]? y%

有两种解决方法

方法一,修改目录权限

  • 使用compaudit命令查看被标记为不安全的文件夹

  • 给不安全的目录添加权限
    1
    chmod 755 /home/admin/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting

    方法二,跳过不安全目录的验证

  • 在.zshrc文件的第一行添加
    1
    ZSH_DISABLE_COMPFIX=true
  • 执行source ~/.zshrc

macOS、Linux安装Zsh和Oh My Zsh教程

https://ganzhixiong.com/p/9dad8f4e/

Author

干志雄

Posted on

2019-05-10

Updated on

2019-05-10

Licensed under

Comments