6

I have a function that looks like

fu! MultiLineTest(u, v)
    if    a:u != 2 &&
       \  a:v != 2

       return 'yes'
    else
       return 'no'
    end
endfu

Of course, the actual function is longer and more complicated.

Now, I'd like to add comments to the multi line expression, like so:

    if    a:u != 2 &&  " Neither u nor
       \  a:v != 2     " v must be 2

If the function is called now, I get an error:

E15: Invalid expression: a:u != 2 && " neither u nor a:v != 2 " v must be 2

I understand why this error is thrown. But I'd still find it nice if I could somehow add comments to such a multi line expression. Is this possible?

  • 6
    If you value your sanity, it's a good idea to give up on end line comments entirely. " is used as both a string delimiter and a comment character, and thus its exact meaning depends on the context. Now, this wouldn't be a problem in a normal language. However, VimL is weird at best. End line comments can create problems that are really hard to debug. – Sato Katsura Aug 8 '15 at 11:16
8

Not directly. But: if the comment applies to the whole line, you better place it above the line. If the comment(s) are each to indicate something about a portion of the line, you could consider splitting the line into exactly those pieces, and use variables named such as that they can replace your comments. It's better (IMHO) when the code "speaks" directly. E.g.

fu! MultiLineTest(u, v)
    let meaning_one = (a:u != 2) "there can also be details (if needed)
    let meaning_two = (a:v != 2) "other details
    return (meaning_one && meaning_two) ? 'yes' : 'no'
endfu

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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