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

时间:2014-02-11 17:26:37

标签: vim

我想要用于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无法找到)时,它会停止运行序列中的其余操作。有没有办法可以强制它继续执行命令序列?

3 个答案:

答案 0 :(得分:3)

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

实际上,您的映射是非常特定的,它说:

  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部分是问题所在。这意味着您的整个映射取决于当前行中是否存在右括号。

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

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

修改

这是当前宏背后的逻辑:

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 )的内容。但我对这种事情的用处存在严重怀疑。

答案 1 :(得分:0)

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

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

^WC

答案 2 :(得分:0)

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

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

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