help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to open a file in sh-mode


From: Xah
Subject: Re: How to open a file in sh-mode
Date: Thu, 14 Aug 2008 03:29:15 -0700 (PDT)
User-agent: G2/1.0

On Aug 14, 12:53 am, "Francis Moreau" <address@hidden> wrote:
> Hello,
>
> I'm trying to open a file with emacs automatically set on sh-mode.
> The file name can't be used to guess that emacs should be in sh-mode
> when opening it.

If the file content's first line starts with “#!”, then you could use
the magic-mode-alist.

-----------------------------------
How Emacs Choose Modes

Emacs determines what mode to use primarily by 2 mechanisms, in order:
(1) Check the first line in the file, using “magic-mode-alist”. (2)
Check the file name's suffix, using “auto-mode-alist”.

The “magic-mode-alist” is a list that emacs use to match the first
line of a file with a mode. For example, if you want files that begin
with the line “<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...” to
always use nxml-mode, then add the following to your “.emacs”:

(add-to-list
 'magic-mode-alist
 '("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0" . nxml-mode))

The magic-mode-alist is a list. In the above example, the string with
the “DOCTYPE” is a regex, used to match the first line of a file.

If emacs goes thru magic-mode-alist and didn't find any match, then
it'll use auto-mode-alist to check on file name suffix. The “auto-mode-
alist” associates a file name suffix with a mode. For example, if you
want files ending in “.js” to always open with js2-mode, then do:

(add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))

Note: The double backslash in the string “\\.js\\'” is used to escape
the backslash. So, the regex engine just got “\.js\'”. The “\.” is to
match a period. The “\'” is one of emacs special regex syntax, to
match end of a string. (See also: Text Pattern Matching in Emacs)

Reference: Elisp Manual: Regexp-Backslash.

You can see what are the values of magic-mode-alist or auto-mode-alist
by typing “Alt+x describe-variable”.

There are few minor details about how emacs determines what mode to
load, but the above should cover vast majority of needs. For detail,
see emacs manual.

Reference: (info "(emacs)Choosing Modes").

--------------------------------------------
above from:
http://xahlee.org/emacs/emacs_installing_packages.html

  Xah
∑ http://xahlee.org/

reply via email to

[Prev in Thread] Current Thread [Next in Thread]