CommentingCode

This page is about commenting parts of code.

Each code language has its own comment string for commenting-out code. In Emacs lisp, for example, it is ;. Some languages have different comment strings for comment start and end. These are the values of mode-specific variables comment-start and comment-end. For example, in C, comment-start is /* and comment-end is */.

The main command for commenting or uncommenting each of the lines in a region is comment-region. With prefix argument, this command adds that many comment strings. So, in Emacs lisp code, where the comment string is ;:

If you want to bind uncommenting to a key, you can use this (equivalent to `C-u M-x comment-region):

      (defun uncomment-region (beg end)
        "Like `comment-region' invoked with a C-u prefix arg."
        (interactive "r")
        (comment-region beg end -1))

I bind `C-x C-;’ to ‘comment-region’. Very handy. (And that key sequence is normally unbound.) – DrewAdams

For end-of-line comments (after existing code), use command indent-for-comment, which is usually bound to `M-;’. You can set the column that the comment string is placed in with command set-comment-column.

In some versions of Emacs, there is command comment-dwim, which combines comment-region and indent-for-comment to always DoWhatIMean. In those versions, it is usually bound to `M-;’ (replacing indent-for-comment).

Also very useful is ‘comment-or-uncomment-region’, present in newer versions of Emacs.

I am addicted to the M-; comment-dwim command. However I wanted to change the default behaviour a bit. If no region is selected, comment-dwim inserts comment at the end of the line. Instead, I wanted to comment out whole line, if we are currently not at the end of the line. This does the trick:

      ;; Original idea from
      ;; http://www.opensubscriber.com/message/emacs-devel@gnu.org/10971693.html
      (defun comment-dwim-line (&optional arg)
        "Replacement for the comment-dwim command.
        If no region is selected and current line is not blank and we are not at the end of the line,
        then comment current line.
        Replaces default behaviour of comment-dwim, when it inserts comment at the end of the line."
          (interactive "*P")
          (comment-normalize-vars)
          (if (and (not (region-active-p)) (not (looking-at "[ \t]*$")))
              (comment-or-uncomment-region (line-beginning-position) (line-end-position))
            (comment-dwim arg)))
      (global-set-key "\M-;" 'comment-dwim-line)

The above version requires the region to start at the beginning of a line - this version corrects that. Source: http://stackoverflow.com/a/20064658/243392

    (defun comment-eclipse ()
      (interactive)
      (let ((start (line-beginning-position))
            (end (line-end-position)))
        (when (region-active-p)
          (setq start (save-excursion
                        (goto-char (region-beginning))
                        (beginning-of-line)
                        (point))
                end (save-excursion
                      (goto-char (region-end))
                      (end-of-line)
                      (point))))
        (comment-or-uncomment-region start end)))

Questions

  • Is there a command for moving to the end of the current comment, assuming point is in a comment? --SueDNymme?

See Also:


CategoryComments CategoryProgramming CategoryRegion