简体   繁体   English

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

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

I have a (fairly crude) mapping I want to use for Jade files. 我有一个(相当粗略的)映射我想用于Jade文件。

nmap <leader>jc ^f)Wc$

should turn these examples 应该转变这些例子

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

into

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

where _ is the cursor in insert mode. 其中_是插入模式下的光标。

However, when f can't find ) it ceases to run the remaining actions in the sequence. 但是,当f找不到时)它停止运行序列中的剩余动作。 Is there a way I can force it to continue with the sequence of commands regardless? 有没有办法我可以强迫它继续执行命令序列?

You seem to need to rethink your problem a bit. 您似乎需要重新考虑一下您的问题。

As it is, your mapping is very specific, it says: 事实上,您的映射非常具体,它说:

  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

The jump to the first closing parenthese on the line part is where the problem is. jump to the first closing parenthese on the line部分jump to the first closing parenthese on the line是问题所在。 It means that your whole mapping depends on the presence of a closing parenthese in the current line. 这意味着您的整个映射取决于当前行中是否存在右括号。

Either you keep that mapping in its current state and stop using it where it doesn't make sense (when there's no closing parenthese on the current line) or you redefine your problem so that it can be solved by a generic solution. 您可以将该映射保持在当前状态,并在没有意义的情况下停止使用它(当前行上没有关闭括号时),或者重新定义问题以便通过通用解决方案解决问题。

A generic solution that may involve a bit of scripting. 可能涉及一些脚本的通用解决方案。 Or not. 或不。

edit 编辑

This the logic behind your current macro: 这是当前宏背后的逻辑:

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

It turns: 事实证明:

foo( bar, baz ) lorem ipsum

into: 成:

foo( bar, baz ) |

and rather obviously does nothing on the line below: 而且显然在下面的行上什么也没做:

foo = { "bar" : "baz" }

The behavior of your macro is both consistent and predictable. 宏的行为既一致又可预测。 It is taylored to a specific situation and doesn't act up in slightly or completely different situations. 它适用于特定情况,并且不会在轻微或完全不同的情况下起作用。

This is the how you would like it to work: 这就是你希望它如何工作:

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

It's like… placing a conditional that does nothing in the middle of a function. 这就像......放置一个在函数中间不执行任何操作的条件。

It would turn: 它会变成:

foo( bar, baz ) lorem ipsum

into: 成:

foo( bar, baz ) |

and: 和:

foo = { "bar" : "baz" }

into: 成:

foo |

This would make the behavior of your macro inconsistent: it would work and have vastly varying effects on any line of text. 这会使宏的行为不一致:它会起作用,并且对任何文本行都有很大的影响。 Somehow I think that's not what you want. 不知何故,我认为这不是你想要的。

For the record, :^f)Wc$ doesn't "continue". 对于记录, :^f)Wc$不“继续”。 The cursor stays on the first printable character on the line and stops there and this is exactly what nmap <leader>jc ^f)Wc$ does as well. 光标停留在该行的第一个可打印字符上并停在那里,这正是nmap <leader>jc ^f)Wc$作用。

In practical terms, you are asking for something like /)\\{-,1} which would match zero or 1 ) . 实际上,你要求像/)\\{-,1}这样的东西匹配零或1 ) But I have serious doubts about the usefulness of such a thing. 但我对这种事情的用处有严重怀疑。

Your initial macro is wrong to begin with because it doesn't seem to map at all with the problem you seem to be trying to solve. 你的初始宏开始是错误的,因为它似乎根本没有映射你似乎试图解决的问题。

The two examples you finally gave can be done with the same command: 您最终给出的两个示例可以使用相同的命令完成:

^WC

romainl has already tried to convince you that your approach has some flaws. 罗曼尔已经试图说服你,你的方法有一些缺陷。

Anyway, if you want your macro to continue after a command failure, you have to split the sequence of normal mode commands into Ex commands (with :normal ), as these do not abort: 无论如何,如果您希望在命令失败后继续宏,则必须将正常模式命令序列拆分为Ex命令(使用:normal ),因为这些命令不会中止:

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

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

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