首页 > 解决方案 > Powershell中不同Vim模式的不同光标

问题描述

PSReadline我使用模块将 Vim 绑定添加到我的 Powershell ,如服务器故障帖子中所示。之后的问题是 Vim 的不同模式没有任何视觉指示器。

我只是想要“命令”和其他模式的不同光标。命令模式的块光标和其他模式的行光标。所以我四处搜索,在微软官方文档中找到了这个: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:无法绑定参数'ViModeIndicator'。无法将值“脚本”转换为类型“Microsoft.PowerShell.ViModeStyle”。” 不会产生任何有用的结果(实际上只有 2 个结果)。

我怎样才能解决这个问题?

标签: powershellvimpsreadline

解决方案


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

Install-Module PsReadline -Scope CurrentUser

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

感谢Mathias R. Jessen评论

该错误告诉您该怎么做:“指定以下枚举数名称之一并重试:无、提示、光标”

我解决了这个问题。我回到文档并检查了文档-ViModeIndicator并发现:

-ViModeIndicator

此选项设置当前 Vi 模式的视觉指示。插入模式或命令模式。

有效值如下:

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

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

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

PSReadLine 模块包含可让您在 PowerShell 中自定义命令行编辑环境的 cmdlet。PowerShell 7.1 随 PSReadLine v2.1 提供。这些文章记录了 PSReadLine v2.1。

因此要解决这个用途:

Install-Module PsReadline -Scope CurrentUser -Force

安装时PsReadline


推荐阅读