Skip to content

Instantly share code, notes, and snippets.

@matveyt
matveyt / comments.md
Last active March 21, 2022 11:22
Vim won't comment on this

This is a pretty common question by beginners: how do I comment out some stuff? And a pretty common answer is to use Visual-Block mode or substitute command. There's even a wiki page to explain this in detail.

But then the novice keeps asking more and more: How do I uncomment? How do I comment with different filetypes? How do I... And then we come again to the usual "go get and install another 10000+ lines plugin named xxx or yyy". But is it really worth one?

So let's put a problem: (1) write a (very!) small command/function able both to comment and uncomment arbitrary line range; (2) an action to be taken depends on the first non-blank line; (3) comment format follows :h 'commentstring' option to allow for easy customization; (4) blank lines are not to be changed; (5) there are two comment styles supported - with indentation (....//foo) and without one (//....foo).

So my best attempt is:

" ToggleComment({line1}, {line2}, {p
@matveyt
matveyt / comments_test.html
Last active February 27, 2021 14:19
HTML comments test
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"/><title>HTML 5.2 comments test</title></head>
<body>
<pre>&lt;! -- Bogus comment --&gt;</pre>
<! -- Bogus comment -->
<p>8.2.4.41-42: parser error, normal continuation after next &gt;</p>