Blog

Zsh and Oh My Zsh with WSL and Cmder

One of the great things about WSL is that it is essentially fully-fledged Linux, so we can take advantage of all of the normal Linux shell customization tools. Here, I'll talk about setting up Zsh and Oh My Zsh on our Ubuntu WSL console, taking advantage of Cmder's emulation to give us full color support.

Installing Zsh

First up, we want to install Zsh. Zsh is a shell similar to Ubuntu's default of Bash, except it adds a few features (like better tab-completion). We will use it as a framework later for installing Oh My Zsh, which lets us easily install extensions and use different themes for our console.

We'll install Zsh using the apt package manager we already have:

sudo apt install zsh

When that's installed, you won't see anything just yet: we have to edit our .bashrc file in our Linux home directory (denoted by ~) to basically redirect Bash to Zsh.

To edit that file, we can use the Nano text editor:

nano ~/.bashrc

Once we are in the file, we want to put the following lines at the top. They boil down to "if we are running normally, switch to Zsh."

if [ -t 1 ]; then
  exec zsh
fi

I added those lines just below the first comments in the file:

With those added, use Ctrl+O to save (press enter to accept the file name) and Ctrl+X to exit. Let's check that everything is working by closing and re-opening our entire console. When you do, you'll see a menu like below:

We will install Oh My Zsh in just a moment, which will overwrite whatever option we choose here. Let's selection option 0 to get out of this menu and back to our shell.

Installing Oh My Zsh

After installing Zsh, our shell works... worse:

We've got one more thing to install: the Oh My Zsh framework. We can install it with the following command (Cmder enables copy and paste, so Ctrl+V should work in your shell):

sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

Well, we have colors! There's a lot of themes available; I'll go through using agnoster here. All we have to do is edit the .zshrc file:

nano ~/.zshrc

Find the line that sets the theme to robbyrussel and change it to set it to agnoster.

With that done, save and exit like we did before. Back at your console, enter the following command to reload our configuration to see our changes:

source ~/.zshrc

Cleaning it up

Much better! One more thing: I'm not a fan of the user and computer name being in the prompt, but the agnoster theme provides an easy fix. We can add a line to our .zshrc file to hide them:

# Hide the user and computer name in our prompt
DEFAULT_USER=$USER

I added it beneath the "User configuration" header, but it will work anywhere. Source the file again as we did earlier, and you should see the prompt be a bit cleaner. It is worth noting that now might be a good time to revisit your color scheme in Cmder, since that will control how the colors appear in the console.

Next Steps

From here, customization depends on your needs. There's more we can do to improve our WSL console experience, like touching up the colors when you use ls, installing extensions, or making some helpful aliases to integrate with Windows programs.

We can also do more with Cmder: we can set up the normal Windows console and PowerShell, play around with Cmder's tasks for launching different consoles, or get Cmder better integrated with Windows.

If you're the security-minded type, you might be interested in getting SSH keys set up in WSL and everything else.

There's certainly a lot of options, but in my experience I've found that having a good setup makes work a lot more efficient. Good luck!

GuidesJack WarrenConsole