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

dfddblmv  于 12个月前  发布在  Shell
关注(0)|答案(6)|浏览(184)

我只是希望PowerShell是白色背景上的黑色文本。然而,PowerShell v5突出显示我的命令并使它们变成黄色,这是不可能看到的。有没有办法在PowerShell中关闭所有语法突出显示?

5t7ly7z5

5t7ly7z51#

PowerShell v5中的注解着色可以通过Set-PSReadlineOption修改。以下命令将注解的前景和背景颜色设置为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

33qvvth1

33qvvth12#

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

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


取出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

jhiyze9q

jhiyze9q3#

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'
}

字符串

4ngedf3f

4ngedf3f4#

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

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)

kgqe7b3p

kgqe7b3p5#

get-help set-psreadlineoption

字符串
https://learn.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 }

z3yyvxxp

z3yyvxxp6#

以下是我在OSX中如何处理那些立即困扰我的事情:

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

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

字符串

相关问题