I really like the Emacs comment-dwim
command. The dwim
—do what I mean—neatly captures the idea of the command. It looks at the context in which it is called and then does the right thing™. Most often, I highlight a region that I want to comment or uncomment and then just call 【Meta+x】 comment-dwim
to comment or uncomment the region. It’s very handy for temporarily commenting out a block of code or for turning a paragraph of explanation into a comment without having to worry about adding a comment indicator on each line. The correct comment character, as determined by the mode, is used to mark the comment.
I use the excellent smex
so I normally just start typing dwim
and the proper command comes up. There’s also a keyboard shortcut, 【Meta+;】, that’s pretty easy to remember and that I’m trying to use more often.
Although I find comment-dwim
most useful for working with regions, it also does the right thing on single lines or even empty lines. See the documentation for the details. If you use the keyboard shortcut, it’s a one-size-fits-all method for inserting or deleting comments.