I’ve decided to migrate my CLI from the reliable and well-known bash to the powerful and versatile zsh. It’s an extended evolution of the Bourne Shell (sh) – it not only inherits many of Bash’s familiar features but also introduces a series of new functionalities, plugin support, and custom themes. Apple adopted Zsh as the default shell some time ago, and I still needed to make the switch on my Linux systems, including WSL2 on Windows.
Why Zsh?
Before diving into the installation and configuration of Zsh (short for “Z Shell”), it’s essential to understand the reasons:
Advanced Autocompletion: Zsh provides intelligent and advanced autocompletion that far exceeds Bash’s capabilities
Extensive Customization: Zsh allows deep and flexible user interface customization. Users can choose from a wide range of predefined themes or create their own designs to adapt the command line appearance to their individual preferences.
Powerful Extensions and Plugins: The Zsh community offers a vast number of extensions and plugins that further extend its functionality. From development tool integration to advanced command history management, these extensions allow users to customize their work environment according to their specific needs.
Enhanced History: Zsh provides an improved command history that makes searching, editing, and running previous commands easier. This feature is especially useful for users who work with a large number of commands on a daily basis.
Installation
Linux (Ubuntu)
On Debian-based Linux systems like Ubuntu, installing Zsh is straightforward. You can check if it’s already installed and install it if not:
⚡ luis@luix:~ $ sudo dpkg -l zsh
:
⚡ luis@luix:~ $ apt install zsh
I verify that it’s a valid shell:
⚡ luis@luix:~ $ grep zsh /etc/shells`
/bin/zsh
/usr/bin/zsh
Switch to Zsh for both luis and root with chsh -s $(which zsh)
⚡ luis@luix:~ $ sudo chsh $USER -s $(which zsh)
Contraseña:
⚡ luis@luix:~ $
⚡ luis@luix:~ $ sudo su -
Contraseña:
root@luix:~# chsh -s $(which zsh)
root@luix:~#
I log out of the graphical session (sometimes I do a full reboot) and log back in with both users to verify that zsh is running.
⚡ luis@luix:~ $ ps
PID TTY TIME CMD
48727 pts/0 00:00:00 zsh <=== !!
48736 pts/0 00:00:00 ps
If you don’t have the .zshrc file, zsh-newuser-install will probably run and offer to create it. In my case I selected option (2) Populate your ~/.zshrc with the configuration recommended.
In case it’s useful, I’ve left several files in this repository zsh-zshrc on GitHub:
- ~/.zshrc. My
zshconfiguration file - ~/.tmux.conf. My
tmuxconfiguration file. I use it in combination withzsh– check out “Terminals with tmux”. It’s accompanied by a script I’ve called t that runstmux. - ~/.zshrc.async. In the repository you’ll see this file. It’s a copy of a (zsh) script by Mathias Fredriksson that allows running asynchronous jobs. I download my own copy from my
.zshrcthe first time. - ~/.oh-my-posh.yaml: Theme for Oh My Posh, described later.
Extra: starship
starship.rs advertises itself as “a Prompt for any Shell, minimal, super-fast, and highly customizable”. It uses symbols and special characters not present in default fonts, so we need to install a Nerd Font.
- From the Nerd Fonts repo >
Downloads. I search for and downloadFira Code(any will do).
sudo apt install fontconfig unzip
mkdir -p ~/.fonts
cd .fonts
curl -LJs -o FiraCode.zip https://github.com/ryanoasis/nerd-fonts/releases/download/v3.2.1/FiraCode.zip
unzip FiraCode.zip && rm FiraCode.zip
fc-cache -fv
- I configure it as the default font in Gnome Console “TODO”
- Install the latest version with:
curl -sS https://starship.rs/install.sh | sh - Configure my
.zshrc(previous section)
macOS
macOS users already have Zsh installed by default, which means they can start using it immediately without any additional installation. They can open Terminal and begin experimenting with Zsh’s features and capabilities.
Extra: starship on macOS
starship.rs advertises itself as “a Prompt for any Shell, minimal, super-fast, and highly customizable”. It uses symbols and special characters not present in default fonts, so we need a Nerd Font.
- From the Nerd Fonts repo >
Downloads. I search for and downloadFira Code(any will do). Unzip the file, select all.ttffiles > right-click >Open> Install all. - I configure it as the default font in iTerm: Settings -> Profiles -> Default -> Text -> Font
FiraCode Nerd Font - Install the latest version with:
brew update && brew upgrade && brew install starship - Configure my
.zshrc
Windows 11
For Windows 11 users who want to use Zsh, I recommend installing the Windows Subsystem for Linux (WSL2) and a compatible Linux distribution.
The default shell in Ubuntu for WSL2 is bash but you can change it exactly the same way as on Linux, and you can also install “tmux”. I talk about it in this article about Windows for Development.
Extra: Nerd Fonts
- From the Nerd Fonts repo >
Downloads. I search for and downloadFira Code(any will do). Unzip the file, select all.ttffiles > right-click >Open> Install all. - I configure it as the default font in iTerm: Settings -> Profiles -> Default -> Text -> Font
FiraCode Nerd Font - Install the latest version with:
brew update && brew upgrade && brew install starship - Configure my
.zshrc
Extra: Oh My Posh
Oh My Posh advertises itself as “a Prompt engine for any Shell”. It allows rendering the PROMPT in a very advanced way, but what I like most is how it handles GIT. The biggest problem when working in a Git repository is PROMPT rendering. Of all the advanced renderers I’ve tried (including Starship), none can do it as fast as Oh My Posh. For this reason alone it has become my choice, above Starship.
I install Oh My Posh following its guide.
- On Linux (including WSL2), themes are installed in
$HOME/.cache/oh-my-posh/themes, although I use my modified theme ~/.oh-my-posh.yaml which I copy directly to my$HOME. An installation example in~/bin.
sudo apt install unzip
mkdir ~/bin
curl -s https://ohmyposh.dev/install.sh | bash -s -- -d ~/bin
curl -LJs -o ~/.oh-my-posh.yaml https://raw.githubusercontent.com/LuisPalacios/zsh-zshrc/main/.oh-my-posh.yaml
PATH=$PATH:/home/luis/bin
I install a font: oh-my-posh font install (I choose Melo). Oh My Posh is designed to use Nerd Fonts. Nerd Fonts are popular fonts that are patched to include icons. To see the icons displayed in Oh My Posh, install a Nerd Font, and configure your terminal to use it. I add the following to my ~/.zshrc: eval "$(oh-my-posh init zsh --config ~/.oh-my-posh.yaml)"
- On macOS:
brew install jandedobbeleer/oh-my-posh/oh-my-posh
- On Windows
Here we have two places where it’s worth installing:
Windows PowerShell - I did the installation via
winget, for PowerShell with Windows Terminal:winget install JanDeDobbeleer.OhMyPosh -s wingetWindows WSL2 - Installation via curl:
curl -s https://ohmyposh.dev/install.sh | bash -s -- -d ~/binThemes: they are installed in
$HOME/.cache/oh-my-posh/themes(on Linux)POSH_THEMES_PATH=C:\Users\<user>\AppData\Local\Programs\oh-my-posh\themes\(on Windows)
I have my theme here: ~/.oh-my-posh.yaml which I copy directly to my $HOME.
- Zsh - Note for WSL 2 (next point). On Unix systems I use Oh My Posh together with Zsh, so I recommend checking out this GitHub Repository: My zsh files and specifically my .zshrc file.
References
Cheatsheet
In the highly recommended Ultimate Resource List you’ll find an excellent Cheatsheet. I highlight the entries related to zsh:
Shells
- Zsh - is a shell designed for interactive use, although it’s also a powerful scripting language.
- Oh My ZSH! - is the best framework for managing your Zsh configuration. Check out its site, oh-my-zsh comes with thousands of useful functions, helpers, plugins, themes. It brings several plugins that make life easier. The best thing about Oh My Zsh are its themes.
Shell Plugins
- zsh-autosuggestions - Fish-like autosuggestions for Zsh.
- zsh-syntax-highlighting - Fish shell-like syntax highlighting for Zsh.
- Awesome ZSH Plugins - A list of frameworks, plugins, themes, and tutorials for ZSH.
Managers
- tmux - is a terminal multiplexer, allowing you to easily switch between several programs in a single terminal.
By making the transition from Bash to Zsh, you’re not only adopting a more powerful and versatile tool, but you’re also opening the doors to a more efficient and adaptable command line experience. With its advanced features and active developer community, Zsh positions itself as the ideal choice for users looking to maximize their productivity and control in Unix-like environments.