Powershell 中不同 Vim 模式的不同光标

Different cursor for different Vim modes in Powershell

我使用 PSReadline 模块向我的 Powershell 添加了 Vim 绑定,如图 in this Server Fault post 所示。之后的问题是 Vim.

的不同模式没有任何视觉指示器

我只是想要 'command' 和其他模式的不同光标。命令模式的块光标和其他模式的行光标。所以我四处搜索并在微软官方文档中找到了这个:Use ViModeChangeHandler to display Vi mode changes

# This example emits a cursor change VT escape in response to a Vi mode change.

function OnViModeChange {
    if ($args[0] -eq 'Command') {
        # Set the cursor to a blinking block.
        Write-Host -NoNewLine "`e[1 q"
    } else {
        # Set the cursor to a blinking line.
        Write-Host -NoNewLine "`e[5 q"
    }
}
Set-PSReadLineOption -ViModeIndicator Script -ViModeChangeHandler $Function:OnViModeChange

我只是将其复制粘贴到 $PROFILE 文件底部 运行 ise $PROFILE.

之后

令人惊讶的是,当我尝试获取我的 $PROFILE:

时出现错误
> & $PROFILE
Set-PSReadLineOption : Cannot bind parameter 'ViModeIndicator'. Cannot convert value "Script" to type
"Microsoft.PowerShell.ViModeStyle". Error: "Unable to match the identifier name Script to a valid enumerator name. Specify one of
the following enumerator names and try again:
None, Prompt, Cursor"
At C:\Users\user\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:27 char:39
+ Set-PSReadLineOption -ViModeIndicator Script -ViModeChangeHandler $Fu ...
+                                       ~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-PSReadLineOption], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.SetPSReadLineOption

谷歌搜索 "Set-PSReadLineOption : Cannot bind parameter 'ViModeIndicator'. Cannot convert value "Script" to type "Microsoft.PowerShell.ViModeStyle"." 没有得到任何有用的结果(实际上只有 2 个结果)。

我该如何解决这个问题?

当我按照服务器故障中给出的说明进行操作时 post 我 运行:

Install-Module PsReadline -Scope CurrentUser

并被告知 PsReadline 已经安装在我的系统上。它要求我在命令末尾附加 -Force 以强制它更新。当时,我没有附加 -Force 而是使用预安装的版本。

感谢Mathias R. Jessen's :

The error tells you what to do: "Specify one of the following enumerator names and try again: None, Prompt, Cursor"

我想通了。我回到文档并检查了 -ViModeIndicator 的文档并发现:

-ViModeIndicator

This option sets the visual indication for the current Vi mode. Either insert mode or command mode.

The valid values are as follows:

None: There`s no indication.
Prompt: The prompt changes color.
Cursor: The cursor changes size.
Script: User-specified text is printed.

这与我在 Powershell 中得到的输出相矛盾,说 "[...] 以下枚举器名称之一,然后重试:None、提示符、光标"。 =41=]。 (注意没有提到脚本)。

所以我明白我一定是使用了错误版本的 PsReadline,如文档中所述(强调我的):

The PSReadLine module contains cmdlets that let you customize the command-line editing environment in PowerShell. PowerShell 7.1 shipped with PSReadLine v2.1. These articles document PSReadLine v2.1.

因此要解决这个问题:

Install-Module PsReadline -Scope CurrentUser -Force

正在安装 PsReadline