Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to set commentstring in Vim

📅 2015-Apr-28 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ commentstring, vim ⬩ 📚 Archive

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

© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘📧