简体   繁体   English

关于Vim映射中的递归的规则是什么?

[英]What are the rules regarding recursion in Vim mappings?

I have a question in two parts about vim mappings, eg, imap, and recursion. 我在两部分中有一个关于vim映射的问题,例如imap和递归。 In Vim, mappings can be either right-recursive or neither left- nor right-recursive, but they are not allowed to be left-recursive, right? 在Vim中,映射既可以是右递归的,也可以是左递归的,也不能是右递归的,但是不允许它们是左递归的,对吗? Why not? 为什么不? I tested the following examples to illustrate what I mean: 我测试了以下示例以说明我的意思:

  1. (left recursive) When I execute (左递归)执行时

     :imap x xyz 

    then type "x" in insert mode, what is input into the buffer is 然后在插入模式下键入“ x”,输入到缓冲区的是

     xyz 

    As far as I can tell, this behavior is indistinguishable from what would happen had I used inoremap instead of imap. 据我所知,这种行为与使用inoremap而不是imap会发生的情况没有区别。

  2. (neither left nor right recursive) When I execute (无论是左还是右递归)当我执行

     :imap x yxz 

    then type "x" in insert mode, what is (attempted to be) put in the buffer is 然后在插入模式下键入“ x”,将(尝试)放入缓冲区的是

     y...yz 
  3. (right recursive) When I execute (正确的递归)当我执行

     :imap x yzx 

    then type "x" in insert mode, what is (attempted to be) put in the buffer is 然后在插入模式下键入“ x”,将(尝试)放入缓冲区的是

     yzyzyzyzyzyz... 
  4. (mutually recursive) When I execute (相互递归)执行时

     :imap x abc :imap ax 

    then type "x" in insert mode, I receive "E223: recursive mapping". 然后在插入模式下键入“ x”,我会收到“ E223:递归映射”。

It seems that I've demonstrated that Vim disallows left-recursive mappings and mutually recursive mappings, but I'd like to verify: What are the rules for defining (possibly mutually) recursive mappings in Vim? 看来我已经证明了Vim不允许左递归映射和相互递归映射,但是我想验证一下: 在Vim中定义(可能是相互)递归映射的规则是什么?

From http://vim.wikia.com/wiki/Recursive_mappings and the vim command 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".

It seems that the documentation only concerns itself with mappings where the {lhs} is a single character, but the behavior is still observed when the {lhs} is more than one character and is a prefix of the {rhs}. 看来,文档仅涉及{lhs}是单个字符的映射,但是当{lhs}多于一个字符并且是{rhs}的前缀时,仍会观察到该行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM