Emacs Lisp: How to Write Comment Command in Major Mode

By Xah Lee. Date: . Last updated: .

This page shows you how to write a command to comment/uncomment a region of text in a major mode.

There are 2 ways to write a command to comment/uncomment code:

This page will show how to use the newcomment.el package to define comment/uncomment commands.

Writing a Comment Command Using newcomment.el


Emacs has a standard command to insert or delete comment, named comment-dwimAlt+;】.

When there is a text selection, comment-dwim will comment or uncomment the region in a smart way.

If there is no text selection, comment-dwim will insert a comment syntax at the end of line, or move to the beginning of comment if already exists.

The comment-dwim is the standard command to comment/uncomment code. Your major mode should support it. When a user types the keyboard shortcut for comment-dwim while in your major mode, it should work.

comment-region, uncomment-region

comment-region and uncomment-region are lower level commands that do the actual work.

comment-dwim simply determine whether to comment or uncomment, and on what region boundary, then call comment-region or uncomment-region.

Make comment-dwim Work for Your Major Mode

All you have to do, is to set 2 buffer-local variables.

For example, here's code for emacs lisp:

(setq-local comment-start "; ")
(setq-local comment-end "")

Here's another example.

In CSS, comment has this syntax:

/* css comment syntax */

Here's code from emacs's css-mode:

(define-derived-mode css-mode fundamental-mode "CSS"
  "Major mode to edit Cascading Style Sheets."

  (setq-local font-lock-defaults css-font-lock-defaults)

  (setq-local comment-start "/*")
  (setq-local comment-start-skip "/\\*+[ \t]*")
  (setq-local comment-end "*/")
  (setq-local comment-end-skip "[ \t]*\\*+/")

  ;; ...


〔►see Emacs Lisp: Regex Tutorial

Write Your Own Comment Command from Scratch

If your language's comment syntax is more complex, different from any of the form in C, Java, python, ruby, php, etc. You'll need to write your own comment/uncomment command.

See: Emacs Lisp: How to Write Your Own Comment Command from Scratch.

thanks to Daniel for correction.

Writing Major Mode Topics

  1. How to Write a Emacs Major Mode for Syntax Coloring
  2. Emacs Lisp: html6-mode
  3. Emacs Lisp: Font Lock Mode Basics
  4. Emacs Lisp: How to Define Face
  5. Emacs Lisp: How to Color Comment in Major Mode
  6. Emacs Lisp: How to Write Comment Command in Major Mode
  7. Emacs Lisp: How to Write Your Own Comment Command from Scratch
  8. Emacs Lisp: How to Write Keyword Completion Command
  9. Emacs Lisp: How to Create Keymap for Major Mode
  10. Emacs Lisp: Create Abbrev and Templates for Major Mode
  11. Emacs Lisp: Text Properties
  12. Emacs Lisp: Overlay Highlighting
  13. Emacs: Lookup Google, Dictionary, Documentation

  1. Emacs Lisp: How to Name Your Major Mode
  2. Emacs Lisp: What's “feature”?
  3. Emacs Lisp: require, load, load-file, autoload, feature, Explained

Syntax Table

  1. Emacs Lisp: Syntax Table Tutorial
  2. Emacs Lisp: How to Find Syntax of a Character?
  3. Emacs Lisp: How to Modify Syntax Table Temporarily
  4. Emacs Lisp: How to Determine If Cursor is Inside String or Comment
  5. Emacs Lisp: Find Matching Bracket Character
Like it? Buy Xah Emacs Tutorial. Thanks.