Elisp: Write Comment Command from Scratch

By Xah Lee. Date: . Last updated: .

This page shows you how to write a command to insert/delete comment syntax chars of a programing language.

First, you should know about emacs's builtin comment package newcomment.el. See: Elisp: How to Write Comment Command in Major Mode.


You want to write a emacs command to comment or uncomment code for your own language. You want to write it from scratch. (maybe because you don't like the newcomment.el behavior, or, your language's comment syntax is different from popular languages supported by emacs syntax table.)


The following code handles C++-style comment // …. The code can be easily modified to handle any comment syntax that starts with a comment char and ends in a newline. (for example, Python's # ….)

It has 3 user-level functions:

“my-comment-dwim” is the general command. If there are no text selection, then it will comment or uncomment the current line, depending on whether the current line is a comment (If the comment start in the middle of the line, the line is not considered a comment). If there is a text selection, then it will comment or uncomment the whole region. Which action it does depends on whether the first line in selection is a comment line.

(defun my-comment-dwim ()
  "Comment or uncomment the current line or text selection."

  ;; If there's no text selection, comment or uncomment the line
  ;; depending whether the WHOLE line is a comment. If there is a text
  ;; selection, using the first line to determine whether to
  ;; comment/uncomment.
  (let (p1 p2)
    (if (use-region-p)
          (setq p1 (region-beginning) p2 (region-end))
          (goto-char p1)
          (if (wholeLineIsCmt-p)
              (my-uncomment-region p1 p2)
            (my-comment-region p1 p2)
        (if (wholeLineIsCmt-p)
          )) )))

(defun wholeLineIsCmt-p ()
    (beginning-of-line 1)
    (looking-at "[ \t]*//")

(defun my-comment-current-line ()
  (beginning-of-line 1)
  (insert "//")

(defun my-uncomment-current-line ()
  "Remove “//” (if any) in the beginning of current line."
  (when (wholeLineIsCmt-p)
    (beginning-of-line 1)
    (search-forward "//")
    (delete-backward-char 2)

(defun my-comment-region (p1 p2)
  "Add “//” to the beginning of each line of selected text."
  (interactive "r")
  (let ((deactivate-mark nil))
      (goto-char p2)
      (while (>= (point) p1)

(defun my-uncomment-region (p1 p2)
  "Remove “//” (if any) in the beginning of each line of selected text."
  (interactive "r")
  (let ((deactivate-mark nil))
      (goto-char p2)
      (while (>= (point) p1)
        (previous-line) )) ))

Elisp, Writing Major Mode

  1. Syntax Coloring
  2. Font Lock Mode
  3. Define Face
  4. Color Comment
  5. Comment Command
  6. Your Own Comment Command
  7. Keyword Completion Command
  8. Create Keymap
  9. Abbrev/Template
  10. Text Properties
  11. Overlay Highlighting
  12. Lookup Doc
  13. Syntax Table

  1. How to Name Your Major Mode
  2. provide, require, features
  3. load, load-file, autoload

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs


Emacs Lisp