Feature-rich Syntax Highlighting For Zsh
Tags: programming, productivity, opensource, zsh
Published: 2022-01-13 21:56:50

<h2 align="center">
  <a href="https://github.com/z-shell/zi">
  <img src="https://raw.githubusercontent.com/z-shell/zi/main/docs/images/logo.png" alt="Logo" width="60" height="60"></a>
</h2>
<div align="center"><h2>
❮ ZI ❯ Feature-rich Syntax Highlighting
</h2> 

For more please see:

- https://z.digitalclouds.dev
- https://github.com/z-shell
- https://github.com/z-shell/F-Sy-H

---

- [Installation](#installation)
  - [Manual](#manual)
  - [ZI](#zi)
- [Features](#features)
  - [Themes](#themes)
  - [Variables](#variables)
  - [Brackets](#brackets)
  - [Conditions](#conditions)
  - [Strings](#strings)
  - [here-strings](#here-strings)
  - [`exec` descriptor-variables](#exec-descriptor-variables)
  - [for-loops and alternate syntax (brace `{`/`}` blocks)](#for-loops-and-alternate-syntax-brace--blocks)
  - [Function definitions](#function-definitions)
  - [Recursive `eval` and `$( )` highlighting](#recursive-eval-and---highlighting)
  - [Chroma functions](#chroma-functions)
  - [Math-mode highlighting](#math-mode-highlighting)
  - [Zcalc highlighting](#zcalc-highlighting)
- [Performance](#performance)

## Installation

Post install try: `fast-theme -t z-shell`. To enable: `fast-theme z-shell`.

### Manual

Clone the Repository.

```shell
git clone https://github.com/z-shell/F-Sy-H ~/path/to/fsh
```

And add the following to your `zshrc` file.

```shell
source ~/path/to/fsh/F-Sy-H.plugin.zsh
```

### [ZI](https://github.com/z-shell/zi)

Add the following to your `zshrc` file.

```shell
zi light z-shell/F-Sy-H
```

Here's an example of how to load the plugin together with a few other popular ones with the use of [Turbo mode](https://z-shell.pages.dev/docs/getting_started/overview#turbo-mode-zsh--53),
i.e.: speeding up the Zsh startup by loading the plugin right after the first
prompt, in background:

```shell
zi wait lucid for \
 atinit"ZI[COMPINIT_OPTS]=-C; zicompinit; zicdreplay" \
    z-shell/F-Sy-H \
 blockf \
    zsh-users/zsh-completions \
 atload"!_zsh_autosuggest_start" \
    zsh-users/zsh-autosuggestions
```

## Features

### Themes

Switch themes via `fast-theme {theme-name}`.

![image1](https://github.com/z-shell/F-Sy-H/raw/main/docs/images/theme.png)

Run `fast-theme -t {theme-name}` option to obtain the snippet above.

Run `fast-theme -l` to list available themes.

### Variables

Comparing to the project `zsh-users/zsh-syntax-highlighting` (the upper line):

<img src="https://github.com/z-shell/F-Sy-H/raw/main/docs/images/parameter.png" alt="image could not be loaded" style="color:red;background-color:black;font-weight:bold" />
</div>

<div style="width:100%;background-color:black;border:3px solid black;border-radius:6px;margin:5px 0;padding:2px 5px">
  <img
    src="https://github.com/z-shell/F-Sy-H/raw/main/docs/images/in_string.png"
    alt="image could not be loaded"
    style="color:red;background-color:black;font-weight:bold"
  />
</div>

### Brackets

<div style="width:100%;background-color:black;border:3px solid black;border-radius:6px;margin:5px 0;padding:2px 5px">
  <img
    src="https://github.com/z-shell/F-Sy-H/raw/main/docs/images/brackets.gif"
    alt="image could not be loaded"
    style="color:red;background-color:black;font-weight:bold"
  />
</div>

### Conditions

Comparing to the project `zsh-users/zsh-syntax-highlighting` (the upper line):

<div style="width:100%;background-color:black;border:3px solid black;border-radius:6px;margin:5px 0;padding:2px 5px">
  <img
    src="https://github.com/z-shell/F-Sy-H/raw/main/docs/images/cplx_cond.png"
    alt="image could not be loaded"
    style="color:red;background-color:black;font-weight:bold"
  />
</div>

### Strings

Exact highlighting that recognizes quotings.

<div style="width:100%;background-color:black;border:3px solid black;border-radius:6px;margin:5px 0;padding:2px 5px">
  <img
    src="https://github.com/z-shell/F-Sy-H/raw/main/docs/images/ideal-string.png"
    alt="image could not be loaded"
    style="color:red;background-color:black;font-weight:bold"
  />
</div>

### here-strings

<div style="width:100%;background-color:black;border:3px solid black;border-radius:6px;margin:5px 0;padding:2px 5px">
  <img
    src="https://github.com/z-shell/F-Sy-H/raw/main/docs/images/herestring.png"
    alt="image could not be loaded"
    style="color:red;background-color:black;font-weight:bold"
  />
</div>

### `exec` descriptor-variables

Comparing to the project `zsh-users/zsh-syntax-highlighting` (the upper line):

<div style="width:100%;background-color:black;border:3px solid black;border-radius:6px;margin:5px 0;padding:2px 5px">
  <img
    src="https://github.com/z-shell/F-Sy-H/raw/main/docs/images/execfd_cmp.png"
    alt="image could not be loaded"
    style="color:red;background-color:black;font-weight:bold"
  />
</div>

### for-loops and alternate syntax (brace `{`/`}` blocks)

<div style="width:100%;background-color:black;border:3px solid black;border-radius:6px;margin:5px 0;padding:2px 5px">
  <img
    src="https://github.com/z-shell/F-Sy-H/raw/main/docs/images/for-loop-cmp.png"
    alt="image could not be loaded"
    style="color:red;background-color:black;font-weight:bold"
  />
</div>

### Function definitions

Comparing to the project `zsh-users/zsh-syntax-highlighting` (the upper 2 lines):

<div style="width:100%;background-color:black;border:3px solid black;border-radius:6px;margin:5px 0;padding:2px 5px">
  <img
    src="https://github.com/z-shell/F-Sy-H/raw/main/docs/images/function.png"
    alt="image could not be loaded"
    style="color:red;background-color:black;font-weight:bold"
  />
</div>

### Recursive `eval` and `$( )` highlighting

Comparing to the project `zsh-users/zsh-syntax-highlighting` (the upper line):

<div style="width:100%;background-color:black;border:3px solid black;border-radius:6px;margin:5px 0;padding:2px 5px">
  <img
    src="https://github.com/z-shell/F-Sy-H/raw/main/docs/images/eval_cmp.png"
    alt="image could not be loaded"
    style="color:red;background-color:black;font-weight:bold"
  />
</div>

### Chroma functions

Highlighting that is specific for a given command.

<div style="width:100%;background-color:black;border:3px solid black;border-radius:6px;margin:5px 0;padding:2px 5px">
  <img
    src="https://github.com/z-shell/F-Sy-H/raw/main/docs/images/git_chroma.png"
    alt="image could not be loaded"
    style="color:red;background-color:black;font-weight:bold"
  />
</div>

The [chromas](https://github.com/z-shell/F-Sy-H/tree/main/chroma)
that are enabled by default can be found
[here](https://github.com/z-shell/F-Sy-H/blob/main/fast-highlight#L166).

### Math-mode highlighting

<div style="width:100%;background-color:black;border:3px solid black;border-radius:6px;margin:5px 0;padding:2px 5px">
  <img
    src="https://github.com/z-shell/F-Sy-H/raw/main/docs/images/math.gif"
    alt="image could not be loaded"
    style="color:red;background-color:black;font-weight:bold"
  />
</div>

### Zcalc highlighting

<div style="width:100%;background-color:black;border:3px solid black;border-radius:6px;margin:5px 0;padding:2px 5px">
  <img src="https://github.com/z-shell/F-Sy-H/raw/main/docs/images/zcalc.png" alt="image could not be loaded" style="color:red;background-color:black;font-weight:bold"/>
</div>

## Performance

Performance differences can be observed in this Asciinema recording, where a `10 kB` function is being edited.

<div style="width:100%;background-color:#121314;border:3px solid #121314;border-radius:6px;margin:5px 0;padding:2px 5px">
  <a href="https://asciinema.org/a/112367">
    <img src="https://asciinema.org/a/112367.png" alt="asciicast">
  </a>
</div>