Windows 10 使用 PowerShell 7 + Windows Terminal 及乱码、不显示 python环境问题


写这篇文章纯属偶然,习惯 Mac上使用 iTerm2 + oh-my-zsh,也希望 Windows上有好用的 Terminal,虽然知道有 Windows Terminal,但懒的折腾也就没整。不过最近电脑频繁死机,不得以重装后,顺便把 Windows Terminal搞了,不过遇到各种乱七八糟的问题,在这里跟大分享一下。

  • 注意:本文各部分不完全是安装过程,建议大家把所有的软件安装完后,再对各部进行配置

1. Windows Terminal

1.1 安装 Windows Terminal

Microsoft App Store 安装Windows Terminal
在这里插入图片描述

1.2 安装字体

NERD FONTS 中选择任意你喜欢的字体并进行安装,注意: 需要与后面相关的字体设置相匹配

安装方法:

  1. 下载相应的字体并解压缩
  2. 选中所有的字体
  3. 右击选择 “安装”

在这里插入图片描述

1.3 配置 Windows Terminal

  • 打开Windows Terminal的配置文件
    在这里插入图片描述

在这里插入图片描述

  • 配置默认设置部分

“defaultProfile”: 将 Power Shell 7 设置为默认终端
“profiles” :设置外观、字体、主题等
“useAcrylic”:用于设置毛玻璃特效
“colorScheme”:设置主题
“face”:设置字体

    "defaultProfile": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
    "profiles": 
    {
        "defaults": 
        {
            "acrylicOpacity": 0.5,
            "closeOnExit": "graceful",
            "colorScheme": "Homebrew",
            "cursorColor": "#FFFFFF",
            "cursorShape": "bar",
            "font": 
            {
                "face": "JetBrainsMono NF",
                "size": 11
            },
            "hidden": false,
            "historySize": 9001,
            "padding": "5, 5, 20, 25",
            "snapOnInput": true,
            "startingDirectory": ".",
            "useAcrylic": false
        },
  • 设置 Windows Terminal 主题

本文主题为经他人修改的 Homebrew 主题
将该配置文件放置于 "schemes": 项目下

{
            "background": "#283033",
            "black": "#000000",
            "blue": "#6666E9",
            "brightBlack": "#666666",
            "brightBlue": "#0000FF",
            "brightCyan": "#00E5E5",
            "brightGreen": "#00D900",
            "brightPurple": "#E500E5",
            "brightRed": "#E50000",
            "brightWhite": "#E5E5E5",
            "brightYellow": "#E5E500",
            "cursorColor": "#FFFFFF",
            "cyan": "#00A6B2",
            "foreground": "#00FF00",
            "green": "#00A600",
            "name": "Homebrew",
            "purple": "#B200B2",
            "red": "#FC5275",
            "selectionBackground": "#FFFFFF",
            "white": "#BFBFBF",
            "yellow": "#999900"
        },

2. Power Shell 7

2.1 安装 Power Shell

Power ShellGitHub 项目中选择适合自己的 Power Shell 进行安装。

2.2 配置 Power Shell

  • 安装 Powe Shell 插件

打开 Windows Terminal ,进入 Power Shell

# 1. 安装 PSReadline 包,该插件可以让命令行很好用,类似 zsh
Install-Module -Name PSReadLine  -Scope CurrentUser

# 2. 安装 posh-git 包,让你的 git 更好用
Install-Module posh-git  -Scope CurrentUser

# 3. 安装 oh-my-posh 包,让你的命令行更酷炫、优雅
Install-Module oh-my-posh -Scope CurrentUser
  • 修改 Power Shell 的配置文件

Set-PoshPrompt :用于设置 oh-my-posh 主题

code $Profile
<#
 * FileName: Microsoft.PowerShell_profile.ps1
 * Author: 刘 鹏
 * Email: [email protected]
 * Date: 2021, Aug. 21
 * Copyright: No copyright. You can use this code for anything with no warranty.
#>


#------------------------------- Import Modules BEGIN -------------------------------
# 引入 posh-git
Import-Module posh-git

# 引入 oh-my-posh
Import-Module oh-my-posh

# 引入 ps-read-line
Import-Module PSReadLine

# 设置 PowerShell 主题
# Set-PoshPrompt ys
Set-PoshPrompt paradox
#------------------------------- Import Modules END   -------------------------------





#-------------------------------  Set Hot-keys BEGIN  -------------------------------
# 设置预测文本来源为历史记录
Set-PSReadLineOption -PredictionSource History

# 每次回溯输入历史,光标定位于输入内容末尾
Set-PSReadLineOption -HistorySearchCursorMovesToEnd

# 设置 Tab 为菜单补全和 Intellisense
Set-PSReadLineKeyHandler -Key "Tab" -Function MenuComplete

# 设置 Ctrl+d 为退出 PowerShell
Set-PSReadlineKeyHandler -Key "Ctrl+d" -Function ViExit

# 设置 Ctrl+z 为撤销
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo

# 设置向上键为后向搜索历史记录
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward

# 设置向下键为前向搜索历史纪录
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
#-------------------------------  Set Hot-keys END    -------------------------------





#-------------------------------    Functions BEGIN   -------------------------------
# Python 直接执行
$env:PATHEXT += ";.py"

# 更新系统组件
function Update-Packages {
	# update pip
	Write-Host "Step 1: 更新 pip" -ForegroundColor Magenta -BackgroundColor Cyan
	$a = pip list --outdated
	$num_package = $a.Length - 2
	for ($i = 0; $i -lt $num_package; $i++) {
		$tmp = ($a[2 + $i].Split(" "))[0]
		pip install -U $tmp
	}

	# update TeX Live
	$CurrentYear = Get-Date -Format yyyy
	Write-Host "Step 2: 更新 TeX Live" $CurrentYear -ForegroundColor Magenta -BackgroundColor Cyan
	tlmgr update --self
	tlmgr update --all

	# update Chocolotey
	Write-Host "Step 3: 更新 Chocolatey" -ForegroundColor Magenta -BackgroundColor Cyan
	choco outdated
}
#-------------------------------    Functions END     -------------------------------





#-------------------------------   Set Alias BEGIN    -------------------------------
# 1. 编译函数 make
function MakeThings {
	nmake.exe $args -nologo
}
Set-Alias -Name make -Value MakeThings

# 2. 更新系统 os-update
Set-Alias -Name os-update -Value Update-Packages

# 3. 查看目录 ls & ll
function ListDirectory {
	(Get-ChildItem).Name
	Write-Host("")
}
Set-Alias -Name ls -Value ListDirectory
Set-Alias -Name ll -Value Get-ChildItem

# 4. 打开当前工作目录
function OpenCurrentFolder {
	param
	(
		# 输入要打开的路径
		# 用法示例:open C:\
		# 默认路径:当前工作文件夹
		$Path = '.'
	)
	Invoke-Item $Path
}
Set-Alias -Name open -Value OpenCurrentFolder
#-------------------------------    Set Alias END     -------------------------------





#-------------------------------   Set Network BEGIN    -------------------------------
# 1. 获取所有 Network Interface
function Get-AllNic {
	Get-NetAdapter | Sort-Object -Property MacAddress
}
Set-Alias -Name getnic -Value Get-AllNic

# 2. 获取 IPv4 关键路由
function Get-IPv4Routes {
	Get-NetRoute -AddressFamily IPv4 | Where-Object -FilterScript {$_.NextHop -ne '0.0.0.0'}
}
Set-Alias -Name getip -Value Get-IPv4Routes

# 3. 获取 IPv6 关键路由
function Get-IPv6Routes {
	Get-NetRoute -AddressFamily IPv6 | Where-Object -FilterScript {$_.NextHop -ne '::'}
}
Set-Alias -Name getip6 -Value Get-IPv6Routes
#-------------------------------    Set Network END     -------------------------------


  • 查看 oh-my-posh 主题

选1个自己喜欢的主题,并对 Set-PoshPrompt 进行修改

Get-PoshThemes

在这里插入图片描述

3. 疑难杂症

3.1 安装字体后 Poweline 仍然乱码

  • 这主要与字体选择有关,安装自己喜欢的字体后,在选择字体时请使以 NF 结尾的字体,例如:JetBrainsMono NF

3.2 Powerline 不显示 Python 虚拟环境

  • 个人感觉这大概是 oh-my-poshbug,因为不启用 oh-my-posh 时,会默认显示 python 的虚拟环境。
    在这里插入图片描述
  • 开启 oh-my-posh 主题后,python 虚拟环境不再显示。
    在这里插入图片描述
  • 解决这个问题需要修改 oh-my-posh 相应主题的配置文件
  • 解决的方案有两个,一是修改 oh-my-posh 对应主题的 python 模块,二是在对应主题添加 Environment Variable 模块
  • 主路径应该在 xxxxxxx\Documents\PowerShell\Modules\oh-my-posh\5.7.0\themes
  • 方法一
{
          "type": "python",
          "style": "powerline",
          "powerline_symbol": "\uE0B0",
          "foreground": "#100e23",
          "background": "#906cff",
          "properties": {
            "prefix": " \uE235 ",
            "display_virtual_env": true,
            "display_default": true,
            "display_mode": "context"
          }
        },

在这里插入图片描述

  • 方法二

找到 anaconda 的环境变量

dir env:

在这里插入图片描述

properties 配置相应的环境变量

{
          "type": "envvar",
          "style": "powerline",
          "powerline_symbol": "\uE0B0",
          "foreground": "#193549",
          "background": "#FF6E6E",
          "properties": {
            "prefix": "\uE235",
            "var_name": "CONDA_DEFAULT_ENV"
          }
        },

在这里插入图片描述

3.3 去掉主机名

  • 去掉主机名需要修改对应主题的 "session"模块

禁用 "display_host" 即可

{
          "type": "session",
          "style": "powerline",
          "powerline_symbol": "\uE0B0",
          "foreground": "#100e23",
          "background": "#ffffff",
          "properties": {
            "display_user": true,
            "display_host": false
          }
        },

3.4 遗留问题

  • 仔细观察会发现 oh-my-posh 左侧会有一段空白,以前在 Vim 中是通过换适版本的主题解决的,但是现在不知道入何解决了
    在这里插入图片描述

参考文献

[1] Oh my Posh 3——易于自定义主题的Powershell美化工具
[2] Windows Terminal 完美配置 PowerShell 7.1
[3] PowerShell 界面美化
[4] Windows Terminal + oh-my-posh模块美化官方教程集锦以及常见问题(问题收集中)
[5] 简单配置与美化Powershell和Terminal
[6] Powershell 主题美化学习过程

猜你喜欢

转载自blog.csdn.net/Y1575071736/article/details/120616233