ZSH + Oh My ZSH! on Windows without WSL
Tags: productivity, zsh, git, terminal
Published: 2022-10-11 17:54:49

Well, WSL it's a good way to have a _Unixish_ environment on Windows, but sadly his performance and memory consumption leads me to find another alternative... and I found it in a place closer than I expected.

## 1. Bash

The first step is to download and install [git](https://git-scm.com/download/win) with bash terminal support.

## 2. ZSH

At least Bash is better than the windows CMD, but ZSH is in another league with his productivity boosts. So we are going to install ZSH over Bash.

### Download ZSH

Download the latest MSYS2 zsh package from the [MSYS2 package repository](https://packages.msys2.org/package/zsh?repo=msys&variant=x86_64). The file will be named something along the lines of `zsh-#.#-#-x86_64.pkg.tar.zst`

> `zst` files can be decompressed with [PeaZip](https://peazip.github.io/)

### Install

Extract the contents of the archive (which should include `etc` and `usr` folders) into your Git Bash installation directory. This is likely to be under `C:\Program Files\Git`. Merge the contents of the folder if asked (no files should be getting overridden).

### Setup

Open the Git Bash terminal and run `zsh` command, then verify the installed version.

```bash
zsh --version
zsh 5.9 (x86_64-pc-msys)
```

Configure `zsh` as the default shell by appending the following to your `~/.bashrc` file:

```
if [ -t 1 ]; then
  exec zsh
fi
```

> Windows can mangle some UTF-8 encoded text, causing unexpected characters to be displayed in your terminal. To fix this, add the following to your `~/.bashrc` file, ideally, before the code that sets your shell as zsh:

```
/c/Windows/System32/chcp.com 65001 > /dev/null 2>&1
```

Close and reopen the terminal, to update those changes. The first time will ask for some files creation, choose:

```
Quit and do nothing.  The function will be run again next time.
```

## 3. Oh my zsh!

### Install

Add superpowers to `zsh` installing **Oh my zsh!** running this command.

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

### Fonts

Download and install manually the [Meslo Nerd Fonts](https://github.com/romkatv/powerlevel10k#meslo-nerd-font-patched-for-powerlevel10k) to include all glyphs and symbols that Powerlevel10k may need

### Theme

There are a lot of themes but my favorite is [Powerlevel10k](https://github.com/romkatv/powerlevel10k) because is easy to set up and use.

```bash
git clone https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/themes/powerlevel10k
```

On the `~/.zshrc` file add this additional configuration

```bash
ZSH_THEME="powerlevel10k/powerlevel10k"
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(history)
POWERLEVEL9K_SHORTEN_DIR_LENGTH=1

# User configuration

export LS_COLORS="rs=0:no=00:mi=00:mh=00:ln=01;36:or=01;31:di=01;34:ow=04;01;34:st=34:tw=04;34:pi=01;33:so=01;33:do=01;33:bd=01;33:cd=01;33:su=01;35:sg=01;35:ca=01;35:ex=01;32:"
```

Restart the terminal and type `p10k configure`.

![Wizard](https://dev-to-uploads.s3.amazonaws.com/i/mtrv39dhe0f0b5pe2shs.gif)

> ⚠ if you can't see the icon symbols and always show an ▯, cancel the process, and refer to step 4 before continue. 

### Plugins 

**Oh My zsh!** have [a lot of plugins](https://github.com/ohmyzsh/ohmyzsh/wiki/Plugins) to use. It's recommended to explore the options and use what is good for your needs.

I've already installed a lot related to software development and other ones to add more functionalities. Running these commands:

```bash
git clone https://github.com/zsh-users/zsh-autosuggestions.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
git clone https://github.com/Pilaton/OhMyZsh-full-autoupdate.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/ohmyzsh-full-autoupdate
```

And now edit the `~/.zshrc` file and add it inside the `plugins` property (don't use commas as separators)

```bash
plugins=(
    adb
    command-not-found
    extract
    deno
    docker
    git
    github
    gitignore
    history-substring-search
    node
    npm
    nvm
    yarn
    volta
    vscode
    sudo
    web-search
    z
    zsh-autosuggestions
    zsh-syntax-highlighting
    ohmyzsh-full-autoupdate
)

# User configuration

ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern cursor root line)
ZSH_HIGHLIGHT_PATTERNS=('rm -rf *' 'fg=white,bold,bg=red')
```

> If you are using NVM take care of following this [configuration to avoid slowing the zsh start-up](https://lakur.tech/2021/12/10/fix-slow-zsh-startup-nvm/) and this [configuration to speed up the compinit](https://gist.github.com/ctechols/ca1035271ad134841284?permalink_comment_id=3365691)

⚠ If you have done previous package configurations, alias definitions, or something else on your `~/.basrc` previous ZSH installation you need to move those manually to `~/.zshrc`.

## 4. Terminals

To use the same terminal inside VSCode and Windows Terminal follow these configurations.

### VS Code

Add these properties to the user `setttings.json`

```diff
{ 
    ...
+   "terminal.integrated.fontFamily": "MesloLGS NF",
+   "terminal.integrated.fontSize": 12,
+   "terminal.integrated.shellIntegration.enabled": true,
+   "terminal.integrated.defaultProfile.windows": "Git Bash",
    ...
}
```

### Microsoft Terminal

Add these configurations on the Git Bash terminal.

```diff
{
    "profiles": {
        "defaults": {},
        "list": [
            {
+               "font": {
+                   "face": "MesloLGS NF",
+                   "size": 12
+               },
                "guid": "{2ece5bfe-50ed-5f3a-ab87-5cd4baafed2b}",
                "hidden": false,
                "name": "Git Bash",
                "source": "Git",
+               "startingDirectory": "D:\\Developer"
            },
        ]
    }
}
```

--- 

## Downsides

- Git Bash **is an emulation layer**, so It's not as fast as I expected, but much faster than WSL (and also not dealing with his memory and performance issues)
- Take some time to load the first time (but, less than WSL).
- Volta on Windows requires additional permissions, maybe you gonna need to come back to NVM
- Strange behavior while writing inside VS Code terminal
- Can't use paths saved on `%VARIABLE%`, you are going to need to translate it with `cygpath $LOCALAPPDATA`. To change to a path use something like this `$(cygpath $LOCALAPPDATA)/Volta/tools/image/node/`

If Git Bash is not what you expect, you can also come back to WSL and use ZSH + Oh My ZSH!

{% post https://dev.to/equiman/zsh-on-windows-with-wsl-1jck %}

---

## Sources

- [Installing Zsh (and oh-my-zsh) in Windows Git Bash](https://dominikrys.com/posts/zsh-in-git-bash-on-windows/)
- [Speeding Up My Shell (Oh My Zsh)](https://blog.mattclemente.com/2020/06/26/oh-my-zsh-slow-to-load/)

---

**That’s All Folks!
Happy Coding** 🖖

[![beer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mkgsx4aw6qn9i9ax6as9.png)](https://github.com/sponsors/deinsoftware)