繁体   English   中英

Vim:“f”在无法找到角色时会破坏我的映射

[英]Vim: “f” breaks my mapping when it can't find a character

我有一个(相当粗略的)映射我想用于Jade文件。

nmap <leader>jc ^f)Wc$

应该转变这些例子

a.classname(href='url', title='title') Click here
p This is a paragraph

a.classname(href='url', title='title') _
p _

其中_是插入模式下的光标。

但是,当f找不到时)它停止运行序列中的剩余动作。 有没有办法我可以强迫它继续执行命令序列?

您似乎需要重新考虑一下您的问题。

事实上,您的映射非常具体,它说:

  jump to the first printable character on the line
> jump to the first closing parenthese on the line
  jump to next WORD
  delete from the cursor to the end of the line
  enter insert mode

jump to the first closing parenthese on the line部分jump to the first closing parenthese on the line是问题所在。 这意味着您的整个映射取决于当前行中是否存在右括号。

您可以将该映射保持在当前状态,并在没有意义的情况下停止使用它(当前行上没有关闭括号时),或者重新定义问题以便通过通用解决方案解决问题。

可能涉及一些脚本的通用解决方案。 或不。

编辑

这是当前宏背后的逻辑:

1. jump to a specific "anchor" on the line
            |
            V
2. perform an action on whatever comes after that anchor 
   on that line

事实证明:

foo( bar, baz ) lorem ipsum

成:

foo( bar, baz ) |

而且显然在下面的行上什么也没做:

foo = { "bar" : "baz" }

宏的行为既一致又可预测。 它适用于特定情况,并且不会在轻微或完全不同的情况下起作用。

这就是你希望它如何工作:

1.  try to jump to a specific "anchor" on the line
            |
            V
2a. if success: perform an action on whatever comes after that anchor 
    on that line
            |
            V
2b. if failure: perform an action on whatever comes after that anchor 
    on that line

这就像......放置一个在函数中间不执行任何操作的条件。

它会变成:

foo( bar, baz ) lorem ipsum

成:

foo( bar, baz ) |

和:

foo = { "bar" : "baz" }

成:

foo |

这会使宏的行为不一致:它会起作用,并且对任何文本行都有很大的影响。 不知何故,我认为这不是你想要的。

对于记录, :^f)Wc$不“继续”。 光标停留在该行的第一个可打印字符上并停在那里,这正是nmap <leader>jc ^f)Wc$作用。

实际上,你要求像/)\\{-,1}这样的东西匹配零或1 ) 但我对这种事情的用处有严重怀疑。

你的初始宏开始是错误的,因为它似乎根本没有映射你似乎试图解决的问题。

您最终给出的两个示例可以使用相同的命令完成:

^WC

罗曼尔已经试图说服你,你的方法有一些缺陷。

无论如何,如果您希望在命令失败后继续宏,则必须将正常模式命令序列拆分为Ex命令(使用:normal ),因为这些命令不会中止:

nmap <leader>jc :execute 'normal ^f)'<CR>Wc$

暂无
暂无

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

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