最近愛折騰的毛病又犯了,開始折騰起Emacs,在自定義Emacs的配置文件存放位置時遇到一點問題,不過,在GNU的Emacs站點,看到這么一段話:
On Windows, the .emacs file may be called _emacs for backward compatibility with DOS and FAT filesystems where filenames could not start with a dot. Some users prefer to continue using such a name, because Explorer cannot create a file with a name starting with a dot, even though the filesystem and most other programs can handle it. In Emacs 22 and later, the init file may also be called.emacs.d/init.el. Many of the other files that are created by lisp packages are now stored in the.emacs.ddirectory too, so this keeps all your Emacs related files in one place.
All the files mentioned above should go in your HOME directory. The HOME directory is determined by following the steps below:
- If the environment variableHOME is set, use the directory it indicates.
- If the registry entry
HKCU\SOFTWARE\GNU\Emacs\HOMEis set, use the directory it indicates.- If the registry entry
HKLM\SOFTWARE\GNU\Emacs\HOMEis set, use the directory it indicates. Not recommended, as it results in users sharing the same HOME directory.- If C:\.emacs exists, then use C:/. This is for backward compatibility, as previous versions defaulted to C:/ if HOME was not set.
- Use the user's AppData directory, usually a directory called Application Data under the user's profile directory, the location of which varies according to Windows version and whether the computer is part of a domain.
也就是說,Emacs的配置文件有三種格式:.emacs文件、_emacs文件或者.emacs.d目錄下的init.el(22或者更高版本)文件,但是,不管哪種格式,必須放對正確的目錄Emacs才能找到,這個正確的目錄就是HOME目錄。
熟悉Linux的朋友一般都知道home目錄,簡單的用~表示,或者用全路徑表示為/home/<xxx>,其中xxx表示用戶名字,所以,在Linux下將.emacs和.emacs.d放到這個目錄下就行了,但windows沒有home目錄的概念,所以,Emacs就按如下方式來查找配置文件:
- 如果設置了HOME環境變量,那么就用它的值作為home目錄~
- 如果存在注冊表鍵值HKCU\SOFTWARE\GNU\Emacs\HOME,就用它的值作為home目錄~
- 如果存在注冊表鍵值HKLM\SOFTWARE\GNU\Emacs\HOME,就用它的值作為home目錄~(和2的區別是,2只是針對當前用戶的注冊表路徑,3則是針對所有用戶)
- 如果存在C:\.emacs,就用C:\作為home目錄~
- 如果以上都不存在的話,就使用<system root>\Users\<user name>\AppData\Roaming作為home目錄~(對於XP和較早windows用戶,需要到Documents and Settings目錄下去找)
從以上策略來看,1是比較好的做法,所以優先級也最高,應該是屬於建議的方式,於是,新建一個HOME環境變量,將它的值設為想要放置Emacs配置文件的地方既可。
之前我是按以上方法設置的HOME目錄,不過現在我后悔了,因為我發現不光是Emacs會使用HOME這個環境變量,會使用這個變量的至少還有以下程序或者插件:
- VIM,VIM會把_viminfo文件放在這個目錄;
- VIM的neocomplcache插件,它會放置一個.neocon目錄進去;
- firefox的pentadactyl插件,它會放一個pentadactyl目錄進去。
所以,個人建議:
- 如果你希望有一個公共的目錄存放這些配置,就像Linux下的~目錄一樣,那么設置HOME這個環境變量最適合不過;
- 如果你希望這個目錄只存放Emacs的配置,那么,還是不要用HOME環境變量了,去注冊表里面添加一個HOME鍵值吧。(至少我個人比較偏向於這個方式)
