views:

565

answers:

3

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?

+10  A: 

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.

AFoglia
Thank you for your answer!
Masi
The "second item" is the notation for making a literal quoted cons. A cons is the low level building block of lists. '(a . (b . nil)) (cons 'a (cons 'b nil)) '(a b) (list a b) are four different ways of expressing the same literal list.
Justin Smith
+4  A: 

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).

Svante
+1  A: 

@masi

In the accepted answer, you should probably use emacs-lisp-mode instead of lisp-mode, since all of these files are going to be in elisp, not lisp. The distinction is minor, and probably not of any great significance, but the emacs guys must have written a separate emacs-lisp-mode for some reason right?

vedang