如何关闭控制台中的语法突出显示?

How to turn off syntax highlighting in console?

我只希望 PowerShell 是白底黑字。但是,PowerShell v5 会突出显示我的命令并将它们变成黄色,这是无法看到的。有没有办法关闭 PowerShell 中的所有语法突出显示?

可以通过 Set-PSReadlineOption 修改 PowerShell v5 中的语法着色。以下命令将评论的前景色和背景色设置为 shell 前景色和背景色:

Set-PSReadlineOption -TokenKind Comment -ForegroundColor $Host.UI.RawUI.ForegroundColor -BackgroundColor $Host.UI.RawUI.BackgroundColor

或者只是黑白:

Set-PSReadlineOption -TokenKind Comment -ForegroundColor Black -BackgroundColor White

您需要对所有 TokenKind 值执行此操作以完全删除语法着色。

如果您还想更改输出流颜色,您可以通过主机的 PrivateData 对象的属性来实现:

$Host.PrivateData.WarningForegroundColor = $Host.UI.RawUI.ForegroundColor
$Host.PrivateData.WarningBackgroundColor = $Host.UI.RawUI.BackgroundColor
...

将所有这些语句放入您的 profile 中,以便在您每次启动 PowerShell 时应用它们,例如:

$HOME\Documents\WindowsPowerShell\profile.ps1

示例,如何关闭所有语法高亮:

Set-PSReadlineOption -TokenKind Parameter -ForegroundColor DarkYellow -BackgroundColor DarkMagenta
Set-PSReadlineOption -TokenKind String -ForegroundColor DarkYellow -BackgroundColor DarkMagenta
Set-PSReadlineOption -TokenKind Operator -ForegroundColor DarkYellow -BackgroundColor DarkMagenta
Set-PSReadlineOption -TokenKind Type -ForegroundColor DarkYellow -BackgroundColor DarkMagenta
Set-PSReadlineOption -TokenKind Variable -ForegroundColor DarkYellow -BackgroundColor DarkMagenta
Set-PSReadlineOption -TokenKind Number -ForegroundColor DarkYellow -BackgroundColor DarkMagenta
Set-PSReadlineOption -TokenKind Member -ForegroundColor DarkYellow -BackgroundColor DarkMagenta
Set-PSReadlineOption -TokenKind Command -ForegroundColor DarkYellow -BackgroundColor DarkMagenta
Set-PSReadlineOption -TokenKind Comment -ForegroundColor DarkYellow -BackgroundColor DarkMagenta
Set-PSReadlineOption -TokenKind Keyword -ForegroundColor DarkYellow -BackgroundColor DarkMagenta
Set-PSReadlineOption -ContinuationPromptForegroundColor DarkYellow -ContinuationPromptBackgroundColor DarkMagenta
Set-PSReadlineOption -EmphasisForegroundColor DarkYellow -EmphasisBackgroundColor DarkMagenta
Set-PSReadlineOption -ErrorForegroundColor DarkYellow -ErrorBackgroundColor DarkMagenta

(Get-Host).PrivateData.ErrorForegroundColor="DarkYellow"
(Get-Host).PrivateData.ErrorBackgroundColor="DarkMagenta"
(Get-Host).PrivateData.WarningForegroundColor="DarkYellow"
(Get-Host).PrivateData.WarningBackgroundColor="DarkMagenta"
(Get-Host).PrivateData.DebugForegroundColor="DarkYellow"
(Get-Host).PrivateData.DebugBackgroundColor="DarkMagenta"
(Get-Host).PrivateData.VerboseForegroundColor="DarkYellow"
(Get-Host).PrivateData.VerboseBackgroundColor="DarkMagenta"
(Get-Host).PrivateData.ProgressForegroundColor="DarkYellow"
(Get-Host).PrivateData.ProgressBackgroundColor="DarkMagenta"

See screenshot (Windows10)

以下是我在 osx 中处理立即困扰我的事情的方法:

$a = get-psreadlineoption | select ErrorBackgroundColor
$clear = $a.ErrorBackgroundColor

'command','number','operator','member' |
foreach { set-psreadlineoption $_ black $clear }
get-help set-psreadlineoption

https://docs.microsoft.com/en-us/powershell/module/PSReadline/Set-PSReadlineOption

'None', 'Comment', 'Keyword', 'String', 'Operator', 
'Variable', 'Command', 'Parameter', 'Type', 'Number', 'Member' |
foreach { set-psreadlineoption $_  black white }

语法在最近的更新中发生了变化。旧语法现在会给你一个讨厌的错误信息:

Set-PSReadLineOption : A positional parameter cannot be found that accepts argument 'Command'.                                                             
At line:1 char:1                                                                
+ Set-PSReadLineOption 'Command' white black                                    
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                    
    + CategoryInfo          : InvalidArgument: (:) [Set-PSReadLineOption], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.SetPSReadLineOption                      

Set-PSReadLineOption : A parameter cannot be found that matches parameter name  'TokenKind'.
At line:1 char:22
+ Set-PSReadlineOption -TokenKind Comment -ForegroundColor 'black' -Bac ...
+                      ~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-PSReadLineOption], Par ameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.SetP SReadLineOption
                                 

更新后的语法似乎要求您传入新设置的字典。

Set-PSReadLineOption -Colors @{None='black';Comment='black';Keyword='black';String='black';Operator='black';Variable='black';Command='black';Parameter='black';Type='black';Number='black';Member='black'}

如果你得到

Set-PSReadLineOption: 'None' is not a valid color property

(这显然意味着你在 Linux),取出 None='black';,像这样:

Set-PSReadLineOption -Colors @{Comment='black';Keyword='black';String='black';Operator='black';Variable='black';Command='black';Parameter='black';Type='black';Number='black';Member='black'}    

另见 https://github.com/PowerShell/PSReadLine/issues/738

https://docs.microsoft.com/en-us/powershell/module/psreadline/set-psreadlineoption?view=powershell-7.1#example-4--set-multiple-color-options

Set-PSReadLineOption -Colors @{
  Command            = 'White'
  Number             = 'White'
  Member             = 'White'
  Operator           = 'White'
  Type               = 'White'
  Variable           = 'White'
  Parameter          = 'White'
  ContinuationPrompt = 'White'
  Default            = 'White'
}