DEV Community

Shrihan
Shrihan

Posted on • Updated on

Make your terminal look Swag with MSYS2 and ZSH

Yeah, I know, I know; you can use Bash on Ubuntu on Windows via WSL, but this article is specially for users using Windows 8.1 and below (and also for enterprise/LTSB Users)

Does your terminal look, boring, ugly and slow? Well, after reading this, your terminal will look like this:

Demo

Pretty cool, right?

Let’s begin!

First things first, make sure you have:

  • a good understanding of Computer Basics, obviously ;)
  • a good understanding of Terminal (Bash recommended) Basics, again obviously :)
  • a decent internet connection.

Installing Cmder

First of all, head over to this website to download and install Cmder, a console emulator. You can install the Mini version. No problems.

Open Cmder, you must see this screen:

Cmder first-install screen

This may look different, but the overall interface must be the same.

Let’s customize it, as it looks pitch black!

Press Win+Alt+P, to bring the settings option. Download the MesloLGS NF font from here.

Click on the four files one by one; after it downloads, select the four .ttf files, then right click, then Install. You may need to provide the Administrator password for that.

After you install the MesloLGS NF font, head over to General → Fonts in the left pane of Cmder Settings, and change the main console font and alternative font to MesloLGS NF. Change the font size to something larger, as the prompt looks like: What is this? A prompt for ants? :) Ok, jokes apart. Your settings should look something like this:

Cmder Settings

Optional: Head over to the Features → Colors tab and change the Schemes option to <Ubuntu>. You can select whatever you like. If you didn’t like any of these, then head over to this page and follow the instructions to install a theme you like. And wherever it says, ConfigPath, it means the path written near the Storage section above all the settings. Mine is C:\tools\Cmder\vendor\conemu-maximus5\ConEmu.xml. I personally prefer <Ubuntu>, provided with Cmder already.

After you do that, let’s install MSYS2.

Installing MSYS2

First of all, download the installer (*.exe) from this website and follow the instructions there. It’s as simple as Next, Next and Install :)

After you install MSYS2, again open the settings of Cmder. Go to Startup → Tasks and click on Add/Refresh default tasks, and on the dialog box, click Refresh default tasks.

Refresh default tasks

Refresh default tasks

After you do that, you will see some tasks in the Predefined tasks panel. Click on the {Bash::Msys2–64} task and click Clone at the bottom. Then, click on the newly cloned task and at the white input box at the right, paste the following:

set CHERE_INVOKING=1 & set MSYSTEM=MINGW64 & set "PATH=%ConEmuDrive%\msys64\mingw64\bin;%ConEmuDrive%\msys64\usr\bin;%PATH%" & %ConEmuBaseDirShort%\conemu-msys2–64.exe -new_console:p %ConEmuDrive%\msys64\usr\bin\bash.exe -- login -ic "exec zsh" -new_console:C:"%ConEmuDrive%\msys64\msys2.ico"
Enter fullscreen mode Exit fullscreen mode

Change task options

Change task options.

What we’re basically doing is, we’re setting CHERE_INVOKING env var to 1, which tells Bash to not cd into the HOME directory always. We’re also setting the MSYSTEM to MINGW64 to use Mingw as the MSYS System instead of the Default MSYS, which is slower. Then, we’re setting the PATH variable to the msys64\usr\bin and msys64\mingw64\bin to use Windows-native binaries, *.exe to serve the purpose of basic Unix commands, like clear , ls , rm , and even utilities like curl. Coming to the PATH, we’re also setting the path to the Native Windows %PATH% variable. This helps to use the already-installed utilities like node, kubectl, docker, npm, etc. And we’re also invoking conemu-msys2-64.exe , which helps to emulate the MSYS System. Then, we’re passing -new_console:p, which starts a new Cmder/ConEmu Console Session. After that, we’re invoking our hero, bash, and setting it as a login shell. The -c "exec zsh" part should be kept for future, now remove that. Just keep ....exe --login -i . After that, again some native Cmder commands.

mic drop……………

So, after that, go to Startup section (the parent one, neither the Tasks, nor Environment) and select the Specified named task to the one we’ve just created/edited.

Installing essential utilities

Let’s install some basic utilities:

pacman -S man vim nano tar
pacman -S openssh rsync make
pacman -S zip unzip
pacman -S mingw64/mingw-w64-x86_64-jq
Enter fullscreen mode Exit fullscreen mode

Notice how easy it is to install things. For example, adding rsync to Windows is historically not easy at all — you end up going through various ports, forks, etc. This is much easier.

Note about sudo: MSYS2 doesn't provide it. See this question or imachug/win-sudo.

Installing Git for Windows

If you had Git for Windows installed previously, uninstall it or just remove C:\Program Files\Git\..., or wherever you installed Git, from $PATH.

Reference

  1. Edit /etc/pacman.conf and just before [mingw32] , add the git-for-windows packages repository:
[git-for-windows]
Server = https://wingit.blob.core.windows.net/x86-64 
Enter fullscreen mode Exit fullscreen mode
  1. Authorize a signing key:
curl -L https://raw.githubusercontent.com/git-for-windows/build-extra/HEAD/git-for-windows-keyring/git-for-windows.gpg |
pacman-key --add - &&
pacman-key --lsign-key 3B6D86A1BA7701CD0F23AED888138B9E1A9F3986
Enter fullscreen mode Exit fullscreen mode
  1. Then, update/synchronise the repository with:
pacman -Syyuu
Enter fullscreen mode Exit fullscreen mode

This updates msys2-runtime and therefore will ask you to close the window (not just exit the pacman process). Don't panic, simply close all currently open MSYS2 shells and MSYS2 programs. Double-check Task Manager and kill pacman.exe if it's still running after the window is closed, because it can linger. Once all are closed, start a new terminal again.

  1. Then synchronize again (updating the non-core part of the packages):
pacman -Syuu
Enter fullscreen mode Exit fullscreen mode
  1. After that, install git and necessary things:
pacman -S git git-extras
pacman -S mingw-w64-x86_64-git-credential-manager mingw-w64-x86_64-git-lfs mingw-w64-x86_64-git-doc-man mingw-w64-x86_64-git-doc-html
Enter fullscreen mode Exit fullscreen mode
  1. Check that everything works by doing git --version and it should output something like git version 2.14.1.windows.1 (or newer).

Output for git --version.

Output for git --version.

Installing ZSH

Finally, our heroine, ZSH! To install it, use:

pacman -S zsh
Enter fullscreen mode Exit fullscreen mode

After that, run clear to clear everything up :)

Remember above, I told to keep -c "exec zsh" for future? Now the time comes. Add the -c "exec zsh" to the required place. Now, exit Cmder and open it again, you will get zsh! Yay!

It looks cool at this point, but it still needs its supporting actor.

Installing Oh-My-Zsh

Here’s our supporting actor, Oh-My-Zsh, which helps us to theme our zsh to the one we’ve seen at the top!

So, first of all, run:

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
Enter fullscreen mode Exit fullscreen mode

This installs oh-my-zsh. Done.

Theme-ing Oh-My-Zsh

To achieve the result above, we need to install a theme called Powerlevel10k. Run:

git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
Enter fullscreen mode Exit fullscreen mode

Then, type nano ~/.zshrc. This opens nano, a text editor. Edit the line where it says, ZSH_THEME="robbyrussell" to ZSH_THEME="powerlevel10k/powerlevel10k". Restart Cmder.

After that, it automatically runs p10k configure. This asks some questions, just follow whatever is told on the screen. To achieve the result above, use the Rainbow Prompt style. I recommend turning on Instant Prompt. You can read more about it here. Also, you can turn on Transient Prompt. You can read more about it here.

At this point, my ~/.zshrc looks like this:

# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
# Path to your oh-my-zsh installation.
export ZSH="/c/Users/AnotherUser.APPLE.000/.oh-my-zsh"
# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="powerlevel10k/powerlevel10k"
# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in $ZSH/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )
# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"
# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"
# Uncomment the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"
# Uncomment the following line to automatically update without prompting.
# DISABLE_UPDATE_PROMPT="true"
# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13
# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS="true"
# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"
# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"
# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"
# Uncomment the following line to display red dots whilst waiting for completion.
# COMPLETION_WAITING_DOTS="true"
# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"
# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"
# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder
# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git zsh-autosuggestions zsh-syntax-highlighting)
source $ZSH/oh-my-zsh.sh
# User configuration
# export MANPATH="/usr/local/man:$MANPATH"
# You may need to manually set your language environment
# export LANG=en_US.UTF-8
# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
#   export EDITOR='vim'
# else
#   export EDITOR='mvim'
# fi
# Compilation flags
# export ARCHFLAGS="-arch x86_64"
# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
#source /usr/share/git/completion/git-completion.sh
#ZSH_THEME="powerlevel10k/powerlevel10k"
#parse_git_branch() {
 #git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
#}
#PROMPT_USER="\[\033[32m\]\u@\h\[\033[00m\]"
#PROMPT_DIR="\[\033[33m\]\w\[\033[00m\]"
#PROMPT_BRANCH="\[\033[35m\]\$(parse_git_branch)\[\033[00m\]"
#export PS1="$PROMPT_USER $PROMPT_DIR$PROMPT_BRANCH"$'\n$ '
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
Enter fullscreen mode Exit fullscreen mode

Note that I’ve added two extra plugins (look for the plugins=... line), zsh-autosuggestions and zsh-autocomplete. You can copy this and paste this to nano editor while editing ~/.zshrc . But you do need to install them. To do that, run:

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/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
Enter fullscreen mode Exit fullscreen mode

You can learn more about them here and here

After that, restart your terminal.

Conclusion

Therefore, I conclude that, Windows users can also have a Linux-y environment, the most powerful!
If you have any questions/suggestions, please write down below. And if I’ve made any mistake, please tell me. I’ll fix it ASAP.
Happy hacking!

Top comments (8)

Collapse
 
stephenasamoah profile image
Stephen Asamoah

After the step to "Authorize signing key", I get this error when I ran 'pacman Syyuu':
error: git-for-windows: signature from "Johannes Schindelin <johannes.schindelin@gmx.de>" is unknown trust

Collapse
 
shrihankp profile image
Shrihan

Hi. It seems the signatures have changed since writing the article.

Please see this: github.com/git-for-windows/git/wik...

Collapse
 
rescenic profile image
Muhammad Ridwan Hakim

I prefer mintty. Install zsh, oh-my-zsh, and powerlevel10k in msys2. Just set "LOGINSHELL=zsh" in msys2_shell.cmd file. You can also append Windows paths (environment) to MSYS2 environment.
msys2
msys2env

Collapse
 
shrihankp profile image
Shrihan

Mintty is the default emulator for MSYS. It comes bundled with the installer and is installed along with MSYS base system. Of course it's a great choice!

Collapse
 
fatihaziz profile image
Fatih Aziz • Edited

YOUR TUTORIAL IS THE BEST!!!
i'd try so many tutorial so i can use oh-my-zsh to my vscode on windows 10 home edition.
only yours is work!

Thank you very much for helping me!

Collapse
 
shrihankp profile image
Shrihan

You're welcome brother!

Collapse
 
furopi profile image
furopi

Great tutorial! Thanks!!! <3

Collapse
 
shrihankp profile image
Shrihan

You're welcome!