Vim录制宏后如何微调?
How to fine-tune Macros after having recorded it through recording in Vim?
具体问题
描述
在注册器 o
记录了所需的操作后,我将整个宏粘贴到我的 ~/.vimrc
并分配如下(直接粘贴映射显示不正确)
预期行为
我想使用这个宏来为自己创建一个新的 "comment line",它引导一个新的脚本部分,其格式使得该部分的名称居中。填充 "section title" 后,我想在新行中进入插入模式。
在下面的屏幕记录中,我测试了 @o
和 @p$ on the word "time". The second attempt with
@p` 都按预期工作。
问题(具体在 Windows 机器上)
如您所见,@o
映射让我得到了垃圾短语,这些短语是我为宏定义的一部分。这与 ^M
运算符有关吗?而且,如何修复 @o
映射,它使用 *
来填充行?
这两个映射在 Linux 系统上工作得很好。 (不知道为什么,因为我已经在 Windows 机器上记录并粘贴了宏定义。)这在 Mac 和 Mac[=105 上似乎也不是问题=].
笼统的问题
- 有没有办法正确替换
^M
运算符(对于 <CR>
或 "Enter"-键)?
- 有没有办法正确替换
^[
运算符(对于 <ESC>
或 "Escape" 键)?
- "recording" 函数通过
q
. 记录的这些奇怪的击键表示是否有系统的映射列表
解决方案
将宏定义中的^M
标记替换为\r
。并且,将 ^[
替换为 \x1b
,用于 ESC 键。映射固定如下:
let @o = ":center\ri\r\x1bkV:s/ /\*/g\rJx50A\*\x1b80d|o"
let @p = ":center\ri\r\x1bkV:s/ /\"/g\rJx50A\"\x1b80d|o"
key-codes/mappings 的完整列表?方法一:通过十六进制代码。
感谢 Zbynek Vyskovsky,图片清晰。对于任何可能想到的密钥,Vim 都在 "face value" 处获取其 ASCII 值。 (诀窍是使用以 \x
开头的转义子句,其中 x
作为连接十六进制值的前导 key/string/character。)因此,对应列表(尚未完成)是如下:
- 输入 ---
\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., "6")
\.. 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 "[=12=]0" and "\x00" force the end of the string.
当您使用 vim 表达式语言进行分配注册时,绝对有可能以独立于平台的方式进行。 vim 表达式中的字符串理解标准转义序列,因此最好将 ^M
替换为 \r
并将 Esc
替换为 \x1b
:
let @o = ":center\riSomeInsertedString\x1b"
据我所知,没有要翻译的特殊字符列表,但您可以简单地获取所有控制字符(32 以下的 ASCII)并将它们翻译成相应的转义序列“\xHexValue
”,其中 HexValue是字符的值。甚至 \r
(或 ^M
)也可以转换为 \x0d
,因为它的 ASCII 值为 13
(0x0d
十六进制)。
具体问题
描述
在注册器 o
记录了所需的操作后,我将整个宏粘贴到我的 ~/.vimrc
并分配如下(直接粘贴映射显示不正确)
预期行为
我想使用这个宏来为自己创建一个新的 "comment line",它引导一个新的脚本部分,其格式使得该部分的名称居中。填充 "section title" 后,我想在新行中进入插入模式。
在下面的屏幕记录中,我测试了 @o
和 @p$ on the word "time". The second attempt with
@p` 都按预期工作。
问题(具体在 Windows 机器上)
如您所见,@o
映射让我得到了垃圾短语,这些短语是我为宏定义的一部分。这与 ^M
运算符有关吗?而且,如何修复 @o
映射,它使用 *
来填充行?
这两个映射在 Linux 系统上工作得很好。 (不知道为什么,因为我已经在 Windows 机器上记录并粘贴了宏定义。)这在 Mac 和 Mac[=105 上似乎也不是问题=].
笼统的问题
- 有没有办法正确替换
^M
运算符(对于<CR>
或 "Enter"-键)? - 有没有办法正确替换
^[
运算符(对于<ESC>
或 "Escape" 键)? - "recording" 函数通过
q
. 记录的这些奇怪的击键表示是否有系统的映射列表
解决方案
将宏定义中的^M
标记替换为\r
。并且,将 ^[
替换为 \x1b
,用于 ESC 键。映射固定如下:
let @o = ":center\ri\r\x1bkV:s/ /\*/g\rJx50A\*\x1b80d|o"
let @p = ":center\ri\r\x1bkV:s/ /\"/g\rJx50A\"\x1b80d|o"
key-codes/mappings 的完整列表?方法一:通过十六进制代码。
感谢 Zbynek Vyskovsky,图片清晰。对于任何可能想到的密钥,Vim 都在 "face value" 处获取其 ASCII 值。 (诀窍是使用以 \x
开头的转义子句,其中 x
作为连接十六进制值的前导 key/string/character。)因此,对应列表(尚未完成)是如下:
- 输入 ---
\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., "6")
\.. 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 "[=12=]0" and "\x00" force the end of the string.
当您使用 vim 表达式语言进行分配注册时,绝对有可能以独立于平台的方式进行。 vim 表达式中的字符串理解标准转义序列,因此最好将 ^M
替换为 \r
并将 Esc
替换为 \x1b
:
let @o = ":center\riSomeInsertedString\x1b"
据我所知,没有要翻译的特殊字符列表,但您可以简单地获取所有控制字符(32 以下的 ASCII)并将它们翻译成相应的转义序列“\xHexValue
”,其中 HexValue是字符的值。甚至 \r
(或 ^M
)也可以转换为 \x0d
,因为它的 ASCII 值为 13
(0x0d
十六进制)。