如何对可视化选择的文本运行自定义命令

Ale*_*cke 3 vim command

在我的 .vimrc 中,我声明了以下命令:

:command EscapeUnderscore %s/_/\\_/g
Run Code Online (Sandbox Code Playgroud)

但是,我希望能够直观地选择几行,并且让命令仅影响选定的行。目前我收到错误E481: No range allowed。我尝试更改%s为 just s,但这没有产生预期的结果。

Pet*_*ker 5

-range将and <line1>/<line2>与您的命令一起使用。看起来像这样:

:command -range EscapeUnderscore <line1>,<line2>s/_/\\_/g
Run Code Online (Sandbox Code Playgroud)

请参阅以下内容以获得更多帮助:

:h :command
:h :command-range
:h <line1>
Run Code Online (Sandbox Code Playgroud)