How to set syntax highlighting on for Emacs files

My .emacs is like a roadmap for me where I source many files. Their extension is .emacs: for instance,

 fileName.emacs

The problem is that only ~/.emacs has syntax highlighting.

I would like to have the syntax highlighting for all sourced files which end with .emacs.

How can you put syntax highlighting on to all sourced .emacs -files?

Upvotes: 4

Views: 7433

Answers (2)

AFoglia
AFoglia

Reputation: 8128

Yes. I assume these are lisp files, so you need Emacs to automatically be in lisp-mode when viewing these files. There's two solutions:

  1. The simplest thing is to change the extension to .el. By default, those are opened in lisp-mode.

  2. If, for some reason you really want to use the .emacs extension, you'll want to add this somewhere in your ~/.emacs file:

    (setq auto-mode-alist 
          (append '((".*\\.emacs\\'" . lisp-mode))
                  auto-mode-alist))
    

auto-mode-alist is the list Emacs uses to determine the major mode to use. Each item is a list, the first is the Emacs regular expression Emacs uses to test the filename against, and if true, it uses the mode given in the third item.

(I don't know what the second item is, I've never used it.)

I strongly suggest option 1 though.

Upvotes: 13

Svante
Svante

Reputation: 51501

You can set the mode in the first non-blank line of the file:

;-*-Lisp-*-

This is a comment for Lisp, but causes Emacs to switch to Lisp mode upon reading it into the buffer (reference).

Upvotes: 8

Related Questions