【问题标题】:How to fine-tune Macros after having recorded it through recording in Vim?在 Vim 中录制后如何微调宏?
【发布时间】:2017-01-08 20:46:00
【问题描述】:

具体问题

说明

在注册者o记录了想要的动作后,我将整个宏粘贴到我的~/.vimrc并分配如下(直接粘贴映射不正确显示)

预期行为

我想使用此宏为自己创建一个新的“注释行”,该“注释行”引导一个新的脚本部分,其格式使该部分的名称居中。填充“节标题”后,我想在新行中进入插入模式。

在下面的屏幕记录中,我测试了 @o@p$ on the word "time". The second attempt with@p` 是否正常工作。

问题(特别是在 Windows 机器上)

如您所见,@o 映射让我使用了垃圾短语,这些短语曾是我对宏的定义的一部分。这是否与^M 运算符有关?而且,如何修复使用* 填充行的@o 映射?

这两个映射在 Linux 系统上运行良好。 (不知道为什么,因为我已经在 Windows 机器上记录并粘贴了宏定义。)这在使用 MacVim 的 Mac 上似乎也不是问题。

一般性问题

  1. 有没有办法正确替换^M 运算符(用于<CR>,或“Enter”键)?
  2. 有没有办法正确替换^[ 运算符(用于<ESC>,或“Escape”键)?
  3. 这些奇怪的击键表示是否有系统的映射列表,由“记录”功能通过q 记录。

解决方案

\r 替换宏定义中的^M 标记。并且,将^[ 替换为\x1b,用于ESC 键。映射固定如下:

let @o = ":center\ri\r\x1bkV:s/ /\*/g\rJx50A\*\x1b80d|o"
let @p = ":center\ri\r\x1bkV:s/ /\"/g\rJx50A\"\x1b80d|o"

键码/映射的完整列表?方法一:通过十六进制代码。

感谢 Zbynek Vyskovsky,图片清晰。对于人们可能想到的任何键,Vim 都将其 ASCII 值作为“面值”。 (诀窍是使用以\x 开头的转义子句,其中x 用作连接到十六进制值的引导键/字符串/字符。)因此,对应列表(不完整)如下:

  • 输入 --- \x0d --- \r
  • ESC --- \x1b --- \e

Vim 原生解决方案

:help expr-quote 偶然提供了以下特殊字符列表。这将作为一般形式的原始问题的明确答案。

string                  *string* *String* *expr-string* *E114*
------
"string"        string constant     *expr-quote*

Note that double quotes are used.

A string constant accepts these special characters:
\...    three-digit octal number (e.g., "\316")
\.. two-digit octal number (must be followed by non-digit)
\.  one-digit octal number (must be followed by non-digit)
\x..    byte specified with two hex numbers (e.g., "\x1f")
\x. byte specified with one hex number (must be followed by non-hex char)
\X..    same as \x..
\X. same as \x.
\u....  character specified with up to 4 hex numbers, stored according to the
    current value of 'encoding' (e.g., "\u02a4")
\U....  same as \u but allows up to 8 hex numbers.
\b  backspace <BS>
\e  escape <Esc>
\f  formfeed <FF>
\n  newline <NL>
\r  return <CR>
\t  tab <Tab>
\\  backslash
\"  double quote
\<xxx>  Special key named "xxx".  e.g. "\<C-W>" for CTRL-W.  This is for use
    in mappings, the 0x80 byte is escaped.
    To use the double quote character it must be escaped: "<M-\">".
    Don't use <Char-xxxx> to get a utf-8 character, use \uxxxx as
    mentioned above.

Note that "\xff" is stored as the byte 255, which may be invalid in some
encodings.  Use "\u00ff" to store character 255 according to the current value
of 'encoding'.

Note that "\000" and "\x00" force the end of the string.

【问题讨论】:

    标签: vim macvim


    【解决方案1】:

    当您使用 vim 表达式语言进行分配注册时,绝对可以以独立于平台的方式进行。 vim 表达式中的字符串理解标准的转义序列,因此最好将^M 替换为\r,将Esc 替换为\x1b

    let @o = ":center\riSomeInsertedString\x1b"
    

    据我所知,没有要翻译的特殊字符列表,但您可以简单地将所有控制字符(低于 32 的 ASCII)转换为相应的转义序列“\xHexValue”,其中 HexValue 是性格。即使\r(或^M)也可以转换为\x0d,因为它的ASCII值是130x0d十六进制)。

    【讨论】:

    • 是否有完整的绝对键映射列表,其中^M-\rESC-\x1b 是两对映射?我想用这个通用的映射列表来更新这个问题。
    • 很棒的评论。认为撅嘴“HexValue”!但是,由于我刚刚将 \r 更改为 \x13,因此宏坏了。是否需要更多转义字符?
    • @Zbynek, thank you for pointing out the "hex code". I was able to find the native Vim "solution" through :help expr-quote`.
    猜你喜欢
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    • 2018-08-10
    • 1970-01-01
    • 2014-01-14
    • 2015-07-11
    • 2012-03-16
    • 1970-01-01
    相关资源
    最近更新 更多