如何将视觉选择从unicode转换为vim命令中的相应字符?

发布于 2025-01-16 02:09:51 字数 462 浏览 4 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

他不在意 2025-01-23 02:09:51

任何提示表示赞赏。

这里有很多......

  1. :help i_ctrl-v 是关于插入模式和范围在命令行模式下很重要,所以 :help command-mode 完全是无关紧要。

  2. 当 Ex 命令处理文本时,它们仅适用于,而不是任意文本。这使得像 '<,'> 这样的范围在这种情况下变得无关紧要。

  3. 仔细阅读从 :help i_ctrl-v 链接的 :help i_ctrl-v_digit 后,我们可以得出结论,应该使用它:

    • 带有小写u
    • 没有+
    • 无需担心值的大小写。

    所以这两者都应该是正确的:

    <前><代码>u00a9
    u00A9

  4. 但您的输入是 U+00A9,因此,即使您以某种方式设法“捕获”该 U+00A9,您还是赢了无法按原样使用它:必须先对其进行消毒。我会进行替换,但是根据您最终想要如何使用该值,可能有数十种方法:

    substitute('U+00A9', '\(\a\)+\(.*\)', '\L\1\2', '')
    

    说明:

    • \(\a\) 捕获字母字符。
    • + 匹配文字 +
    • \(.*\) 捕获其余部分。
    • \L 将其后面的所有内容小写。
    • \1\2 重用上面的两个捕获组。
  5. 从这里,我们可以想象一种基于替换的方法。假设“And I want to generated the next to it”表示您想要获取:

    <前><代码>U+00A9©

    你可以这样做:

    v<运动>
    y
    :调用 feedkeys("'>a\" .substitute(@", '\(\a\)+\(.*\)', '\L\1\2', '') 。 “\”)
    

    说明:

    • v 直观地选择 覆盖的文本。
    • y 将其拉到“未命名寄存器”@"
    • :help feedkeys() 用作将一系列复杂字符发送到 Vim 输入队列的低级方法。它允许我们在执行宏之前以编程方式构建宏。
    • '> 将光标移动到视觉选择的末尾。
    • a 在光标后启动插入模式。
    • + 替换的输出插入适当的字符。

    不过,该片段需要转换为映射。

Any hint is appreciated.

Here are a whole lot of them…

  1. :help i_ctrl-v is about insert mode and ranges matter in command-line mode so :help command-mode is totally irrelevant.

  2. When they work on text, Ex commands only work on lines, not arbitrary text. This makes ranges like '<,'> irrelevant in this case.

  3. After carefully reading :help i_ctrl-v_digit, linked from :help i_ctrl-v, we can conclude that it is supposed to be used:

    • with a lowercase u,
    • without the +,
    • without worrying about the case of the value.

    So both of these should be correct:

    <C-v>u00a9
    <C-v>u00A9
    
  4. But your input is U+00A9 so, even if you somehow manage to "capture" that U+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:

    substitute('U+00A9', '\(\a\)+\(.*\)', '\L\1\2', '')
    

    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.
  5. 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:

    U+00A9©
    

    you could do:

    v<motion>
    y
    :call feedkeys("'>a\<C-v>" . substitute(@", '\(\a\)+\(.*\)', '\L\1\2', '') . "\<Esc>")<CR>
    

    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.

掀纱窥君容 2025-01-23 02:09:51

如果您只想将 unicode 转换为相应的字符,您可以使用这样的 nr2char 函数:

:%s/U+\(\x\{4\}\)/\=nr2char('0x'.submatch(1))/g

简要说明

U+\(\x\{4\}\) - search for a specific pattern (U+ and four hexadecimal characters which are stored in group 1)
\= - substitute with result of expression
'0x'.submatch(1) - append 0x to our group (U+00A9 -> 0x00A9)

如果您希望文本旁边有 unicode 字符,您需要稍微修改右侧(使用 submatch(0) 获得完全匹配,. 进行追加)

In case you would like to just convert unicodes to corresponding characters, you could use such nr2char function:

:%s/U+\(\x\{4\}\)/\=nr2char('0x'.submatch(1))/g

Brief explanation

U+\(\x\{4\}\) - search for a specific pattern (U+ and four hexadecimal characters which are stored in group 1)
\= - substitute with result of expression
'0x'.submatch(1) - append 0x to our group (U+00A9 -> 0x00A9)

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)

东北女汉子 2025-01-23 02:09:51

如果有人想知道如何编写替换命令:

'<,'>s/\<[uU]+\(\x\+\)\>/\=submatch(0)..' '..nr2char(str2nr(submatch(1), 16), 1)/g

正则表达式为:

  • 单词开头
  • 字母“U”或“u”
  • 文字“加”
  • 一个或多个十六进制数字(放入“捕获组”)
  • 单词结束

然后替换为 (:h sub-replace-expression) 连接:

  • 整个匹配的字符串
  • 单个空格
  • 字符由取自“捕获组”的 UTF-8 十六进制代码

这将在可视/命令模式下执行并在选定的情况下工作线范围。

In case someone wonders how to compose the substitution command:

'<,'>s/\<[uU]+\(\x\+\)\>/\=submatch(0)..' '..nr2char(str2nr(submatch(1), 16), 1)/g

The regex is:

  • word start
  • Letter "U" or "u"
  • Literal "plus"
  • One or more hex digits (put into "capture group")
  • word end

Then substituted by (:h sub-replace-expression) concatenation of:

  • the whole matched string
  • single space
  • character by UTF-8 hex code taken from "capture group"

This is to be executed in Visual/command mode and works over selected line range.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文