2010-03-07 25 views
9

作爲一個文件中的註釋,我該如何編寫這個特定的文件應該在emacs中打開的模式?例如,假設我有一個名爲「foo」的腳本。在foo的身體,我願意把這樣的:聲明應該打開一個emacs文件的模式

# sh-mode 
# rest of my script here... 

Emacs的是,當「富」在Emacs打開應當使用「SH-模式」。請注意,我不想通過.emacs文件擴展名來完成此操作。這裏的要點是,「foo」的文件名沒有說明它是什麼類型的文件 - 我希望從文件本身中指定它。有沒有辦法做到這一點?

謝謝。在文件的頂部

# -*- mode: sh -*- 

回答

20

我通常添加類似的東西。請參閱emacs documentation瞭解更多信息。

+0

你也可以添加 ###局部變量:在文件的結尾' : ###模式:乳膠 ###尾 – 2010-03-07 19:44:06

2

另請參閱magic-mode-alist。

magic-mode-alist is a variable defined in `files.el'. 

Documentation: 
Alist of buffer beginnings vs. corresponding major mode functions. 
Each element looks like (REGEXP . FUNCTION) or (MATCH-FUNCTION . FUNCTION). 
After visiting a file, if REGEXP matches the text at the beginning of the 
buffer, or calling MATCH-FUNCTION returns non-nil, `normal-mode' will 
call FUNCTION rather than allowing `auto-mode-alist' to decide the buffer's 
major mode. 

If FUNCTION is nil, then it is not called. (That is a way of saying 
"allow `auto-mode-alist' to decide for these files.") 
7

還要注意,如果第一行是散列註釋,Emacs也將正確識別文件的類型,例如,

#!/bin/sh 
相關問題