Search code examples
syntaxcommentsvim

Add end of line comment to vim syntax


Not every command in Vim allows you to add end-of-line comments. Sometimes the " is valid as an argument, so it would be ambiguous. However, if you insert a pipe, the command is ended and you can insert a comment. So you can actually achieve reliable end of line comments in vim thusly:

noremap ' ` |" Use single quote as alternate range key

Neat, right? But the syntax/vim.vim file doesn't recognize this as an end of line comment. How do I tell Vim to recognize this syntax?

I found this in syntax/vim.vim:

syn match   vimLineComment  +^[ \t:]*".*$+  contains=@vimCommentGroup,vimCommentString,vimCommentTitle

I tried adding something like this to my ~/.vimrc, but there is no effect. VimScript is hard. :/

syntax match vimLineComment '|".*$+'

Any ideas?


Solution

  • you cannot use in-line comments for maps

    :h map-comments
    

    you will see:

                                *map-comments*
    It is not possible to put a comment after these commands, because the '"'
    character is considered to be part of the {lhs} or {rhs}.
    

    I hope this answers your question.

    hack

    Okay, you may have good reason to do that.

    Only define syn match vimLineComment is not enough, you have to overwrite the vimMapRhs syntax. so these two lines will make |"foo bar highlighted as comment:

    syn match vimMapRhs '.*\ze|\s*".*'
    syn match vimLineComment '|\s*".*$'
    

    enter image description here this may change the "comment" highlight, but I don't recommend to do it.