简体   繁体   English

为什么 vimrc 映射宏中的“dO----------”会抛出错误“E488:尾随字符”?

[英]Why does "dO----------" in a vimrc mapped macro throw the error "E488: Trailing characters"?

In my vimrc, I have a fold marker, a function, and a mapped macro:在我的 vimrc 中,我有一个折叠标记、一个 function 和一个映射宏:

set foldmarker=----------,++++++++++++

"Function to switch INTO visual line mode, not just toggle
function SwitchToVisLine()
    if visualmode()!="V"
        execute "normal! V"
    endif
endfunction

"Macro to wrap and fold visually highlighted lines
xnoremap <F3> :call SwitchToVisLine()
              \dO
              \----------<CR>
              \++++++++++<ESC>
              \k0pzako

The point is to be able to go from any of the three visual modes and tap <F3> to have the active lines wrapped in the fold markers, folded, and the cursor returned on a new line above the whole mess.关键是能够从三种视觉模式中的任何一种中选择 go 并点击<F3>以将活动线包裹在折叠标记中,折叠,并且 cursor 在整个混乱上方的新行上返回。

PROBLEM: When using the macro as written, I get the error E488: Trailing characters: dO----------问题:使用所写的宏时,出现错误E488: Trailing characters: dO----------

What's confusing is that when I walk through each keystroke manually, even calling the function to switch from visual or visual-block mode into visual-line mode, I DON'T get this error.令人困惑的是,当我手动遍历每个击键时,甚至调用 function 从视觉或视觉块模式切换到视觉线模式时,我都没有收到此错误。 It only happens when I run it as a mapped macro.仅当我将其作为映射宏运行时才会发生。

Any help is much appreciated!任何帮助深表感谢!

In its current form, your attempt at making a multiline macro looks like this to Vim with the line continuation characters removed:在当前的形式中,您尝试制作多行宏在 Vim 中看起来像这样,并删除了行继续字符:

:call SwitchToVisLine()dO----------<CR>++++++++++<ESC>k0pzako
                      /\
                     <CR>

which is incorrect due to the missing <CR> after :call SwitchToVisLine() .这是不正确的,因为:call SwitchToVisLine()之后缺少<CR>

It should look like this:它应该如下所示:

xnoremap <F3> :call SwitchToVisLine()<CR>
              \dO
              \----------<CR>
              \++++++++++<ESC>
              \k0pzako

That said, there are quite a few improvement opportunities, here.也就是说,这里有很多改进机会。

  • The macro could be replaced with two :help:silent :help:put commands:宏可以替换为两个:help:silent :help:put命令:

     xnoremap <F3>:sil'<put!='----------'\|sil'>put='++++++++++'<CR>

    which:哪个:

    • makes your custom function unnecessary because there is no need to force visual-line mode anymore,使您的自定义 function 变得不必要,因为不再需要强制视觉线模式,
    • doesn't pollute any register,不污染任何寄存器,
    • is not destructive.没有破坏性。
  • You could use the value of foldmarker instead of repeating yourself:您可以使用foldmarker的值而不是重复自己:

     xnoremap <F3>:sil'<put,=&fmr->split(',')[0]\|sil'>put=&fmr->split(',')[1]<CR>

Ten lines to one, nice.十对一,不错。

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

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