How to set commentstring in Vim

The commentstring option is used by Vim to handle comments that span the entire line. This option can be set in your Vimrc for a specific filetype or globally for all files.

To set a commentstring globally for all undetected filetypes, for example the hash symbol:

setglobal commentstring=# %s

To set a commentstring specifically for a filetype, say for C++ files:

autocmd FileType cpp setlocal commentstring=// %s

The above setting is actually useful because Vim does not use this style of comments by default for C++.

For more info, see How Vim handles comments.

Tried with: Vim 7.4 and Ubuntu 14.04

One thought on “How to set commentstring in Vim

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.