7

What is the syntax for inserting a comment or comments into the vimrc file?

Having trouble finding this on vim documentation online.

9

It's in a different help file (cmdline, help :comment):

                            *:quote* *:comment*
'"' at the start of a line causes the whole line to be ignored.  '"'
after a command causes the rest of the line to be ignored.  This can be used
to add comments.  Example:
    :set ai     "set 'autoindent' option
It is not possible to add a comment to a shell command ":!cmd" or to the
":map" command and a few others, because they see the '"' as part of their
argument.  This is mentioned where the command is explained.

The reason it's there is because vimrc is nothing but a sequence of command-line commands. Pretty much everything that applies to command-line mode works in vimrc.

Example of whole-line comment in vimrc:

" show tab line always
set showtabline=2
3

You can use " at the start of the line:

" A comment
set foo=bar

You can also do this after commands, to ignore the rest of the line:

set foo=bar  " A comment

However, you need to be careful when adding comments after commands. In mappings and :! commands it's downright impossible: inoremap a b " Map a to b won't work. The "comment" is seen as part of the command. The same applies when running shell commands with :!ls " a comment

In some other cases it may also cause unexpected behaviour. For example normal! p " A comment will have the space between the p and " inserted, and the comment part may also be evaluated if Vim guesses it as part of a command.

Another problem is adding comments in when running multiple commands, which is something I often do in autocommands. For example this won't work:

autocmd Filetype go
    \  echom "A message"  
    \  " echo a message
    \| echom "A second message"

As far as I know, there is no sane way to get this to work :-/

There are probably other caveats; I chose to not remember them and only use " at the start of a line :-)

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.