Reputation: 6342
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
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*\\"
Upvotes: -1
Reputation: 32370
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.
:help :bar
to create a separate Ex command
:help :execute
):bar
):help :execute
)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
Reputation: 195049
you cannot use in-line comments for map
s
: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.
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*".*$'
this may change the "comment" highlight, but I don't recommend to do it.
Upvotes: 7