3

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:

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

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

Drew
  • 77,472
  • 10
  • 114
  • 243
alper
  • 1,370
  • 1
  • 13
  • 35

1 Answers1

4
(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))
phils
  • 50,977
  • 3
  • 79
  • 122
  • Updating into (add-to-list 'auto-mode-alist '("\\.mdl\\'" . markdown-mode)) fixed it, thanks – alper Apr 02 '20 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: https://emacs.stackexchange.com/q/59387/18414 – alper Jul 01 '20 at 19:16