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
  • 75,699
  • 9
  • 109
  • 225
alper
  • 1,238
  • 11
  • 30
  • Does this answer your question? [Make emacs automatically open binary files in hexl-mode](https://emacs.stackexchange.com/questions/10277/make-emacs-automatically-open-binary-files-in-hexl-mode) – Drew Sep 15 '20 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 Sep 16 '20 at 09:39

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
  • 48,657
  • 3
  • 76
  • 115
  • 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