Emacs: comment-line vs comment-dwim

By Xah Lee. Date: . Last updated: .

Emacs 25, new command comment-lineCtrl+x Ctrl+;

Before this, you normally use comment-dwimAlt+;

Here's the difference.

When There's No Selection

For example, suppose you have:

xd.dot = function (u,v) {
    return ▮(u[0]*v[0])+(u[1]*v[1])
}

After comment-line, you get:

xd.dot = function (u,v) {
    // return (u[0]*v[0])+(u[1]*v[1])
▮}

After comment-dwim, you get:

xd.dot = function (u,v) {
    return (u[0]*v[0])+(u[1]*v[1]) // }

When There's Selection

For example, suppose you have:

xd.dot = function (u,v) {
    return (u[0]*v[0])+(u[1]*v[1])
}

with the first paren parts after return selected.

After comment-line, you get:

xd.dot = function (u,v) {
    // return (u[0]*v[0])+(u[1]*v[1])
}

After comment-dwim, you get:

xd.dot = function (u,v) {
    return // (u[0]*v[0])
        +(u[1]*v[1])
}

Merge comment-line and comment-dwim

You can merge the two commands into one.

see Emacs: Toggle Comment Current Line

This will toggle line, when no selection. But if cursor is at end of line, it'll add line comment there. If there's selection, it'll do comment-dwim.

Which is Better?

I think, statistically, toggling of comment by line is by far more frequently needed than adding comment at end of line.

The current situation isn't ideal. Now, we have 2 commands. plus comment-region, uncomment-region. (they don't have keys.)

If would be better that comment-line behavior gets merged into comment-dwim, by simply tweaking the behavior of comment-dwim:

When there's no selection, if cursor is at the end of line, add comment to the end of line, else toggle whole line. When there's selection, do comment-dwim.

I've implemented this and used for a year, but never polished it. https://github.com/xahlee/xah-comment.el 〔it is standalone package, not dependent on newcomment.el, which is the comment-dwim package.〕

The package is supposed to also handle multi-line comment syntax.

Writing a comment command is actually very fun. It's a sort of pure problem. You'd think it's simple, but it's rather quite tedius. There are many different situations you have to deal with, and you have to think about how you want it to be.

For how to, see Emacs Lisp: How to Write Your Own Comment Command from Scratch

by the way, writing a indentation command is similarly non-trivial. It's surprising to know, that emacs indentation command, are mostly based on previous line. That is, just indent to whatever previous line is.

Emacs Does Not Handle Multi-Line Comment Syntax

For all emacs's glory, emacs does not handle multi-line comment syntax in general.

Many languages have both multi-line comment and single-line comment. Example:

/**
 * dot product of 2 vectors
 * @param {array} - u Vector of the form [x,y]
 * @param {array} - v Vector of the form [x,y]
 * @return {number} - the dot product.
 */
xd.dot = function (u,v) {
    return (u[0]*v[0])+(u[1]*v[1]) // single line comment
}

History

The new command comment-line is due to Artur Malabarba. He first wrote the command:

Implementing comment-line By Artur Malabarba. At http://endlessparentheses.com/implementing-comment-line.html

He then sent a patch to the emacs dev:

New in Emacs 25.1: comment-line By Artur Malabarba. At http://endlessparentheses.com/new-in-emacs-25-1-comment-line.html

http://lists.gnu.org/archive/html/emacs-devel/2015-01/msg00884.html

I reported this as a problem 8 years ago, but got rejected.

Like it? Buy Xah Emacs Tutorial. Thanks.