Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs - setting comment character by file extension

Tags:

comments

emacs

I work with a certain group of files (ASCII readable) but with weird extension names. The comment character in all of these is #

Everytime I comment lines, I have to set the comment character for every file-open-close cycle. Is there a way in which I can set up .emacs file to know which comment character to use ?

like image 258
boffin Avatar asked Dec 03 '25 06:12

boffin


1 Answers

Do all the files live within a common directory tree?

If so, then you could simply place a file named .dir-locals.el at the top level of that tree, with the contents:

((fundamental-mode . ((comment-start . "#"))))

For details, read:
C-hig (emacs) Directory Variables RET

(That approach has the bonus that anyone else using Emacs to access those files will also benefit from the .dir-locals.el file.)

Otherwise I'd be inclined to create a simple derived mode with that comment character, and assign all those file extensions to it. If you're dealing with these files in lots of different places, that would probably be the way to go:

(define-derived-mode my-mode fundamental-mode "MyMode"
  "Comments start with `#'."
  (set (make-local-variable 'comment-start) "#"))

(add-to-list 'auto-mode-alist '("\\.\(foo\|bar\)\\'" . my-mode))

That will make files with .foo and .bar extensions start in my-mode with the # comment character.

like image 199
phils Avatar answered Dec 05 '25 00:12

phils



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!