【问题标题】:What are the rules regarding recursion in Vim mappings?Vim 映射中关于递归的规则是什么?
【发布时间】:2013-03-11 00:43:13
【问题描述】:

我有两个关于 vim 映射的问题,例如 imap 和递归。在 Vim 中,映射既可以是右递归的,也可以既不是左递归的也不是右递归的,但它们不允许是左递归的,对吧?为什么不?我测试了以下示例来说明我的意思:

  1. (左递归)当我执行时

    :imap x xyz
    

    然后在插入模式下输入“x”,输入到缓冲区的内容是

    xyz
    

    据我所知,如果我使用 inoremap 而不是 imap,这种行为与会发生的情况无法区分。

  2. (既不是左递归也不是右递归)当我执行时

    :imap x yxz
    

    然后在插入模式下输入“x”,缓冲区中的(试图)是什么

    y...yz
    
  3. (右递归)当我执行时

    :imap x yzx
    

    然后在插入模式下输入“x”,缓冲区中的(试图)是什么

    yzyzyzyzyzyz...
    
  4. (相互递归)当我执行时

    :imap x abc
    :imap a x
    

    然后在插入模式下输入“x”,我会收到“E223:递归映射”。

似乎我已经证明 Vim 不允许左递归映射和相互递归映射,但我想验证一下:在 Vim 中定义(可能相互)递归映射的规则是什么?强>

【问题讨论】:

  • 但是……为什么?!递归映射的唯一有效用途是让它一直应用到它在缓冲区结束时出错。
  • @IngoKarkat,为什么不呢? Vim 允许这样做,所以我想知道它们是如何工作的。您确定您给出的用例是唯一有效使用(可能是相互的)递归映射吗?

标签: vim recursion mapping


【解决方案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} 的前缀时,仍然可以观察到这种行为。

【讨论】:

  • 在最后一段中,{lhs} 和 {rhs} 不应该互换吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-06
  • 2014-04-04
  • 2011-07-24
相关资源
最近更新 更多