Justin Force
Justin Force

Reputation: 6342

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?

Upvotes: 4

Views: 2466

Answers (3)

Good Pen
Good Pen

Reputation: 801

+1 fot "This is not "neat" at all":

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

My preference is to use end of line comment (without adding |) where vimL allows.
The pain point is:
It's hard to remember when vimL allows that. (so some people never uses end of line comment in vimL, which may narrow his choice of formating)

Inspired by the OP, we can utilize the syntax highlight. (But I don't know how to implement yet)

Below is some information which seems to be needed:

Relevant lines in the syntax.vim: /home/linuxbrew/.linuxbrew/Cellar/neovim/0.6.1/share/nvim/runtime/syntax/syntax.vim


syn region  vimString   start="^\s*\\\z(['"]\)" skip='\\\\\|\\\z1' end="\z1" oneline keepend contains=@vimStringGroup,vimContinue

    syn match vimComment  excludenl +\s"[^\-:.%#=*].*$+lc=1   contains=@vimCommentGroup,vimCommentString
    syn match vimComment  +\<endif\s\+".*$+lc=5   contains=@vimCommentGroup,vimCommentString
    syn match vimComment  +\<else\s\+".*$+lc=4    contains=@vimCommentGroup,vimCommentString


    syn region  vimCommentString    contained oneline start='\S\s\+"'ms=e   end='"'
    hi def link vimCommentString vimString


    " end of line comment
        syn match   vimLineComment  +^[ \t:]*".*$+  contains=@vimCommentGroup,vimCommentString,vimCommentTitle
        hi def link vimLineComment   vimComment

        syn match   vim9LineComment +^[ \t:]\+#.*$+ contains=@vimCommentGroup,vimCommentString,vimCommentTitle
        hi def link vim9Comment  Comment


    syn match   vimCommentTitle '"\s*\%([sS]:\|\h\w*#\)\=\u\w*\(\s\+\u\w*\)*:'hs=s+1    contained contains=vimCommentTitleLeader,vimTodo,@vimCommentGroup
    hi def link vimCommentTitle  PreProc
    syn match   vimCommentTitleLeader   '"\s\+'ms=s+1   contained

    syn match   vimContinue "^\s*\\"

enter image description here

Upvotes: -1

dreftymac
dreftymac

Reputation: 32370

Background

  • vim 7.3 all platforms
  • vimscript language (used in vim files)

Problem

The vimscript language supports comments, but end of line comments do not always work predictably, because end of line comments can be mistakenly interpreted by vim as part of the command.

Adding end of line comments is problemmatic in vimscript, because it does not work with all commands.

Solutions

  • 1) use the pipe character :help :bar to create a separate Ex command
    • this is the solution enumerated by @sidewaysmilk
  • 2) simply add the comment below the relevant vimscript command on the next line
  • 3) use the execute command (see :help :execute )

Pitfalls

  • Solution 1) is a somewhat unconventional use of the pipe (aka :bar)
  • Not all commands support the pipe character (see e.g. :help :execute)
  • Solution 2) may not be desirable for the readability of the vimscript, and it does not directly solve the issue in the OP
  • searching for this feature on the internet is tricky because it turns up links related to comments in general-purpose programming contexts, unrelated to vimscript

See also

Vim help links (enter these directly into vim Cmdline-mode):

  • :help vim-script-intro | /comments for some commands
  • :help :bar
  • :help Command-line-mode

Web links:

Upvotes: 4

Kent
Kent

Reputation: 195049

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.

Upvotes: 7

Related Questions