Have a great looking terminal and a more effective shell with Oh my Zsh using powerlevel10k on WSL 2 using Windows

Now Git, the Azure CLI, and Kubernetes Kubectl are used more and more, the bash prompt is one of the most productive ways to do your daily work. When using Windows, the usage of WSL (2) is a must-have when it comes to having a Linux shell to execute bash scripts. This works nice but with Oh-My-Zsh you can make the command-prompt look- and behave better so you can do your work more efficient.

Update on 30-01-2022: Update theme to powerlevel10k and the necessary steps

This blogpost tells you the steps to take to get a prompt on Windows with WSL using Visual Studio Code. Actually, this blog post is a reminder for me. It’s how I have configured the WSL and VS Code. It looks like this:

vscodeohmyzsh

Why use Oh-My-Zsh

Oh-My-Zsh is a plugin that runs on top of ZSHUsing Oh-My-Zsh instead of the default bash prompt has several advantages:

  • Themeable prompts. By default, the robbyrussell theme is configured. There are many themes to choose from: https://github.com/robbyrussell/oh-my-zsh/wiki/Themes. When prompted in a git repo, Zsh shows information about the Git status for example.
  • Advanced command history. Works over shells and even works after reboots. See the whole history with “alt+/”.  Very powerful is searching in history using a partly typed command: for example type az and when you hit your arrow up, it only shows the commands in history which start with az.
  • Spelling correction. Type case insensitive and Zsh finds out what you mean.
  • Auto completion. Type – after a command and tab through the command options.
  • Keybindings. Type bindkey to see all keybindings.
  • Globbing. Is a short expression that lets you filter files. For example:
    ls mydir/**/*.json to have all json files in any directory within mydir.
  • Minimize keystrokes. Here is a cheatsheet to make you type less.
  • Plugin support. By default the Git plugin is already configured. Let’s configure the zsh-syntax-highlighting and zsh-autosuggestions. There are much more plugins which can be found here: https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins or here https://github.com/zsh-users

With the needed knowledge a couple of them should be possible in the bash prompt as well, but Oh-My-Zsh makes it very easy.

This list is not extensive. There are so many possibilities, too many to sum them up here. In short: Using Oh-My-Zsh makes your prompt highly customizable using community-created plugins and themes so it makes your life easier.

Why I’m using VS Code together with the terminal in VS Code

I’m using the prompt a lot, to type and execute commands. Very often I need to execute a command in a script file. Therefore I like to use Visual Studio Code together with the terminal within VS Code. So I have only 1 window open to have both the file and the terminal.

I could use Windows Terminal or Cmder if I would only use the command prompt. But having a file ready to be filled with commands or executing commands which are already in a file, I really like to use Visual Studio Code.

Using Oh-My-Zsh also works in Windows Terminal of course and also if you just open WSL itself. This blogpost shows my personal configuration.

Steps to get the cool prompt

Prerequisite

In Windows:

A. Install Windows Sub System 2 (WSL)


I’m using Ubuntu 20.04 LTS:
https://www.microsoft.com/en-us/p/ubuntu-2004-lts/9n6svws3rx71#activetab=pivot:overviewtab

B. Install Visual Studio Code
By downloading it here: https://code.visualstudio.com/docs/?dv=win

C. Download the following 4 fonts for powerlevel10k and install them on Windows

https://github.com/romkatv/powerlevel10k#manual-font-installation

D. Start Ubuntu and in the settings in Windows of the window, change the font to: MesloLGS NF

ohmyzshubuntu


Installation and configuration steps

1. Open Ubuntu

2. Install Zsh (and curl and git)

sudo apt-get install zsh curl git

3. Install Oh-My-Zsh

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

4. Get the powerlevel10k theme

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

5. Change the Theme to powerlevel10k

nano ~/.zshrc
change the value of ZSH_THEME="robbyrussell"
to: ZSH_THEME="powerlevel10k/powerlevel10k"

6. Configure powerlevel10k

restart zsh by executing:

exec zsh

The powerlevel10k wizard starts.
Configure according to your preferences.

When needed, rerun the configuration wizard using:

p10k configure

7. Optionally: Enable autosuggestions

Clone the Git repo

git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

Add the plugin to the list in the oh-my-zsh configuration

nano ~/.zshrc

find the following by scrolling down:

plugins=(
    git
)

make sure it looks like this:

plugins=(
    git
    zsh-autosuggestions
)

Save the file with CTRL-X. Enter y and hit enter to save the file to the same filename.

8. Optionally: Enable syntax highlighting

Clone the Git repo

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
plugins=(
    git
    zsh-autosuggestions
    zsh-syntax-highlighting
)

9a Configure Visual Studio Code

Open Visual Studio Code and make sure to configure the following settings:
Of course, you can also use one of the other powerline fonts.
The second line makes the WSL the default shell when opening the terminal.

"terminal.integrated.fontFamily": "MesloLGS NF",
"terminal.integrated.shell.windows": "C:\\Windows\\System32\\wsl.exe"

Restart VS Code to apply the changes.

9b Configure Windows Terminal

Choose for the Settings in the Windows Terminal

windowsterminal

Add the following line to the profile of Ubuntu:

"fontFace": "MesloLGS NF"
So the result looks like this in profiles.json:
// To view the default settings, hold "alt" while clicking on the "Settings" button.
// For documentation on these settings, see: https://aka.ms/terminal-documentation
{
    "$schema": "https://aka.ms/terminal-profiles-schema",
    "defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
    "profiles":
    [
        {
            // Make changes here to the powershell.exe profile
            "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
            "name": "Windows PowerShell",
            "commandline": "powershell.exe",
            "hidden": false
        },
        {
            // Make changes here to the cmd.exe profile
            "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
            "name": "cmd",
            "commandline": "cmd.exe",
            "hidden": false
        },
        {
            "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
            "hidden": false,
            "name": "PowerShell Core",
            "source": "Windows.Terminal.PowershellCore"
        },
        {
            "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}",
            "hidden": false,
            "name": "Ubuntu",
            "source": "Windows.Terminal.Wsl",
            "fontFace": "DejaVu Sans Mono for Powerline"
        },
        {
            "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
            "hidden": false,
            "name": "Azure Cloud Shell",
            "source": "Windows.Terminal.Azure"
        }
    ],

    // Add custom color schemes to this array
    "schemes": [],

    // Add any keybinding overrides to this array.
    // To unbind a default keybinding, set the command to "unbound"
    "keybindings": []
}
10. Optionally: Multiple command prompts in VS Code
If you want to be able to have multiple command prompts in Visual Studio Code, then install extension https://marketplace.visualstudio.com/items?itemName=Tyriar.shell-launcherTo have a WSL, Powershell and Powershell Core command prompt add this to the configuration: (Make sure you have installed the Powershell Core Command Prompt before using the exact configuration below):

Add the following settings to VS Code:

"shellLauncher.shells.windows": [
        {

            "shell": "C:\\Windows\\System32\\wsl.exe",
            "label": "WSL Bash"

        },
        {

            "shell": "C:\\Program Files\\PowerShell\\7-preview\\pwsh.exe",            
            "label": "Powershell Core"            

        },
        {

            "shell": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
            "label": "PowerShell"
        }
    ],
Add a short-cut to change the shell:
Add this line to keybindings.json to change shell with ctrl-shift-t
 {
    "key": "ctrl+shift+t",
    "command": "shellLauncher.launch"
}
11. Optionally: Short-cut in VS Code to execute selected line
I also like to have a short-cut to execute the selected line in a file in VS Code:
{
    "key": "f3",
    "command": "workbench.action.terminal.runSelectedText"
}

Conclusion

I like the way of working with Oh-My-Zsh very much. Although I’m still learning to use it every day because it’s so extensive.

Advertentie

Een gedachte over “Have a great looking terminal and a more effective shell with Oh my Zsh using powerlevel10k on WSL 2 using Windows

  1. [oh-my-zsh] Insecure completion-dependent directories detected:
    drwxrwxrwx 1 danisheng danisheng 512 Nov 14 22:25 /home/user/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting

    [oh-my-zsh] For safety, we will not load completions from these directories until
    [oh-my-zsh] you fix their permissions and ownership and restart zsh.
    [oh-my-zsh] See the above list for directories with group or other writability.

    [oh-my-zsh] To fix your permissions you can do so by disabling
    [oh-my-zsh] the write permission of “group” and “others” and making sure that the
    [oh-my-zsh] owner of these directories is either root or your current user.
    [oh-my-zsh] The following command may help:
    [oh-my-zsh] compaudit | xargs chmod g-w,o-w

    [oh-my-zsh] If the above didn’t help or you want to skip the verification of
    [oh-my-zsh] insecure directories you can set the variable ZSH_DISABLE_COMPFIX to
    [oh-my-zsh] “true” before oh-my-zsh is sourced in your zshrc file.

    Like

Geef een reactie

Vul je gegevens in of klik op een icoon om in te loggen.

WordPress.com logo

Je reageert onder je WordPress.com account. Log uit /  Bijwerken )

Twitter-afbeelding

Je reageert onder je Twitter account. Log uit /  Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log uit /  Bijwerken )

Verbinden met %s

Deze site gebruikt Akismet om spam te bestrijden. Ontdek hoe de data van je reactie verwerkt wordt.