如何在控制台中关闭语法高亮?

Sim*_*xca 10 powershell syntax-highlighting powershell-5.0 psreadline

我只想让PowerShell成为白色背景上的黑色文字.但是,PowerShell v5突出显示了我的命令,并使它们变黄,这是不可能看到的.有没有办法在PowerShell中关闭所有语法高亮?

Ans*_*ers 10

PowerShell v5中的语法着色可以通过修改Set-PSReadlineOption.以下命令将注释的foregound和背景颜色设置为shell前景色和背景色:

Set-PSReadlineOption -TokenKind Comment -ForegroundColor $Host.UI.RawUI.ForegroundColor -BackgroundColor $Host.UI.RawUI.BackgroundColor
Run Code Online (Sandbox Code Playgroud)

或者只是黑白:

Set-PSReadlineOption -TokenKind Comment -ForegroundColor Black -BackgroundColor White
Run Code Online (Sandbox Code Playgroud)

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

如果您还想更改输出流颜色,可以通过主机PrivateData对象的属性执行此操作:

$Host.PrivateData.WarningForegroundColor = $Host.UI.RawUI.ForegroundColor
$Host.PrivateData.WarningBackgroundColor = $Host.UI.RawUI.BackgroundColor
...
Run Code Online (Sandbox Code Playgroud)

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

$HOME\Documents\WindowsPowerShell\profile.ps1
Run Code Online (Sandbox Code Playgroud)

  • 感谢这个和所有其他答案。PS 没有提供一种更简单的方法来简单地关闭语法突出显示并使用窗口的默认颜色作为文本和背景,这简直是疯了。 (2认同)

aus*_*man 7

https://learn.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'
}
Run Code Online (Sandbox Code Playgroud)


小智 5

示例,如何关闭所有语法突出显示:

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"
Run Code Online (Sandbox Code Playgroud)

查看截图(Windows10)


tri*_*eee 5

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

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                      
Run Code Online (Sandbox Code Playgroud)

或者

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
                                 
Run Code Online (Sandbox Code Playgroud)

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

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'}
Run Code Online (Sandbox Code Playgroud)

如果你得到

Set-PSReadLineOption: 'None' is not a valid color property
Run Code Online (Sandbox Code Playgroud)

(这显然意味着你在 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'}    
Run Code Online (Sandbox Code Playgroud)

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