简体   繁体   中英

What are the rules regarding recursion in Vim mappings?

I have a question in two parts about vim mappings, eg, imap, and recursion. In Vim, mappings can be either right-recursive or neither left- nor right-recursive, but they are not allowed to be left-recursive, right? 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

     xyz 

    As far as I can tell, this behavior is indistinguishable from what would happen had I used inoremap instead of 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

     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

     yzyzyzyzyzyz... 
  4. (mutually recursive) When I execute

     :imap x abc :imap ax 

    then type "x" in insert mode, I receive "E223: recursive mapping".

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?

From http://vim.wikia.com/wiki/Recursive_mappings and the vim command

: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}.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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