I was doing following solution for markdown files: How do I associate a file extension with a specific mode?

(autoload 'markdown-mode "markdown-mode.el"
  "Major mode for editing Markdown files" t)

(setq auto-mode-alist
      (cons '("\\.md" . markdown-mode) auto-mode-alist))

I want to do it same operation for text show up when I want to commit a message for git. When I type git commit a COMMIT_EDITMSG file shows up which has Fundamental mode. Since COMMIT_EDITMSG does not have any extention I was not able to match it into markdown-mode as I done for *.md files.

CRM Buffer                 Size Mode             File
.   COMMIT_EDITMSG         1259 Fundamental      ~/folder/.git/COMMIT_EDITMSG

My .gitconfig file setup:

    pager = less -r
    editor = TERM=xterm-256color emacsclient -t

[Q] Is there a way to open git's COMMIT_EDITMSG file in markdown-mode?

  • Does this answer your question? Make emacs automatically open binary files in hexl-mode
    – Drew
    Commented Sep 15, 2020 at 21:16
  • Seems like an alternative solution. I am not sure will COMMIT_EDITMSG or any generated file from git at .git/ without an extention will considered as an binary file
    – alper
    Commented Sep 16, 2020 at 9:39

1 Answer 1

(setq auto-mode-alist
      (cons '("\\.md" . markdown-mode) auto-mode-alist))

That \\.md should be \\.md\\' otherwise it'll match every filename containing the sequence .md anywhere. (I.e. You already weren't limiting your usage to just matching filename extensions. For instance /home/alper/.mdir/foo/bar.c would match your pattern.)

As you can see, the regexps in auto-mode-alist are matched against the entire filename, so you can easily use it to match your COMMIT_EDITMSG files.

One would normally use add-to-list to update the variable:

(add-to-list 'auto-mode-alist
             '("/\\.git/COMMIT_EDITMSG\\'" . markdown-mode))
  • Updating into (add-to-list 'auto-mode-alist '("\\.mdl\\'" . markdown-mode)) fixed it, thanks
    – alper
    Commented Apr 2, 2020 at 13:51
  • I know this is solved but I am having very wierd behavior. I am using emacs editor to edit commit message. Everythings works all fine. But if I enable magit-diff the markdown-mode does not apply to that COMMIT_EDITMSG, and git commit fails. Please see: emacs.stackexchange.com/q/59387/18414
    – alper
    Commented Jul 1, 2020 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.