防止地图破坏运动命令错误(Prevent map from breaking on motion command error)

我的想法是有一个地图,允许我折叠用花括号括起来的代码块。

nnoremap zff 0f{zf%

这可以按预期工作,但仅限于左大括号。 只要{和}在同一行,以下版本似乎就可以工作。 它们应该在不同的行上,尽管光标只跳到开口{并且没有创建折叠。

nnoremap zff 0f{f}zf%

编辑:

问题似乎是,一旦其中一个动作命令出现错误,地图就会中断。 :silent! 防止ex命令在发生错误时破坏映射。 运动命令是否有等价物?

The idea is to have a map that allows me to fold blocks of code enclosed in curly braces.

nnoremap zff 0f{zf%

This works as expected but only on the opening brace. The following version seems to work as long as { and } are on the same line. Should they be on different lines though the cursor only jumps to the opening { and no fold is created.

nnoremap zff 0f{f}zf%

edit:

The problem appears to be that once there is an error in one of the motion commands the map breaks. :silent! prevents ex commands from breaking a map in case of an error. Is there an equivalent for motion commands?

最满意答案

你是对的,命令序列中的错误会破坏序列,这通常是正确的。 如果您想在错误的情况下继续,只需通过:normal!单独执行命令:normal! 。 这可以通过:execute命令链接在一个命令行中:execute :

nnoremap zff :exe 'normal! 0f{' | exe 'normal! f}' | exe 'normal! zf%'

You're right that an error in a command sequence breaks the sequence, and this is usually right. If you want to continue even in case of errors, just execute the commands separately through :normal!. This can be chained in a single command-line via :execute:

nnoremap zff :exe 'normal! 0f{' | exe 'normal! f}' | exe 'normal! zf%'

更多推荐