3

For better readability I'd like to split if-statements with multiple expression into multiple lines. But when executing such a statement via a register vim always complains about it.

This:

if 1==1 && 2==2
  echo '1'
endif

works.

But this:

if 1==1
    \ && 2==2
  echo '1'
endif

gives E10: \ should be followed by /, ? or &.

Is it really not possible to use line-continuation for if-statements?

1
  • 1
    I think it might be worth editing the circumstance in which this fails into the question: I'd like to upvote it, because I think both your answer and Matt's are very useful, but find it hard to do so in its current form.
    – Rich
    May 1, 2020 at 9:10

4 Answers 4

5

As it was noted, the problem is that neither :h :@, nor :h execute() know anything about :h line-continuation. Only :h :source supports it.

Here is the piece of code taken from my config to deal with this problem.

" preprocess VimScript to allow
" :h line-continuation and :h line-continuation-comment
function s:preprocess(script)
    if stridx(&cpo, 'C') < 0
        let [l:curr, l:last] = [1, len(a:script) - 1]
        while l:curr <= l:last
            " match line-continuation or line-continuation-comment
            let l:cont = matchlist(a:script[l:curr], '\v^\s*(\\|"\\ )(.*)')
            if empty(l:cont)
                " skip over normal line
                let l:curr += 1
            else
                " join line-continuation
                if l:cont[1] ==# '\'
                    let a:script[l:curr - 1] .= l:cont[2]
                endif
                unlet a:script[l:curr]
                let l:last -= 1
            endif
        endwhile
    endif
    return a:script
endfunction

Now something like call execute(s:preprocess(getreg('', 1, 1)), '') should work ok.

In order to make it useful we still need some dedicated operators/commands/mappings and such. I have my implementation in my Vim config. However, it lacks documentation and isn't available as a separate plugin, so you're at your own risk trying to figure out the details.

1
  • I haven't used it yet, but your functions seems like a good workaround. Therefore I am accepting your answer. Many thanks!
    – radlan
    May 15, 2020 at 12:08
2

According to :help E10, :set nocp while editing the if statement will fix the issue. That is, after setting it, command line continuation will be enabled.

1
  • Of course 'nocompatible' is set. I thought it would only fail to work for if-statements, but I noticed that other line continuations also don't work. The difference is whether they are executed in a script (then it works) or executed directly from a register (:@"). Then it doesn't work.
    – radlan
    Apr 30, 2020 at 21:50
2

As stated in the comment to https://vi.stackexchange.com/a/25009/21417 this has nothing to do with if-statements (or 'compatible' being set). But there obviously is a difference between running that code by sourcing a vimscript file or by running it from a register.

In my case I was just testing a function and didn't bother to :source the whole script that contains the function, but instead copied the function into the clipboard and then executed the content of that clipboard register via :@".

I assume it must be the result of some special escaping rules when copying to (or executing from) a register that break line-continuation.

2
  • Yes, I have this problem all the time, running from a register doesn't respect `` line continuations. Unfortunately, this doesn't appear to be documented in the Vim help system. (Also, you should have mentioned in your question that you were having this problem running from a register. That would have helped find the right answer directly.)
    – filbranden
    Apr 30, 2020 at 22:03
  • 1
    @filbranden I would have mentioned that if would have been aware that this could make a difference. But if I were aware of that I already would know (or better "suspect") the answer to my question. Nevertheless, you and Rich are correct, it is helpful to mention this in the question, now that it is clear that that makes a difference.
    – radlan
    May 15, 2020 at 10:07
2

As of Vim 8.2.0997, line continuations will now work correctly in this scenario.

Thanks to Yegappan Lakshmanan for writing the patch and for letting me know about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.