简体   繁体   中英

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:

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.

PROBLEM: When using the macro as written, I get the error 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. 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:

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

which is incorrect due to the missing <CR> after :call SwitchToVisLine() .

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:

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

    which:

    • makes your custom function unnecessary because there is no need to force visual-line mode anymore,
    • doesn't pollute any register,
    • is not destructive.
  • You could use the value of foldmarker instead of repeating yourself:

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

Ten lines to one, nice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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