我有两个关于vim映射的问题,例如imap和递归。在Vim中,映射可以是右递归的,也可以是左递归的,也不是右递归的,但它们不允许是左递归的,对吧?为什么不?我测试了以下示例来说明我的意思:
(左递归)当我执行
时:imap x xyz
然后在插入模式下键入“x”,输入到缓冲区的是
xyz
据我所知,这种行为与我使用inoremap而不是imap会发生的情况难以区分。
(左右都没有递归)当我执行
时:imap x yxz
然后在插入模式下键入“x”,(尝试)放入缓冲区的是
y...yz
(右递归)当我执行
时:imap x yzx
然后在插入模式下键入“x”,(尝试)放入缓冲区的是
yzyzyzyzyzyz...
(相互递归)当我执行
时:imap x abc
:imap a x
然后在插入模式下键入“x”,我收到“E223:递归映射”。
似乎我已经证明Vim不允许左递归映射和相互递归映射,但我想验证:在Vim中定义(可能是相互的)递归映射的规则是什么?
答案 0 :(得分:1)
来自http://vim.wikia.com/wiki/Recursive_mappings和vim命令
:help recursive_mapping
If you include the {lhs} in the {rhs} you have a recursive mapping. When
{lhs} is typed, it will be replaced with {rhs}. When the {lhs} which is
included in {rhs} is encountered it will be replaced with {rhs}, and so on.
This makes it possible to repeat a command an infinite number of times ...
There is one exception: If the {rhs} starts with {lhs}, the first character
is not mapped again (this is Vi compatible) ... For example, if you use:
:map x y
:map y x
Vim will replace x with y, and then y with x, etc.
When this has happened 'maxmapdepth' times (default 1000),
Vim will give the error message "recursive mapping".
似乎文档只关注{lhs}是单个字符的映射,但是当{lhs}是多个字符并且是{rhs}的前缀时,仍然会观察到这种行为。 / p>