Windows 10 使用 PowerShell 7 + Windows Terminal 及乱码、不显示 python环境问题
目錄
- 1. Windows Terminal
- 1.1 安裝 Windows Terminal
- 1.2 安裝字體
- 1.3 配置 Windows Terminal
- 2. Power Shell 7
- 2.1 安裝 Power Shell
- 2.2 配置 Power Shell
- 3. 疑難雜癥
- 3.1 安裝字體后 Poweline 仍然亂碼
- 3.2 Powerline 不顯示 Python 虛擬環(huán)境
- 3.3 去掉主機(jī)名
- 3.4 遺留問(wèn)題
- 參考文獻(xiàn)
寫(xiě)這篇文章純屬偶然,習(xí)慣 Mac上使用 iTerm2 + oh-my-zsh,也希望 Windows上有好用的 Terminal,雖然知道有 Windows Terminal,但懶的折騰也就沒(méi)整。不過(guò)最近電腦頻繁死機(jī),不得以重裝后,順便把 Windows Terminal搞了,不過(guò)遇到各種亂七八糟的問(wèn)題,在這里跟大分享一下。
- 注意:本文各部分不完全是安裝過(guò)程,建議大家把所有的軟件安裝完后,再對(duì)各部進(jìn)行配置
1. Windows Terminal
1.1 安裝 Windows Terminal
從Microsoft App Store 安裝Windows Terminal。
1.2 安裝字體
在 NERD FONTS 中選擇任意你喜歡的字體并進(jìn)行安裝,注意: 需要與后面相關(guān)的字體設(shè)置相匹配
安裝方法:
1.3 配置 Windows Terminal
- 打開(kāi)Windows Terminal的配置文件
- 配置默認(rèn)設(shè)置部分
“defaultProfile”: 將 Power Shell 7 設(shè)置為默認(rèn)終端
“profiles” :設(shè)置外觀、字體、主題等
“useAcrylic”:用于設(shè)置毛玻璃特效
“colorScheme”:設(shè)置主題
“face”:設(shè)置字體
- 設(shè)置 Windows Terminal 主題
本文主題為經(jīng)他人修改的 Homebrew 主題
將該配置文件放置于 "schemes": 項(xiàng)目下
2. Power Shell 7
2.1 安裝 Power Shell
在 Power Shell 的 GitHub 項(xiàng)目中選擇適合自己的 Power Shell 進(jìn)行安裝。
2.2 配置 Power Shell
- 安裝 Powe Shell 插件
打開(kāi) Windows Terminal ,進(jìn)入 Power Shell
# 1. 安裝 PSReadline 包,該插件可以讓命令行很好用,類(lèi)似 zsh Install-Module -Name PSReadLine -Scope CurrentUser# 2. 安裝 posh-git 包,讓你的 git 更好用 Install-Module posh-git -Scope CurrentUser# 3. 安裝 oh-my-posh 包,讓你的命令行更酷炫、優(yōu)雅 Install-Module oh-my-posh -Scope CurrentUser- 修改 Power Shell 的配置文件
Set-PoshPrompt :用于設(shè)置 oh-my-posh 主題
code $Profile <#* FileName: Microsoft.PowerShell_profile.ps1* Author: 劉 鵬* Email: littleNewton6@outlook.com* 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# 設(shè)置 PowerShell 主題 # Set-PoshPrompt ys Set-PoshPrompt paradox #------------------------------- Import Modules END -------------------------------#------------------------------- Set Hot-keys BEGIN ------------------------------- # 設(shè)置預(yù)測(cè)文本來(lái)源為歷史記錄 Set-PSReadLineOption -PredictionSource History# 每次回溯輸入歷史,光標(biāo)定位于輸入內(nèi)容末尾 Set-PSReadLineOption -HistorySearchCursorMovesToEnd# 設(shè)置 Tab 為菜單補(bǔ)全和 Intellisense Set-PSReadLineKeyHandler -Key "Tab" -Function MenuComplete# 設(shè)置 Ctrl+d 為退出 PowerShell Set-PSReadlineKeyHandler -Key "Ctrl+d" -Function ViExit# 設(shè)置 Ctrl+z 為撤銷(xiāo) Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo# 設(shè)置向上鍵為后向搜索歷史記錄 Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward# 設(shè)置向下鍵為前向搜索歷史紀(jì)錄 Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward #------------------------------- Set Hot-keys END -------------------------------#------------------------------- Functions BEGIN ------------------------------- # Python 直接執(zhí)行 $env:PATHEXT += ";.py"# 更新系統(tǒng)組件 function Update-Packages {# update pipWrite-Host "Step 1: 更新 pip" -ForegroundColor Magenta -BackgroundColor Cyan$a = pip list --outdated$num_package = $a.Length - 2for ($i = 0; $i -lt $num_package; $i++) {$tmp = ($a[2 + $i].Split(" "))[0]pip install -U $tmp}# update TeX Live$CurrentYear = Get-Date -Format yyyyWrite-Host "Step 2: 更新 TeX Live" $CurrentYear -ForegroundColor Magenta -BackgroundColor Cyantlmgr update --selftlmgr update --all# update ChocoloteyWrite-Host "Step 3: 更新 Chocolatey" -ForegroundColor Magenta -BackgroundColor Cyanchoco outdated } #------------------------------- Functions END -------------------------------#------------------------------- Set Alias BEGIN ------------------------------- # 1. 編譯函數(shù) make function MakeThings {nmake.exe $args -nologo } Set-Alias -Name make -Value MakeThings# 2. 更新系統(tǒng) os-update Set-Alias -Name os-update -Value Update-Packages# 3. 查看目錄 ls & ll function ListDirectory {(Get-ChildItem).NameWrite-Host("") } Set-Alias -Name ls -Value ListDirectory Set-Alias -Name ll -Value Get-ChildItem# 4. 打開(kāi)當(dāng)前工作目錄 function OpenCurrentFolder {param(# 輸入要打開(kāi)的路徑# 用法示例:open C:\# 默認(rèn)路徑:當(dāng)前工作文件夾$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 關(guān)鍵路由 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 關(guān)鍵路由 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個(gè)自己喜歡的主題,并對(duì) Set-PoshPrompt 進(jìn)行修改
Get-PoshThemes3. 疑難雜癥
3.1 安裝字體后 Poweline 仍然亂碼
- 這主要與字體選擇有關(guān),安裝自己喜歡的字體后,在選擇字體時(shí)請(qǐng)使以 NF 結(jié)尾的字體,例如:JetBrainsMono NF。
3.2 Powerline 不顯示 Python 虛擬環(huán)境
- 個(gè)人感覺(jué)這大概是 oh-my-posh 的 bug,因?yàn)椴粏⒂?oh-my-posh 時(shí),會(huì)默認(rèn)顯示 python 的虛擬環(huán)境。
- 開(kāi)啟 oh-my-posh 主題后,python 虛擬環(huán)境不再顯示。
- 解決這個(gè)問(wèn)題需要修改 oh-my-posh 相應(yīng)主題的配置文件
- 解決的方案有兩個(gè),一是修改 oh-my-posh 對(duì)應(yīng)主題的 python 模塊,二是在對(duì)應(yīng)主題添加 Environment Variable 模塊
- 主路徑應(yīng)該在 xxxxxxx\Documents\PowerShell\Modules\oh-my-posh\5.7.0\themes
- 方法一
- 方法二
找到 anaconda 的環(huán)境變量
dir env:在 properties 配置相應(yīng)的環(huán)境變量
{"type": "envvar","style": "powerline","powerline_symbol": "\uE0B0","foreground": "#193549","background": "#FF6E6E","properties": {"prefix": "\uE235","var_name": "CONDA_DEFAULT_ENV"}},3.3 去掉主機(jī)名
- 去掉主機(jī)名需要修改對(duì)應(yīng)主題的 "session"模塊
禁用 "display_host" 即可
{"type": "session","style": "powerline","powerline_symbol": "\uE0B0","foreground": "#100e23","background": "#ffffff","properties": {"display_user": true,"display_host": false}},3.4 遺留問(wèn)題
- 仔細(xì)觀察會(huì)發(fā)現(xiàn) oh-my-posh 左側(cè)會(huì)有一段空白,以前在 Vim 中是通過(guò)換適版本的主題解決的,但是現(xiàn)在不知道入何解決了
參考文獻(xiàn)
[1] Oh my Posh 3——易于自定義主題的Powershell美化工具
[2] Windows Terminal 完美配置 PowerShell 7.1
[3] PowerShell 界面美化
[4] Windows Terminal + oh-my-posh模塊美化官方教程集錦以及常見(jiàn)問(wèn)題(問(wèn)題收集中)
[5] 簡(jiǎn)單配置與美化Powershell和Terminal
[6] Powershell 主題美化學(xué)習(xí)過(guò)程
總結(jié)
以上是生活随笔為你收集整理的Windows 10 使用 PowerShell 7 + Windows Terminal 及乱码、不显示 python环境问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 为玩客云或树莓派安装LNMP和PhpMy
- 下一篇: 2017年第23届中国东北国际建筑装饰博