0%

WSL和windows的zsh终端配置教程

1 WSL终端配置

1.1 更换配色

背景rgb(0,43,53),文字rgb(147,161,161)

1.2 下载并安装字体FiraCode,右键属性选择字体为FiraCode

1.3 下载zsh

1
2
sudo apt-get update
sudo apt-get install zsh

1.4 重启wsl安装zsh

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

1.5 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 自定义主题
cp ~/.oh-my-zsh/themes/agnoster.zsh-theme ~/.oh-my-zsh/custom/themes/agnoster_wsl.zsh-theme

# 找到下面内容,并将blue替换为0.75
# Dir: current working directory
prompt_dir() {
prompt_segment blue $CURRENT_FG '%~'
}

# 配置主题
vim ~/.zshrc
ZSH_THEME="agnoster_wsl"
# 隐藏用户名+主机名
DEFAULTUSER='cetrol

2 powershell终端配置

2.1 管理员运行powershell

1
2
# 允许powershell运行任何脚本
Set-ExecutionPolicy Bypass

2.2 下载chocolatey安装包管理工具

1
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

2.3 安装colortools修改配色

1
2
3
4
5
choco install colortool
# 查看所有配色
colortool -s
# 设置配色
colortool 配色名字

2.4 安装oh-my-posh

1
2
Install-Module posh-git -Scope CurrentUser
Install-Module oh-my-posh -Scope CurrentUser

2.5 设置主题(熟悉的用户直接到第7步即可)

1
2
3
Import-Module posh-git
Import-Module oh-my-posh
Set-Theme Agnoster

2.6 隐藏用户名主机名

1
$DefaultUser = 'cetrol'

2.7 配置字体

  • 下载powerline字体并安装

  • 在powershell中运行下面代码后,右键属性里才可以看到安装的字体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
Install-Module -Name PSReadLine -Force -SkipPublisherCheck
if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force }
@"
#requires -Version 2 -Modules posh-git

function Write-Theme {
param(
[bool]
`$lastCommandFailed,
[string]
`$with
)

`$lastColor = `$sl.Colors.PromptBackgroundColor
`$prompt = Write-Prompt -Object `$sl.PromptSymbols.StartSymbol -ForegroundColor `$sl.Colors.PromptForegroundColor -BackgroundColor `$sl.Colors.SessionInfoBackgroundColor

#check the last command state and indicate if failed
If (`$lastCommandFailed) {
`$prompt += Write-Prompt -Object "`$(`$sl.PromptSymbols.FailedCommandSymbol) " -ForegroundColor `$sl.Colors.CommandFailedIconForegroundColor -BackgroundColor `$sl.Colors.SessionInfoBackgroundColor
}

#check for elevated prompt
If (Test-Administrator) {
`$prompt += Write-Prompt -Object "`$(`$sl.PromptSymbols.ElevatedSymbol) " -ForegroundColor `$sl.Colors.AdminIconForegroundColor -BackgroundColor `$sl.Colors.SessionInfoBackgroundColor
}

`$user = [System.Environment]::UserName
`$computer = [System.Environment]::MachineName
`$path = Get-FullPath -dir `$pwd
if (Test-NotDefaultUser(`$user)) {
`$prompt += Write-Prompt -Object "`$user@`$computer " -ForegroundColor `$sl.Colors.SessionInfoForegroundColor -BackgroundColor `$sl.Colors.SessionInfoBackgroundColor
}

if (Test-VirtualEnv) {
`$prompt += Write-Prompt -Object "`$(`$sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor `$sl.Colors.SessionInfoBackgroundColor -BackgroundColor `$sl.Colors.VirtualEnvBackgroundColor
`$prompt += Write-Prompt -Object "`$(`$sl.PromptSymbols.VirtualEnvSymbol) `$(Get-VirtualEnvName) " -ForegroundColor `$sl.Colors.VirtualEnvForegroundColor -BackgroundColor `$sl.Colors.VirtualEnvBackgroundColor
`$prompt += Write-Prompt -Object "`$(`$sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor `$sl.Colors.VirtualEnvBackgroundColor -BackgroundColor `$sl.Colors.PromptBackgroundColor
}
else {
`$prompt += Write-Prompt -Object "`$(`$sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor `$sl.Colors.SessionInfoBackgroundColor -BackgroundColor `$sl.Colors.PromptBackgroundColor
}

## Writes the drive portion
`$prompt += Write-Prompt -Object "`$path " -ForegroundColor `$sl.Colors.PromptForegroundColor -BackgroundColor `$sl.Colors.PromptBackgroundColor

`$status = Get-VCSStatus
if (`$status) {
`$themeInfo = Get-VcsInfo -status (`$status)
`$lastColor = `$themeInfo.BackgroundColor
`$prompt += Write-Prompt -Object `$(`$sl.PromptSymbols.SegmentForwardSymbol) -ForegroundColor `$sl.Colors.PromptBackgroundColor -BackgroundColor `$lastColor
`$prompt += Write-Prompt -Object " `$(`$themeInfo.VcInfo) " -BackgroundColor `$lastColor -ForegroundColor `$sl.Colors.GitForegroundColor
}

## Writes the postfix to the prompt
`$prompt += Write-Prompt -Object `$sl.PromptSymbols.SegmentForwardSymbol -ForegroundColor `$lastColor

`$timeStamp = Get-Date -UFormat %r
`$timestamp = "[`$timeStamp]"

`$prompt += Set-CursorForRightBlockWrite -textLength (`$timestamp.Length + 1)
`$prompt += Write-Prompt `$timeStamp -ForegroundColor `$sl.Colors.PromptForegroundColor

`$prompt += Set-Newline

if (`$with) {
`$prompt += Write-Prompt -Object "`$(`$with.ToUpper()) " -BackgroundColor `$sl.Colors.WithBackgroundColor -ForegroundColor `$sl.Colors.WithForegroundColor
}
`$prompt += Write-Prompt -Object (`$sl.PromptSymbols.PromptIndicator) -ForegroundColor `$sl.Colors.PromptBackgroundColor
`$prompt += ' '
`$prompt
}

`$sl = `$global:ThemeSettings #local settings
`$sl.PromptSymbols.StartSymbol = ''
`$sl.PromptSymbols.PromptIndicator = [char]::ConvertFromUtf32(0x276F)
`$sl.PromptSymbols.SegmentForwardSymbol = [char]::ConvertFromUtf32(0xE0B0)
`$sl.Colors.PromptForegroundColor = [ConsoleColor]::White
`$sl.Colors.PromptSymbolColor = [ConsoleColor]::White
`$sl.Colors.PromptHighlightColor = [ConsoleColor]::DarkBlue
`$sl.Colors.GitForegroundColor = [ConsoleColor]::Black
`$sl.Colors.WithForegroundColor = [ConsoleColor]::DarkRed
`$sl.Colors.WithBackgroundColor = [ConsoleColor]::Magenta
`$sl.Colors.VirtualEnvBackgroundColor = [System.ConsoleColor]::Red
`$sl.Colors.VirtualEnvForegroundColor = [System.ConsoleColor]::White
"@>$env:userprofile"\Documents\WindowsPowerShell\Modules\oh-my-posh\2.0.263\Themes\Paradox.psm1" # 建议自己把代码放到记事本然后替换路径,此条路径要改。。
@"
chcp 65001
Set-PSReadlineOption -EditMode Emacs
function which($name) { Get-Command $name | Select-Object Definition }
function rmrf($item) { Remove-Item $item -Recurse -Force }
function mkfile($file) { "" | Out-File $file -Encoding ASCII }
Import-Module posh-git
Import-Module oh-my-posh
Set-Theme Agnoster
$DefaultUser = 'cetrol'

## Chocolatey profile
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
Import-Module "$ChocolateyProfile"
}

3 Vscode终端配置

  • 设置->用户->功能->终端 设置可以正确显示Agnoster的字体,比如fira code retina字体