I have been using ZSH for a long time. I have been admiring the power and convenience of ZSH. For a long time, I planned to record the process of installing and using ZSH. -)

What is ZSH?

The Z Shell(Zsh) is a Unix Shell that can be used as an interactive login Shell and a powerful Shell script command interpreter. Zsh can be thought of as an extension of the Bourne shell with a large number of improvements, including some bash, KSH, and TCSH features.

In short, it is an extension and enhancement of the shell scripting language.

What functions does ZSH have?

From Wiki

  • Out of the box, programmable command-line completion helps users enter various parameters and options.
  • Command history is shared across all shells started by the user.
  • By extending the file wildcard, it is possible to expand the find file name without using an external command.
  • Improved variable and array handling.
  • Edits multi-line commands in a buffer.
  • Multiple compatibility modes, such as the /bin/sh runtime can masquerading as the Bourne shell.
  • Prompt that can be customized for presentation; Includes displaying information on the right side of the screen and automatically hiding it when typing long commands.
  • Loadable modules that provide a variety of other support: complete TCP and Unix domain socket control, FTP client and extended mathematical functions.
  • Fully customizable.

Installation and use of ZSH

The following examples are all based on centOS 7.4, similar to macOS

Universal Yum install:

sudo yum update && sudo yum -y install zsh
Copy the code

After the installation is successful, enter ZSH –version to check whether the installation is successful. The following output is displayed (for different versions and platforms) :

ZSH 5.0.2 (x86_64 - redhat - Linux - gnu)Copy the code

After ZSH is installed successfully, we can switch the default shell to ZSH. Before doing this, we can confirm what shell scripts are already installed:

~ cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/zsh
Copy the code

You can clearly view the list of shell scripts that have been installed. Of course, there are many ways to verify this, such as printing shell environment variables directly:

~ echo $SHELL
/bin/zsh
Copy the code

Switch the default shell to ZSH:

~ chsh -s $(which zsh)
Changing shell for root.
chsh: Warning: "/usr/bin/zsh" is not listed in /etc/shells.
Shell changed.
Copy the code

Once ZSH was installed, it wasn’t quite as powerful as we described earlier, and we needed a cool tool Oh My ZSH to manage and extend our ZSH

Oh My Zsh

The website address

Description on the official website: It comes bundled with a ton of helpful functions, helpers, plugins, themes, and a few things that make you shout…

Support for numerous extension functions, plug-ins, themes, etc. I have made a list of specific uses according to my usage habits:

Plugins

  • Git: powerful git abbreviation command (enabled by default)
  • Zsh-autosuggestions: Shell command prompts for automatic completion of possible paths
  • ZSH -syntax-highlighting: Special command highlighting

Themes

  • Robbyrussell: Default theme, very nice
  • af-magic
  • Agnoster: Dark theme
  • Avit: Clean interface

  • Alias: a unique hyperlink alias. How to use it later

The installation

1, the git clone https://github.com/robbyrussell/oh-my-zsh.git ~ /. Oh - my - ZSH 2, cp ~ /. ZSHRC ~ /. ZSHRC. 3, cp orig ~/.oh-my-zsh/templates/zshrc. ZSH -template ~/.zshrc 4, CHSH -s /bin/zshCopy the code

After the installation is successful, open a new terminal. If the theme is changed, the installation is successful and the configuration has taken effect.

To ensure the validity of the configuration, you can enter a simple command:

➜ ~ la total usage 100k-rw ------- 1 root root 852 July 1 18:02. bash_history-rw-r --r-- 1 root root 18 December 29 2013. bash_logout Bash_profile-rw-r --r-- 1 root root 176 December 29 2013. bashrc DRWX ------ 3 root Root 4.0k 10月 15 2017. cache DRWX ------ 3 root root 4.0k 7月 2 22:30. config -rw-r--r--. 1 root root 100 12月 29 2013 .cshrc -rw------- 1 root root 0 7月 2 22:29. node_repl_history drwxr-xr-x 11 root root 4.0k 7月 1 18:04.oh-my-zsh Drwxr-xr-x 2 root root 4.0k 10月 15 2017. PIP DRWXR ----- 3 root root 4.0k 7月 1 18:04. Pki-rw-r --r-- 1 root root 64 10月 CFG DRWX ------ 2 root root 4.0k 7月 2 22:28. ssh-rw-r --r--. 1 root root 129 12月 29 2013. TCSHRC -rw------- 1 root root 3.3k 7月 7 23:22. viminfo -rw-r--r-- 1 root root 36K 7月 1 18:07 .zcompdump-iz2zea8scx4z0lcmstkb0rz-5.0.2 -rw------- 1 root root 2.6k 7月 9 09:18. zsh_history -rw-r--r-- 1 root root 3.1k 7月 1 18:07. ZSHRC -rw-r--r-- 1 root root 0 July 1 18:07. zshrc.origCopy the code

If la replaces ls -a, congratulations on your installation.

Topic selection

As for the choice of theme, the default theme robbyrussell is enough for me. If I have more requirements and want to change the theme, I can choose the corresponding theme to switch from here.

Here’s how to switch themes:

Open the configuration file ➜ ~ vim ~/. ZSHRC 2. Locate the row that contains the theme configuration and switch to the corresponding theme ZSH_THEME="robbyrussell" 3. Source ~/. ZSHRC Enables the configuration file to take effect and opens a new terminalCopy the code

Alias Indicates the use of an alias

In the.zshrc configuration file, there are some examples of aliases set by default:

# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
Copy the code

What does that mean? Easy to understand, alias our command (abbreviated)

Example: When we want to jump to a blog directory under the /home/richardwei/blog path, we usually write: CD /home/richardwei/blog, if this command is used frequently, then it would be tedious to hit it or find it every time. Now let’s do this:

➜ ~ vim ~/.zshrc # 2. On the next line add: Alias myblog=" CD /home/richardwei/blog" # note that the equal can not have a space # 3. :wq save and exit # 4 Type this command to enter the corresponding blog directoryCopy the code

For some commonly used commands that are tedious and fixed, you can use the alias alias to replace them, which can effectively improve our work efficiency.

Plug-in selection and configuration

Plug-in configuration:

1. Open the configuration file ➜ ~ vim ~/. ZSHRC 2. If you need to add a plugin, just: plugins=(git zsh-autosuggestions)Copy the code

git

Use abbreviations instead of complex Git commands

Git plug-in already configured by default to open, we can through the cat ~ /. Oh – my – ZSH/plugins/git/git plugin. The ZSH command to view configured command (alias), is also one of the most commonly used functions

Ex. : Git checkout -b feature/test => gco -b feature/test => gco -b feature/test => gco -b feature/test => gco -b feature/test Origin test => gpsup Add all file changes to workspace git add. => ga. Git log –graph –pretty=’%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold) Blue)<%an>%Creset’ –all => glola… .Is the bane of long, hard to remember commands

zsh-autosuggestions

Efficiency artifact, command automatic inference and completion, super easy to use

Installation:

This section describes only the installation process in oH-my-zsh

  1. Clone code to local oh-my-zsh plugin directory

    git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
  2. Add to the configuration file vim ~/.zshrc

    plugins=(zsh-autosuggestions)
  3. Source ~/. ZSHRC The new terminal takes effect

If we had typed CD ~/.oh-my-zsh, the next time we typed CD ~, it would automatically suggest directories we might need to go to, etc. Very useful

zsh-syntax-highlightingCopy the code

Shell command highlighting prompt

Website Demo:

Before:

After: 

Before:

After: 

Before:

After: 

The installation

  1. Clone code locally

    git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
  2. Edit the configuration

    plugins=(... zsh-syntax-highlighting)
  3. Source ~/. ZSHRC The new terminal takes effect