Indiana University
University Information Technology Services
  
What are archived documents?
Login>>
Login

Login is for authorized groups (e.g., UITS, OVPIT, and TCC) that need access to specialized Knowledge Base documents. Otherwise, simply use the Knowledge Base without logging in.

Close

In Emacs, how can I make the program start in a given mode?

Emacs has many different major modes available that help with editing specific types of files (e.g., English text, C programs, TeX manuscripts). There are three primary methods that Emacs uses to determine what mode to use, as described below:

  • The first method uses the file extension. For example, when Emacs is started on a file that ends in .c, c-mode is started. You can extend the list of recognized extensions by adding lines similar to the following to your .emacs file: (setq auto-mode-alist (cons '("\\.foo$" . text-mode) auto-mode-alist)) The above example ensures that, when you edit a file that ends in .foo, Emacs will start in text-mode.

  • The second method is a little less convenient and will not work well with all types of files. Anywhere in the first non-blank line of a file, you can place the following: -*-Mode-*- Replace Mode with the mode of your choice (e.g., C, TeX, Perl), and Emacs will then use that mode for its major mode. Obviously, this method isn't very useful unless you're editing a file format that has a commenting character available. A practical example of this is to place the following in the first line of a Lisp file: ;-*-Lisp-*-

    Since the semicolon is a comment character in Lisp programs, only Emacs looks at this line.

  • The third method for mode determination works a little better than the second with situations where the first line is used for something specific (e.g., the "#!/bin/sh" that occurs as the first line of a Bourne shell script wouldn't work properly if you use the second method). Emacs will look for a set of lines anywhere in a file that begins with "Local Variables:" and ends with "End:" and allow you to specify local variables and the mode. A good example is one that works well with Perl scripts. Add the following to the end of a Perl script to force perl-mode: # Local Variables: # mode: perl # End:

    Since the hash mark ( # ) is Perl's comment character, these lines will be ignored.

At Indiana University, for personal or departmental Linux or Unix systems support, see At IU, how do I get support for Linux or Unix?

This is document acix in domain all.
Last modified on August 22, 2008.

Comments/Questions/Corrections

Use this form to offer suggestions, corrections, and additions to the Knowledge Base. We welcome your input!

If you are affiliated with Indiana University and would like assistance with a specific computing problem, please use the Ask a Consultant form, or contact your campus Support Center.

Contact Information

Note: We will reply to your comment at this address. If your message concerns a problem receiving email, please enter an alternate email address.