Level: ⭐ Difficulty: ⭐ Interesting: ⭐⭐⭐


The goal is to create a Shell that has the same experience as Linux and MAC.

If you have PowerShell basics, finish in about half an hour.

Windows users will be familiar with this ugly black box. Yes, it’s the CMD command line, and it’s been around since the dawn of Windows.

Later, Windows developed a new tool to replace CMD based on.NET Framework: Windows PowerShell, which is not only compatible with CMD, but also introduces CMDlet, which is more powerful. Open it to see the hot sun in the blue sky and white clouds. The font and color matching feel like a blue screen, which makes me feel uncomfortable, especially the mistakes thrown out. Although I read the error prompts carefully, I still don’t know what I’m talking about, and even the simplest copy-paste function cannot be realized. Can’t compare to the Shell on Linux and MAC.

Not long ago, Windows finally released its cross-platform PowerShell 7. It’s officially called PowerShell, and because it uses.NET Core as the runtime, it can be used across platforms: Windows, macOS, Linux, and even ARM. Looks like a lot of hard work from scratch.

Microsoft announced Windows Terminal at Build 2019, which unites the PowerShell, CMD, and Windows Linux Subsystem (WSL) environments on Windows.

It’s easy to confuse PowerShell with Windows Terminal; they’re two different pieces of software. PowerShell is a command line program that actually executes instructions, whereas Windows Terminal is a tool that manages various command lines. Why Windows Terminal when PowerShell alone will do? That is because Windows can not only install CMD, but also install PowerShell 5.1, PowerShell 7, WSL, Azure Cloud Shell, etc. It requires a tool for centralized management, which is Windows Terminal. As you can see from the following figure, Windows Terminal can open different command-line programs. So, the command is entered in Windows Terminal interface, it will call the corresponding PowerShell to execute, and then return to Windows Terminal to display after execution, so we do not have to face the ugly CMD, PowerShell interface.

The most exciting part: Windows Terminal can be configured with themes to make the interface look better and more personal.

Install Windows Terminal

If you’re running Windows 10, you can open the Microsoft Store, search Windows Terminal, and install it.

If you have not installed PowerShell 7.x, you are highly recommended to install PowerShell 7.x. If you cannot download PowerShell 7.x, you can download it here for Windows X64.

You will also learn from PowerShell later.

Principle of theme beautification

Saw the beautification course on the net, a lot of methods let me doubt, then looked up a lot of data, just gradually understand the whole process.

The beautification process is actually divided into two parts, PowerShell and Windows Terminal, because the two software are independent. As mentioned above, Windows Terminal is only a shell (carrier), which is used to display the output of PowerShell. Ultimately, it is PowerShell that executes the Shell. Windows Terminal itself has a theme mechanism that makes it easy to change themes through configuration files. Similarly, PowerShell can be customized, but the most powerful feature is its plug-in mechanism, which adds power to PowerShell by introducing modules into it.

So the next step is to configure PowerShell and Windows Terminal separately.

A prerequisite for

Install the tools that will be used later.

The installationVisual Studio Code(VS Code)

Powerful editor for editing configuration files. Here to install

The installationscoop

Scoop is a package management tool for Windows (unofficially), similar to Yum for Linux or Homebrew for MAC. Open PowerShell 7 and type the following command:

# set execute permission
> Set-ExecutionPolicy RemoteSigned -scope CurrentUser
Download the script from the network and install it
> Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh')
If you can't access the target address, use the following address to install it:
> Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://cdn.yulinyige.com/script/scoop-installs.ps1')
Copy the code

configurationPowerShell

Why configure PowerShell first? Because the final output is determined by The PowerShell, the native PowerShell output is ugly.

Take a look at the effect after decoration.

It’s important to note that this is just a glorified version of PowerShell itself, not Windows Terminal. PowerShell + Windows Terminal with beautification, better results.

PowerShell mainly beautifies:

  1. Change the overall color scheme.

  2. Change the output style to display the user name and computer name before the prompt.

  3. Enhanced Git command function and Git branch status display.

  4. Automatic completion function, according to the history command and the current directory to complete.

  5. The ls command displays colors.

Windows PowerShell 5.x and PowerShell 7.x are two separate shells. Note that 5.x has a Windows prefix, while 7.x does not. The configuration is also independent of each other, so if you are configuring on 7.x, enabling 5.x does not take effect.

To reduce confusion, I’ll stick with native PowerShell 7.x.

The installationoh-my-poshposh-git

Oh-my-posh is a PowerShell theme management tool, posh-git can implement git command enhancement tools like oh-my-zsh (command aliases, display branch information, etc.), however, oh-my-posh is based on posh-git, So you have to install both.

You can install it from PowerShell Gallery by opening PowerShell 7 (not Windows PowerShell) and entering the following command:

> Install-Module posh-git
> Install-Module oh-my-posh
Copy the code

configurationPowerShell

Open the Prompt

This will enable the default configuration
> Set-Prompt
# set the theme, Agnoster is the theme name
> Set-Theme Agnoster
Copy the code

The display immediately changes to a Linux Shell style. PS C:\Users\ Yulinyige > becomes yulinyige@DESKTOP-N8LA1TE. However, you will notice that the command prompt is garbled because the font currently in use contains no special symbols, which will be installed later.

The Prompt effect only works for the current window, so we have to write the configuration to the PowerShell configuration file. Where is it? Type the command (the $sign is also required) to print out the path (if you use Windows PowerShell 5.x, you’ll find it saved in the Windows PowerShell folder, so you can see that they are separate).

> $PROFILE
Copy the code

Ok, open the configuration with the previously installed VSCode.

> code $PROFILE
Copy the code

Write the following configuration to a PS1 file and save it, which will load automatically every time PowerShell is started.

Import-Module posh-git
Import-Module oh-my-posh
Set-Theme Agnoster
Copy the code

In addition, you can use PowerShell’s PSReadLine to enrich PowerShell’s capabilities by adding the following

Set-PSReadlineKeyHandler -Key Tab -Function Complete # Set Tab completion Set-PSReadLineKeyHandler -Key "Ctrl+ D "-Function MenuComplete # Set Ctrl+ D for menu completion and Intellisense Set-psreadlinekeyhandler-key "Ctrl+z" -function Undo # Set Ctrl+z to Undo set-psreadlineKeyhandler-key uparrow-function HistorySearchBackward # Set the up Key to backward-search history set-psReadlineKeyHandler -Key DownArrow -Function HistorySearchForward # Set the down key to forward search historyCopy the code

replaceoh-my-poshThe theme

Oh-my-posh has plenty of themes built in for us to use, which you can check out here by changing the Theme name after the previous set-theme command. The following themes are not pretty, you can customize the theme, in order to reduce the difficulty, here will not study in detail.

replacePowerShellThe font

Remember the garbled command shown earlier? Let’s change the font and fix it. Example Install the FantasqueSansMono-NF font (you can also use the system font, this step does not need to install).

> scoop search FantasqueSansMono-NF
> scoop bucket add 'nerd-fonts'
The next command will add the sudo option
> sudo scoop install FantasqueSansMono-NF
Copy the code

Once the fonts are installed, right click on the title bar, turn on the default Settings (defaults, not properties), select the fonts you just installed, and when you are sure, restart PowerShell and everything appears to be fine.

useColorToolCustom PowerShell text colors

Didn’t we configure the PowerShell theme using Oh-my-posh, why do we need ColorTool to change the color? In fact, oh-my-posh is changing the style of the content displayed, while ColorTool is changing the color of the text. When oh-my-Posh’s built-in colors don’t fit your needs, you can use ColorTool to change them. If OK, you can skip this step.

ColorTool, a Microsoft tool that changes the color of PowerShell text, is also very simple to use.

# Install Microsoft's official Color tool
> scoop install colortool

# Check out the built-in color scheme, there are 8 kinds
> colortool --schemes

# Set the theme followed by the color scheme name.
> colortool OneHalfDark.itermcolors
Copy the code

To enhancePowerShelllsfunction

Dircolors is a Linux command that sets the colors of directories or files displayed by the ls command. Similarly, if you want PowerShell to display directories in color, you can use the dircolors plug-in.

# installation DirColors
> Install-Module DirColors
Open PowerShell configuration
> code $PROFILE
Copy the code

Add a line at the end of the configuration file and restart PowerShell to see the colored directories.

Import-Module DirColors
Copy the code

Other plug-ins can be installed in the same way, so start exploring

Configure Windows Terminal

Start Windows Terminal after you’ve been around PowerShell for so long.

One of the most powerful features is the ability to customize themes, including: fonts, text colors, backgrounds, etc. The final configuration into what effect, depends on the aesthetic level. You can refer to the WIndows Terminal documentation for configuration. The following is my configuration process.

Open PowerShell 7 in Windows Terminal and you’ll see that it’s just as ugly. The previous configuration doesn’t seem to work because Windows Terminal has its own set of themes.

The configuration file

Windows Terminal has two configuration files. One is the default default.json, which cannot be modified. One is setting.json, which we are going to modify. Finding the configuration file is as simple as clicking the drop down arrow in the title bar and then clicking Settings, which opens the configuration with the default JSON editor.

If you want to open the default default.json, hold Alt while clicking Settings.

The previous version of Windows Terminal profile was called profile.json, so there are several articles on the web that discuss profile.json.

If you look closely at the two configuration files, their structure is the same. I divide it into four parts:

  • GlobalThe overall configuration, startup parameters, and styles of Windows Terminal APP are shown in the following figureGlobal Config.
  • profilesConfigure the style for each terminal.
  • schemesTerminal color scheme.
  • actionsDefine shortcut key operations, generally the default.

The following figure shows the reach of Global and Profiles. Setting. json overrides the same configuration of default.json. If you do not know how to configure setting.json, refer to the key-value value of each node in default.json. The main topic configuration is the Global, Profiles, and Schemes nodes.

profilesConfiguring terminal Styles

Profiles has the list node, which is an array representing the style of each terminal, so it is possible to make each terminal look different. For details on each node, see the official documentation.

Want to configure all terminal styles uniformly? Arrangement! You can modify the defaults node, key-value is the same as list.

{
    "profiles": {"defaults": {// For all terminals
        },
        "list":[
            {
                / / terminal 1
            },
            {
                 / / terminal 2}}}]Copy the code

Next we configure the PowerShell style and find the child node whose name=PowerShell.

The font color

The font color is defined in the Schemes node, just refer to the name of Shemes, Campbell by default, we change it to One Half Dark and immediately see the font color change.

{
   "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}"."hidden": false."name": "PowerShell"."source": "Windows.Terminal.PowershellCore"."colorScheme":"One Half Dark"
}
Copy the code

The font

Any system-installed font can be referenced, but it is important to support special characters, otherwise garbled characters like the one above will appear. We used the official recommended Font Cascadia Code (the Github attachment is hard to download, I have a backup, click here to download). When you unpack it, you’ll find a lot of fonts and you can install any you like. I installed the otf/static/CascadiaCodePL – Bold. Otf, the name of the font open font viewer.

{
	"guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}"."hidden": false."name": "PowerShell"."source": "Windows.Terminal.PowershellCore"."colorScheme":"One Half Dark"."fontFace":"Cascadia Code PL"
}
Copy the code

There are plenty of good fonts out there:

  • Sarasa many people recommend, support Chinese.
  • Jetbrains Mono Jetbrains official font.

The background image

You can customize the background image, alignment, image zoom, image transparency, etc.

{... // Image path, Note the slash escape "backgroundImage":"C:\\Users\\ Yulinyige \\Pictures\ dolaameng. Jfif ", / / zoom mode "backgroundImageStretchMode" : "uniformToFill", / / alignment "backgroundImageAlignment" : "center", // Opacity" backgroundImageOpacity":0.2}Copy the code

Frosted glass effect

Remember the frosted glass effect of Windows 7? In fact, I think it looks good, especially when clicking the gradual feedback effect, meat meat, in line with the operation habits. But in Windows 8, the interface was flat. Windows Terminal also supports frosted glass. Doesn’t frosted glass look a little prettier when you turn it on?

{... // Enable frosted glass effect "useAcrylic":true, // Frosted glass opacity "acrylicOpacity":0.5}Copy the code

schemesThe color scheme

Schemes are managed by Schemes, again an array, and each scheme has a name name, which is referenced by name. Several color schemes are preset by default, which can be viewed in default.json, detailed here.

Except for name, they are all matching colors. I can’t find the specific meaning of the color. The following color description comes from here.

// Typical schemes
{
    "schemes":[
        {
            "name": "Campbell".// The name of the color scheme, required
            "foreground": "#CCCCCC".// Display the font color
            "background": "#0C0C0C"./ / the background color
            "cursorColor": "#FFFFFF".// Cursor color
            "black": "#0C0C0C".// arrow left triangle, git directory. Git directory prompt arrow background prompt text
            "red": "#C50F1F".// After SSH, vim opens the text file and enters ordinary characters to display the text
            "green": "#13A10E".// a background arrow is displayed in the.git directory
            "yellow": "#C19C00".// Git directory branch arrow background prompt
            "blue": "#0037DA".// Directory arrow body
            "purple": "# 881798".// After SSH, vim and other tools open the file, such as {and} symbol body, git update after the display of branch arrow background prompt
            "cyan": "#3A96DD".// Quotes and internal characters
            "white": "#CCCCCC"./ / unknown
            "brightBlack": "# 767676".// CD, etc.. And *, and command parameter character colors
            "brightRed": "#E74856".Git status is displayed as an error command
            "brightGreen": "#16C60C".SSH user permissions are displayed
            "brightYellow": "#F9F1A5".// Enter the command character
            "brightBlue": "#3B78FF".// SSH folder highlight, SSH directory, vim open text file without entering line ~ character display
            "brightPurple": "#B4009E"./ / unknown
            "brightCyan": "#61D6D6".// SSH vim and other tools open the file after the {and} symbol background
            "brightWhite": "#F2F2F2" // Prompt text to the left and center of the directory arrow}}]Copy the code

How to find the right color match

Customizing color schemes can be tedious and time-consuming. The best way to do this is to use a color scheme made by someone else. You can find iterm2-color-schemes, there are more than 200 of them, pick the Color in Screenshots and copy the style in the json file corresponding to windwosterminal.

There are also sites where you can pick and match colors:

  • windowsterminalthemes.dev/
  • terminal.sexy/

CRT with X effect

CRT mode is like the green characters on black background in the Matrix, with pixels piled into the text, instantly back to the 80’s. Is it cool or nostalgic?

{ "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}", "hidden": false, "name": "PowerShell", "source": "Windows. Terminal. PowershellCore fontFace", "" :" Cascadia Code PL ", / / as long as add this way, can open bright blind effect. "experimental.retroTerminalEffect":true }Copy the code

Doraemon’s white color palette is A bit of A split. Look at the matrix’s final configuration, which actually changed all the colors to # 00D900.

{
    "profiles":
    {
        "list": [{"guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}"."hidden": false."name": "PowerShell"."source": "Windows.Terminal.PowershellCore"."colorScheme":"Zhuang B"."fontFace":"Cascadia Code PL"."experimental.retroTerminalEffect":true}},"schemes": [{"name": "Zhuang B"."black": "# 000000"."red": "#00d900"."green": "#00d900"."yellow": "#00d900"."blue": "#00d900"."purple": "#00d900"."cyan": "#00d900"."white": "#00d900"."brightBlack": "#00d900"."brightRed": "#00d900"."brightGreen": "#00d900"."brightYellow": "#00d900"."brightBlue": "#00d900"."brightPurple": "#00d900"."brightCyan": "#00d900"."brightWhite": "#00d900"."background": "# 000000"."foreground": "#00d900"}}]Copy the code

GlobalGlobal configuration

The global configuration is not studied one by one. For details, refer to the startup, Interaction, and appearance sections of the official documentation.

The default configuration is basically pretty good. One area worth changing is the default Terminal. Since the latest version of PowerShell is very powerful, we want Windows Terminal to be enabled by default. DefaultProfile to PowerShell GUID:

The last

In fact, the entire configuration process is not complicated (or maybe I’m being too verbose), and in the end it looks the same. But as a developer, there are two things to think about:

  • How does it unify all terminals into Windows Terminal?
  • How to develop a configurable application?