如何将视觉选择从unicode转换为vim命令中的相应字符?
我正在尝试将 Unicode 代码的多个实例转换为其相应的字符。
我有一些采用这种格式的文本:
U+00A9
我想在它旁边生成以下内容:
©
我尝试在可视模式下选择代码并使用选择范围“<,”>在命令模式下作为 i_CTRL_V 的输入,但我不知道如何在命令上使用特殊键。
我在 :help command-mode 手册中没有找到任何有用的内容。我可以使用其他工具解决这个问题,但我想提高我的 vim 知识。任何提示表示赞赏。
编辑: 正如 @m_mlvx 所指出的,我的目标是直观地选择,然后运行一些命令来查找 Unicode 并进行替换。手动输入像 :s/U+00A9/U+00A9 ©/g
这样的替换不是我感兴趣的,因为它需要在每次替换时手动输入每个特殊字符。
I'm trying to convert multiple instances of Unicode codes to their corresponding characters.
I have some text with this format:
U+00A9
And I want to generate the following next to it:
©
I have tried to select the code in visual mode and use the selection range '<,'> in command mode as input for i_CTRL_V but I don't know how to use special keys on a command.
I haven't found anything useful in the manual with :help command-mode . I could solve this problem using other tools but I want to improve my vim knowledge. Any hint is appreciated.
Edit:
As @m_mlvx has pointed out my goal is to visually select, then run some command that looks up the Unicode and does the substitution. Manually input a substitution like :s/U+00A9/U+00A9 ©/g
is not what I'm interested in as it would require manually typing each of the special characters on every substitution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里有很多......
:help i_ctrl-v
是关于插入模式和范围在命令行模式下很重要,所以:help command-mode
完全是无关紧要。当 Ex 命令处理文本时,它们仅适用于行,而不是任意文本。这使得像
'<,'>
这样的范围在这种情况下变得无关紧要。仔细阅读从
:help i_ctrl-v
链接的:help i_ctrl-v_digit
后,我们可以得出结论,应该使用它:u
,+
,所以这两者都应该是正确的:
<前><代码>u00a9
u00A9
但您的输入是
U+00A9
,因此,即使您以某种方式设法“捕获”该U+00A9
,您还是赢了无法按原样使用它:必须先对其进行消毒。我会进行替换,但是根据您最终想要如何使用该值,可能有数十种方法:说明:
\(\a\)
捕获字母字符。+
匹配文字+
。\(.*\)
捕获其余部分。\L
将其后面的所有内容小写。\1\2
重用上面的两个捕获组。从这里,我们可以想象一种基于替换的方法。假设“And I want to generated the next to it”表示您想要获取:
<前><代码>U+00A9©
你可以这样做:
说明:
v
直观地选择
覆盖的文本。y
将其拉到“未命名寄存器”@"
。:help feedkeys()
用作将一系列复杂字符发送到 Vim 输入队列的低级方法。它允许我们在执行宏之前以编程方式构建宏。'>
将光标移动到视觉选择的末尾。a
在光标后启动插入模式。
+ 替换的输出插入适当的字符。不过,该片段需要转换为映射。
Here are a whole lot of them…
:help i_ctrl-v
is about insert mode and ranges matter in command-line mode so:help command-mode
is totally irrelevant.When they work on text, Ex commands only work on lines, not arbitrary text. This makes ranges like
'<,'>
irrelevant in this case.After carefully reading
:help i_ctrl-v_digit
, linked from:help i_ctrl-v
, we can conclude that it is supposed to be used:u
,+
,So both of these should be correct:
But your input is
U+00A9
so, even if you somehow manage to "capture" thatU+00A9
, you won't be able to use it as-is: it must be sanitized first. I would go with a substitution but, depending on how you want to use that value in the end, there are probably dozens of methods:Explanation:
\(\a\)
captures an alphabetic character.+
matches a literal+
.\(.*\)
captures the rest.\L
lowercases everything that comes after it.\1\2
reuses the two capture groups above.From there, we can imagine a substitution-based method. Assuming "And I want to generate the following next to it" means that you want to obtain:
you could do:
Explanation:
v<motion>
visually selects the text covered by<motion>
.y
yanks it to the "unnamed register"@"
.:help feedkeys()
is used as low-level way to send a complex series of characters to Vim's input queue. It allows us to build the macro programatically before executing it.'>
moves the cursor to the end of the visual selection.a
starts insert mode after the cursor.<C-v>
+ the output of the substitution inserts the appropriate character.That snippet begs for being turned into a mapping, though.
如果您只想将 unicode 转换为相应的字符,您可以使用这样的 nr2char 函数:
简要说明
如果您希望文本旁边有 unicode 字符,您需要稍微修改右侧(使用
submatch(0)
获得完全匹配,.
进行追加)In case you would like to just convert unicodes to corresponding characters, you could use such nr2char function:
Brief explanation
In case you would like to have unicode character next to text you need to modify slightly right side (use
submatch(0)
to get full match and.
to append)如果有人想知道如何编写替换命令:
正则表达式为:
然后替换为 (
:h sub-replace-expression
) 连接:这将在可视/命令模式下执行并在选定的情况下工作线范围。
In case someone wonders how to compose the substitution command:
The regex is:
Then substituted by (
:h sub-replace-expression
) concatenation of:This is to be executed in Visual/command mode and works over selected line range.