Search code examples
emacsfile-type

how do I get emacs to treat a file with the wrong extension correctly?


So (let us not concern ourselves with why) I have a .emacs file which is called dotemacs, and a .bashrc file which is called dotbashrc.

When I load up dotemacs, I get no syntax highlighing (amongst other things). If I do M-x lisp-mode then all is well.

Without changing the name of the file, how do I get emacs to recognise automatically that dotemacs is a lisp file and go into lisp-mode? Similarly for bash scripts, and indeed any other type of file with the wrong (or no) extension.


Solution

  • You can put this at the top of the dotemacs file:

    ; -*- mode: lisp -*-
    

    causing it to start elisp-mode when you load the file.

    For shell scripts, putting a #!/bin/bash (for whichever shell you are using) is enough to turn on the correct mode. Or otherwise put this at the top of the file:

    # -*- mode: sh -*-