简体   繁体   English

在哪里可以找到在 Windows 上运行的 Emacs 的 .emacs 文件?

[英]Where can I find my .emacs file for Emacs running on Windows?

I tried looking for the .emacs file for my Windows installation for Emacs, but I could not find it.我尝试为 Emacs 的 Windows 安装寻找 .emacs 文件,但我找不到它。 Does it have the same filename under Windows as in Unix? Windows 下的文件名是否与 Unix 中的文件名相同?

Do I have to create it myself?我必须自己创建吗? If so, under what specific directory does it go?如果是这样,它在哪个特定目录下?

Copy and pasted from the Emacs FAQ, http://www.gnu.org/software/emacs/windows/ :从 Emacs FAQ 中复制并粘贴, http ://www.gnu.org/software/emacs/windows/:

Where do I put my init file? 我在哪里放置我的 init 文件?

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.在 Windows 上, .emacs文件可能被称为_emacs以向后兼容 DOS 和 FAT 文件系统,其中文件名不能以点开头。 Some users prefer to continue using such a name, because Windows Explorer cannot create a file with a name starting with a dot, even though the filesystem and most other programs can handle it.一些用户更喜欢继续使用这样的名称,因为 Windows 资源管理器无法创建名称以点开头的文件,即使文件系统和大多数其他程序可以处理它。 In Emacs 22 and later, the init file may also be called .emacs.d/init.el .在 Emacs 22 及更高版本中,init 文件也可以称为.emacs.d/init.el Many of the other files that are created by Lisp packages are now stored in the .emacs.d directory too, so this keeps all your Emacs related files in one place.许多由 Lisp 包创建的其他文件现在也存储在.emacs.d目录中,因此这将所有与 Emacs 相关的文件保存在一个地方。

All the files mentioned above should go in your HOME directory.上面提到的所有文件都应该放在您的 HOME 目录中。 The HOME directory is determined by following the steps below: HOME 目录通过以下步骤确定:

  1. If the environment variable HOME is set, use the directory it indicates.如果设置了环境变量 HOME,请使用它指示的目录。
  2. If the registry entry HKCU\\SOFTWARE\\GNU\\Emacs\\HOME is set, use the directory it indicates.如果设置了注册表项 HKCU\\SOFTWARE\\GNU\\Emacs\\HOME,请使用它指示的目录。
  3. If the registry entry HKLM\\SOFTWARE\\GNU\\Emacs\\HOME is set, use the directory it indicates.如果设置了注册表项 HKLM\\SOFTWARE\\GNU\\Emacs\\HOME,请使用它指示的目录。 Not recommended, as it results in users sharing the same HOME directory.不推荐,因为它会导致用户共享相同的 HOME 目录。
  4. If C:\\.emacs exists, then use C:/ .如果C:\\.emacs存在,则使用C:/ This is for backward compatibility, as previous versions defaulted to C:/ if HOME was not set.这是为了向后兼容,因为如果未设置 HOME,以前的版本默认为C:/
  5. 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.使用用户的AppData目录,通常是用户配置文件目录下的Application Data目录,其位置根据Windows版本和计算机是否属于域而有所不同。

Within Emacs, ~ at the beginning of a file name is expanded to your HOME directory, so you can always find your .emacs file with Cx Cf ~/.emacs .在 Emacs 中,文件名开头的~扩展到您的 HOME 目录,因此您始终可以使用Cx Cf ~/.emacs找到您的 .emacs 文件。

There's further information at HOME and Startup Directories on MS-Windows .HOME 和 MS-Windows 上的启动目录中有更多信息。

It should be stored in the variable user-init-file.它应该存储在变量 user-init-file 中。 Use CH v user-init-file RET to check.使用CH v user-init-file RET进行检查。 You can also open it directly by using Mx eval-expression RET (find-file user-init-file) RET也可以直接使用Mx eval-expression RET (find-file user-init-file) RET

Open the file like this in Emacs for Windows:在 Emacs for Windows 中打开这样的文件:

C-x C-f ~/.emacs

More information in the Emacs Wiki Emacs Wiki中的更多信息

在我的 Vista 机器上,它位于C:\\Users\\<USER>\\AppData\\Roaming\\

Note that it may NOT be enough to just type Ctrl-x Ctrl-f ~/.emacs and create the file.请注意,仅键入Ctrl-x Ctrl-f ~/.emacs并创建文件可能还不够。

It may be that your Emacs application uses a different place to store your init file, and if so, then creating the file ~/.emacs simply creates a useless file which your Emacs application ignores.可能是您的 Emacs 应用程序使用不同的位置来存储您的 init 文件,如果是这样,那么创建文件 ~/.emacs 只会创建一个您的 Emacs 应用程序忽略的无用文件

Also, you may want to do more than just access the .emacs init file, but you may want to know where it is, ie, its pathname.此外,您可能想做的不仅仅是访问.emacs init 文件,而且您可能想知道它在哪里,即它的路径名。

To get at this there are two methods:要解决这个问题,有两种方法:

Easy way: type Ctrl + H V user-init-file Return简单的方法:输入Ctrl + H V user-init-file Return

Slightly trickier way: You can find out where your system is storing its own .emacs file by:稍微棘手的方法:您可以通过以下方式找出系统存储其自己的 .emacs 文件的位置:

  1. Click options and scroll down to "Set Default Font..."单击选项并向下滚动到“设置默认字体...”
  2. Change the font setting and click okay更改字体设置并单击确定
  3. On the options menu, go down to "Save Options"在选项菜单上,转到“保存选项”
  4. When the options are saved, the system saves its .emacs file, and you can read the file path in the minibuffer at the bottom of the Emacs screen选项保存后,系统会保存其.emacs文件,您可以在Emacs屏幕底部的minibuffer中读取文件路径

On versions of Emacs on Windows above 22, it seems to have moved to在 22 以上的 Windows 上的 Emacs 版本上,它似乎已经转移到

~/.emacs.d/init.el

, ~ being the value of your environment variable HOME (see Control Panel → SystemAdvancedEnvironment variables ). , ~是环境变量 HOME 的值(请参阅控制面板 →系统高级环境变量)。

The file itself might not exist.文件本身可能不存在。 In that case just create it.在这种情况下,只需创建它。

In Windows 7 put your init.el file in C:\\Users\\user-name\\AppData\\Roaming\\.emacs.d\\ , where user-name is your user/login folder.在 Windows 7 中,将您的init.el文件放在C:\\Users\\user-name\\AppData\\Roaming\\.emacs.d\\ ,其中user-name是您的用户/登录文件夹。

Take care so your init.el file won't be named init.el.txt .注意不要将init.el文件命名为init.el.txt This is something Windows does if you create your file with some editor like Notepad.如果您使用记事本等编辑器创建文件,Windows 就会这样做。

You must create an emacs initialization file.您必须创建一个 emacs 初始化文件。 One is not automatically created.一个不是自动创建的。 I had a similar issue and this answer tracks down what I did.我有一个类似的问题,这个答案追踪了我所做的。

My issue was my ~/.emacs.el file was not loading.我的问题是我的~/.emacs.el文件没有加载。 Strange because this has always worked for me.奇怪,因为这一直对我有用。

This question/answer helped me but I had to put my init file in the %USERPROFILE%\\AppData\\Roaming\\.emacs.d\\init.el because this is apparently the default behavior on Windows.这个问题/答案对我有帮助,但我不得不将我的 init 文件放在%USERPROFILE%\\AppData\\Roaming\\.emacs.d\\init.el因为这显然是 Windows 上的默认行为。

To troubleshoot this, I ran the following in the emacs *scratch* buffer.为了解决这个问题,我在 emacs *scratch*缓冲区中运行了以下命令。

user-emacs-directory
"~/.emacs.d/"

When I saw user-emacs-directory was ~/.emacs.d , I simply moved my .emacs.el file to %USERPROFILE%\\.emacs.d\\init.el .当我看到user-emacs-directory~/.emacs.d ,我只是将我的 .emacs.el 文件移动到%USERPROFILE%\\.emacs.d\\init.el But this still didn't work.但这仍然没有奏效。 I continued with expand-file-name as shown below:我继续使用expand-file-name ,如下所示:

(expand-file-name user-emacs-directory)
"c:/Users/pats/AppData/Roaming/.emacs.d/"

Got to love how Windows works.必须喜欢 Windows 的工作方式。 (not) So I moved my emacs.el file to the %USERPROFILE%\\AppData\\Roaming\\.emacs.d\\init.el and this worked. (不是)所以我将我的emacs.el文件移动到%USERPROFILE%\\AppData\\Roaming\\.emacs.d\\init.el并且这有效。 The file was now being read.现在正在读取文件。 But I got other errors because my initialization file loaded other (personal emacs) files (in ~/myenv/emacs/*.el .但是我遇到了其他错误,因为我的初始化文件加载了其他(个人 emacs)文件(在~/myenv/emacs/*.el

Warning (initialization): An error occurred while loading ‘c:/Users/pats/AppData/Roaming/.emacs.d/init.el’:

Hum... Seems like all my files ~/myenv/emacs/*.el would need to be moved in order for this to work but I didn't want to do that.嗯...似乎我的所有文件~/myenv/emacs/*.el都需要移动才能使其正常工作,但我不想这样做。 Then I realized that because the HOME environment variable was not set, emacs was performing its default behavior .然后我意识到因为 HOME 环境变量没有设置, emacs 正在执行它的默认行为

SOLUTION解决方案

Once I set my windows HOME environment variable to %USERPROFILE% everything began to work like it has for the past 25 years.一旦我将 Windows HOME环境变量设置为%USERPROFILE%一切都开始像过去 25 年一样工作。 :-) :-)

To set the HOME environment variable, I typed WindowsKey+"edit environment variables for your account" to open the Environment Variables dialog box, and entered HOME=%USERPROFILE% .要设置 HOME 环境变量,我键入 WindowsKey+“为您的帐户编辑环境变量”以打开“环境变量”对话框,然后输入HOME=%USERPROFILE%

Now my emacs initialization file .emacs.el is is back to its rightful place $HOME/.emacs.el and not in %USERPROFILE%\\AppData\\Roaming\\.emacs.d\\init.el现在我的 emacs 初始化文件 .emacs.el 回到了它应有的位置$HOME/.emacs.el而不是在%USERPROFILE%\\AppData\\Roaming\\.emacs.d\\init.el

To be fair, if Windows had just one place to put files for user installed packages the solution of making HOME= %USERPROFILE\\AppData\\Roaming might be acceptable, but because some applications use %USERPROFILE% , some use %USERPROFILE\\AppData\\Roaming and others use %USERPROFILE\\AppData\\Local it just makes it difficult to know where to find your configuration files.公平地说,如果 Windows 只有一个地方为用户安装的软件包放置文件,那么制作 HOME= %USERPROFILE\\AppData\\Roaming的解决方案可能是可以接受的,但是因为有些应用程序使用%USERPROFILE% ,所以有些应用程序使用%USERPROFILE\\AppData\\Roaming和其他人使用%USERPROFILE\\AppData\\Local只会让您很难知道在哪里可以找到您的配置文件。

I prefer having everything in my %USERPROFILE% or $HOME directory.我更喜欢将所有内容都放在我的 %USERPROFILE% 或 $HOME 目录中。

Another similar question was here:另一个类似的问题在这里:

As kanja answered, the path to this file is stored in the user-init-file variable (or if no init file exists, the variable contains the default value for where to create it).正如 kanja 回答的那样,该文件的路径存储在user-init-file变量中(或者,如果不存在 init 文件,则该变量包含创建它的位置的默认值)。

So regardless of which of the possible init file names you are using, and which directory it is in, you should be able to visit your init file with:因此,无论您使用的是哪个可能的 init 文件名,以及它在哪个目录中,您都应该能够使用以下命令访问您的 init 文件:

M-: (find-file user-init-file) RET M-: (find-file user-init-file) RET

Or display its full path in the echo area with:或在回声区域显示其完整路径:

M-: (expand-file-name user-init-file) RET M-: (expand-file-name user-init-file) RET

在 Emacs 23 和 Windows 7 上,它仅在您设置时才有效:

HKCU\SOFTWARE\GNU\Emacs\HOME

I've found that Emacs 22 will occasionally open either "C:\\Documents and Settings\\username\\Application Data\\.emacs", or just "C:\\Documents and Settings\\username\\.emacs" on my XP machine.我发现 Emacs 22 偶尔会在我的 XP 机器上打开“C:\\Documents and Settings\\username\\Application Data\\.emacs”,或者只是“C:\\Documents and Settings\\username\\.emacs”。 I haven't found an explanation for why it occasionally changes it's mind.我还没有找到解释为什么它偶尔会改变主意的原因。

~ will always point to whatever the current instance of emacs thinks is HOME, but kanja's tip (Ch v user-init-file) will always tell you what ~/.emacs actually maps to. ~ 将始终指向 emacs 的当前实例认为是 HOME 的任何内容,但是 kanja 的提示 (Ch v user-init-file) 将始终告诉您 ~/.emacs 实际映射到的内容。

在 Windows XP 上,它是:

C:\Documents and Settings\yourusernamehere\Application Data\

On Windows 8.1, if Emacs is started from Windows Explorer , a shortcut or from cmd console it uses C:\\Users\\<USER>\\AppData\\Roaming.emacs init file.在 Windows 8.1 上,如果 Emacs 是从Windows 资源管理器、快捷方式或cmd控制台启动的,它会使用C:\\Users\\<USER>\\AppData\\Roaming.emacs init 文件。 When I start Emacs from PowerShell, Emacs looks for its init file in C:\\Users\\<USER> folder.当我从 PowerShell 启动 Emacs 时,Emacs 会在C:\\Users\\<USER>文件夹中查找其 init 文件。 The fix to this issue was to set the HOME user environment variable (Control Panel\\System and Security\\System->Advanced system settings->Advanced->Environment variables) to C:\\Users\\<USER> .解决此问题的方法是将HOME用户环境变量(控制面板\\系统和安全\\系统->高级系统设置->高级->环境变量)设置为C:\\Users\\<USER> After this change, no matter how I start Emacs, it uses the same init file (see the accepted answer of this question )此更改后,无论我如何启动 Emacs,它都使用相同的 init 文件(请参阅此问题的已接受答案)

有一个基于您的 Windows 版本和额外信息的目录列表: http : //www.gnu.org/software/emacs/manual/html_node/emacs/Windows-HOME.html

For WIndows7& Emacs26.3:对于 WINdows7&Emacs26.3:


if HOME environment is set, then the .emacs file should be in that folder.如果设置了HOME环境,则.emacs文件应该在该文件夹中。

otherwise, it should be in c:\\ .否则,它应该在c:\\


In both cases, if .emacs is not there, _emacs should be used.在这两种情况下,如果.emacs不存在,则应使用_emacs

This is because we cannot create .emacs file according to the windows file naming rules.(but we can download or copy it from somewhere else).这是因为我们无法根据 windows 文件命名规则创建.emacs文件。(但我们可以从其他地方下载或复制它)。

After Emacs 27.1 , emacs has started respecting the $XDG_CONFIG_HOME .Emacs 27.1 之后,emacs开始尊重$XDG_CONFIG_HOME The init file or the init directory can now be found in $XDG_CONFIG_HOME/emacs/init.el .现在可以在$XDG_CONFIG_HOME/emacs/init.el找到 init 文件或 init 目录。

In Windows $XDG_CONFIG_HOME could translate to %LOCALAPPDATA% .在 Windows 中$XDG_CONFIG_HOME可以转换%LOCALAPPDATA%

In any case you can use the following emacs variables to find out the location of the your initialization file by Mx eval-expression在任何情况下,您都可以使用以下 emacs 变量通过Mx eval-expression找出初始化文件的位置

user-init-file

or the emacs configuration directory或者 emacs 配置目录

user-emacs-directory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM