Customizing Windows Terminal

Something you have to face every day, and if it doesn't look good, it can affect your mood 🐶. (Finally going to finish the overdue blog posts, the diarrhea-style updates are ending.)

This post was translated from my Chinese blog post with the aid of ChatGpt.

Many of the methods I found were outdated, so in the end, I simply followedthe official documentation of Oh My Poshto configure it.

Updating PowerShell

First, use the $PSVersionTable command to check the PowerShell version. The system comes with version 5.1, while the latest version is 7.2. These two versions are independent and can be installed simultaneously. Here, I installed the new version.

After installing, open Windows Terminal and go to Settings. Change the default profile to PowerShell (the system’s built-in version is called Windows PowerShell).
ps1.png

Changing Font

When selecting a font, choose one that includes Nerd fonts, otherwise, some icons may appear as gibberish. I used the font sarasa-mono-sc-nerd-font, which is the Simplified Chinese mono-spaced Sarasa Gothic font with additional Nerd font characters. Here is the download link, and I downloaded the sarasa-mono-sc-nerd-font.zip:

Nerd Fonts is a project that addresses the issue of programmers lacking suitable fonts during development by using a plethora of font icons. It can incorporate a wide range of external fonts from popular font icon libraries into the project at hand. The supported font icon libraries include Font Awesome, Devicons, Octicons, and more.

"Sarasa Mono SC Nerd Font" is a free and open-source font that combines elements from Iosevka and Source Han Sans. It's designed for optimal usage within terminal environments.

Font File Name Suffixes

Reference: zrqlx126

After downloading, extract and install the required font. The meanings of font suffixes are as follows:

Additional Terminology

  • unhinted - Unhinted font
  • ttf - TrueType font file format
  • ttc - TrueType font collection file format (Sarasa Mono SC categorizes fonts by weight, merging similar ttf font files)

Name after the First Hyphen

  • gothic - Full-width quotation marks
  • ui - Narrow-width quotation marks
  • mono - Full-width dashes
  • term - Half-width dashes
  • fixed - Non-ligature and half-width dashes

Name after the Second Hyphen

  • cl - Classical Chinese character form
  • sc - Simplified Chinese character form
  • tc - Traditional Chinese character form
  • j - Japanese character form
  • k - Korean character form
  • hc - Hong Kong character form

Name after the Third Hyphen

  • slab - Extra bold stroke character form
  • regular - Regular font
  • italic - Italic font
  • extralight - Extra light font
  • light - Light font
  • semibold - Semi-bold font
  • bold - Bold font
  • extralightitalic - Extra light italic font
  • lightitalic - Light italic font
  • semibolditalic - Semi-bold italic font
  • bolditalic - Bold italic font

Name after the Fourth Hyphen

  • nerd - Additional Nerd fonts

Then, in Windows Terminal, navigate to Settings - Profiles - PowerShell - Appearance - Font, and set the font to Sarasa-mono SC Nerd Font.
Font.png

Installing Beautification Modules

Use winget to install the beautification module Oh My Posh:

1
winget install JanDeDobbeleer.OhMyPosh -s winget

Then, restart Windows Terminal and open the PowerShell configuration file using the following command:

1
notepad $PROFILE

If it prompts that the configuration file doesn’t exist, use this command to create it:

1
New-Item –Path $ProfileType File –Force

Paste and save the following line:

1
oh-my-posh init pwsh | Invoke-Expression

Execute the following command to reload the configuration file or restart Windows Terminal to see the changes:

1
. $PROFILE

Changing the Theme

Oh My Posh offers many themes. You can use the following command to see available themes or visit the official website for a theme preview:

1
Get-PoshThemes

Once you find a suitable theme, copy the command below for that theme and paste it into your PowerShell configuration file. Replace "Theme" with the actual theme name:

1
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/Theme.omp.json" | Invoke-Expression

For example, if you chose the theme takuya, the command would be:

1
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/takuya.omp.json" | Invoke-Expression

If you want to modify the theme colors, first export the current theme configuration:

1
oh-my-posh config export --output ~/.pwshtheme.omp.json

Please change the path yourself, the path I exported here is C:\Users\Username\.pwshtheme.omp.json, and the theme file is in C:\Users\Username\AppData\Local\Programs\oh-my-posh\themes.Then, open the exported file and adjust the colors.

If you want to modify more content please refer to the official documentation instructions

After making the modifications, use notepad $PROFILE to open the configuration file for PowerShell. Replace the original theme configuration with the new one, and pay attention to the file path.

1
2
+ oh-my-posh init pwsh --config "~/.pwshtheme.omp.json" | Invoke-Expression
- oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/takuya.omp.json" | Invoke-Expression

Installing Scoop

Reference: P3TERX

Scoop is a Windows package manager. Use the following command to install Scoop:

1
Invoke-WebRequest get.scoop.sh | Invoke-Expression

Install aria2

aria2 can enable multi-threaded downloads for Scoop.

1
scoop install aria2

Install extras bucket

You can add additional buckets to Scoop. Bucket is a software repository. After installing Scoop, you’ll have the Main bucket, but its inclusion criteria are quite strict. Therefore, it’s advisable to install an “extras bucket,” which can provide more software options, including Steam. However, it’s not recommended to add too many buckets.

The installation command is as follows:

1
scoop bucket add extras

Common Commands

  • scoop update —— Update Scoop and the list of software packages.
  • scoop install -k <app> —— Install a package non-globally (and disable package caching).
  • scoop install -gk <app> —— Install a package globally (and disable package caching).
  • scoop uninstall -p <app> —— Uninstall a non-global package (and delete configuration files).
  • scoop uninstall -gp <app> —— Uninstall a global package (and delete configuration files).
  • scoop update -k * —— Update all non-global packages (and disable package caching).
  • scoop update -gk * —— Update all packages (and disable package caching).
  • scoop cleanup -k * —— Remove all old versions of non-global packages (and delete package caches).
  • scoop cleanup -gk * —— Remove all old versions of packages (and delete package caches).
  • scoop cache rm * —— Clear the package cache.
  • scoop bucket list —— View all buckets.

Auxiliary Modules

Checking Script Execution Policy

First, check the script execution policy settings. There are four policy options:

1
Get-ExecutionPolicy
  • Restricted —— Default setting, doesn’t allow any script to run.
  • AllSigned —— Only runs scripts that have been digitally signed with a certificate.
  • RemoteSigned —— Runs local scripts without requiring digital signatures, but scripts downloaded from the internet must be signed.
  • Unrestricted —— Allows all scripts to run.

If the policy is not RemoteSigned or Unrestricted, execute the following command to modify the policy. Otherwise, some modules might not install properly:

1
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Confirm

posh-git

posh-git is a PowerShell module that integrates Git with PowerShell, allowing you to display summarized Git status information in the PowerShell prompt. It also provides Tab completion for common Git commands, branch names, paths, and more.

1
PowerShellGet\Install-Module posh-git -Scope CurrentUser -Force

Next, execute the following command to integrate the module:

1
Add-PoshGitToProfile

Alternatively, you can add the following line to your PowerShell configuration file:

1
Import-Module posh-git

Afterward, reload your configuration file or restart Windows Terminal. When you type git fe and press Tab, if it automatically completes to git fetch, then the integration was successful.

PSReadLine

Reference: about_PSReadLine

PSReadLine 2.1 provides a powerful command-line editing experience for the PowerShell console. It offers the following features:

  • Syntax coloring of the command line
  • Intuitive indication of syntax errors
  • Improved multiline experience for editing and history
  • Customizable key bindings
  • Cmd and Emacs modes
  • Numerous configuration options
  • Bash-style completion in Cmd mode (optional, with Emacs mode as the default)
  • Emacs yank/kill cycle
  • Token-based “word” movement and deletion based on PowerShell tokens
  • Predictive IntelliSense

PSReadLine 2.1.0 comes bundled with PowerShell 7.2 and is supported in all the versions of PowerShell that are currently supported. Therefore, there’s no need for additional installation.

Configuration

First, open the configuration file:

1
notepad $PROFILE

Copy and paste the following content:

1
2
3
4
5
6
7
Set-PSReadLineOption -PredictionSource History # Set prediction source to history

Set-PSReadlineKeyHandler -Key Tab -Function Complete # Set Tab key for autocompletion
Set-PSReadLineKeyHandler -Chord "Ctrl+RightArrow" -Function ForwardWord # Set Ctrl + Right Arrow for incremental completion
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo # Set Ctrl+z for undo
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward # Set Up Arrow for backward history search
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward # Set Down Arrow for forward history search

After saving, either reload the configuration file or restart Windows Terminal.

Usage

  • —— Autocomplete suggestions
  • CTRL+ —— Incremental completion of suggestions
  • and —— Search through command history
  • Tab —— Autocomplete
  • CTRL+z —— Undo

ZLocation

ZLocation is a variation of Jump-Location that tracks your most frequently used directories based on the number of times you’ve navigated to them. After a short learning phase, z will take you to the most popular directories that match all regular expressions given on the command line. You can use Tab-Completion/Intellisense to pick a non-preferred directory.

Installation

Use the following command to install and configure ZLocation:

1
Install-Module ZLocation -Scope CurrentUser; Import-Module ZLocation; Add-Content -Value "`r`n`r`nImport-Module ZLocation`r`n" -Encoding utf8 -Path $PROFILE.CurrentUserAllHosts

Usage

  • z —— Display known folders and their weights
  • z c: —— Jump to the C drive
  • z zlo —— Jump to the ZLocation folder (abbreviation)
  • z -l foo —— List all locations that match “foo”
  • z - —— Jump to the previous or initial folder that was navigated to

The Fuck

The Fuck is a fun tool that allows you to correct the previous erroneous command by typing fuck.

Installation

Before installing The Fuck, you need to have Python (3.4+), pip, and python-dev installed. The installation command is as follows:

1
pip install thefuck

Configuration

Open the PowerShell configuration file:

1
notepad $PROFILE

Paste the following content:

1
2
$env:PYTHONIOENCODING="utf-8"
iex "$(thefuck --alias)"

Save the file, then reload the configuration file or restart Windows Terminal.

Usage

After entering an incorrect command, type fuck and press Enter to confirm.

thefuxk.png

Upgrade

1
pip3 install thefuck --upgrade

Neofetch

Neofetch is a command-line system information tool written in Bash 3.2+.
Neofetch can display information about your PC’s operating system, software, and hardware.

1
scoop install neofetch

Afterwards, simply type neofetch and press Enter to view your system information.

gsudo

gsudo is a Windows alternative to sudo that offers a user experience similar to the original Unix-like sudo. It allows you to run commands with elevated privileges in the current console window, a new window, or elevate the current shell’s permissions. This eliminates the need to repeatedly run PowerShell as an administrator and avoids losing the command history when restarting.

Installation

You can install gsudo using the traditional method with winget:

1
winget install gsudo

Reducing UAC Prompts

To minimize UAC prompts and achieve multiple gsudo elevations with only one UAC prompt, you can enable credential caching. Use the following command:

1
gsudo config CacheMode auto

Windows Terminal Settings

The following settings can be applied specifically to PowerShell or adjusted as default settings for all shells by going to Settings - Profiles - Defaults.

Color Scheme

You’ve adjusted the theme for PowerShell, but the font colors for input and output are still the original ones, controlled by Windows Terminal’s Color Scheme. There are 8 default color schemes, though they might not be the most pleasing to the eye. Fortunately, you can create your own color scheme.

Open Windows Terminal’s Settings, navigate to Color Scheme, click on Add a new color scheme on the right, create your own color scheme, and then adjust the colors one by one. Once done, click Save.
Color Scheme

Then go to the PowerShell configuration file below, find Appearance - Color Scheme, and set it to the color scheme you just created.
Color Scheme 2

Acrylic Effect

In Windows Terminal’s Settings, find Show acrylic in tab row (requires relaunch) and enable it. After restarting Windows Terminal, you’ll see the tab row with the acrylic effect enabled.

Then go to the PowerShell configuration file below, find Appearance - Transparency - Enable acrylic and enable it. This will apply the acrylic effect to the window background. You can fine-tune the transparency according to your preference; I set it to 85%.

Background Image

Still in the PowerShell configuration file, find Appearance - Background image - Background image path. Choose a background image and adjust the Background image opacity.

Done

After all these configurations, your PowerShell in Windows Terminal should look much better and more functional.
ps完工.png