如何在 PowerShell 中的 PSReadLine 中设置颜色

dan*_*gph 8 powershell

PowerShell 中的PSReadLine具有语法着色,但如何指定颜色?

Jas*_*irk 8

有几种不同的方式。你可以这样做:

Set-PSReadlineOption -TokenKind Comment -ForegroundColor Green
Run Code Online (Sandbox Code Playgroud)

或者:

$options = Get-PSReadlineOption
$options.CommentForegroundColor = Green
Run Code Online (Sandbox Code Playgroud)

可能的颜色来自 .NET ConsoleColor Enumeration。实际的 RGB 颜色值可以在控制台属性对话框中更改。

要查看当前的颜色设置,请Get-PSReadlineOption自行执行。


Tat*_*eva 6

虽然 Jason Shirk 描述的方法仍然适用于我的 PowerShell Core 版本 6.0.1 的 Mac,如此处所述,但它们不再适用于我的 Linux 机器(版本 6.1.0)。

他们似乎完全改变了这个 cmdlet 的界面:PowerShell 6 Set-PSReadlineOption

现在,您可以提供一个颜色哈希表作为-Colors参数的值。好消息是你现在有更多的颜色选择。

从示例中:

$colors = @{
  # ConsoleColor enum has all the old colors
  "Error" = [ConsoleColor]::DarkRed

  # A mustardy 24 bit color escape sequence
  "String" = "$([char]0x1b)[38;5;100m"

  # A light slate blue RGB value
  "Command" = "#8470FF"
}

Set-PSReadLineOption -Colors $colors
Run Code Online (Sandbox Code Playgroud)