【问题标题】:How to declare the location of Emacs's init file as a variable如何将 Emacs 的 init 文件的位置声明为变量
【发布时间】:2013-07-09 15:10:44
【问题描述】:

我在几台计算机(Linux 和 Windows 机器)上使用 Emacs,我的 .emacs.d 通过 ownCloud 同步:.emacs(在 ~ 或 Appdata 中)只加载 {ownCloud 目录}/.emacs.d/init.el。

我的问题是,如何将 init.el 的路径作为依赖于操作系统/用户名的变量。

例如,Zenburn theme 上的 README 要求您的 init 包含

(add-to-list 'custom-theme-load-path "~/.emacs.d/themes/")

但是由于我的主目录和 .emacs.d 的路径因机器而异,所以这不起作用。 我正在考虑这样的事情来解决它:

(add-to-list 'custom-theme-load-path "{THE DIRECTORY THAT CONTAINS THIS INIT FILE}/themes/")

这可以吗?我才两周才开始使用 Emacs,所以不要太用力:P

【问题讨论】:

  • 哦,你可能想澄清你的问题。您的标题与正文不同。 (此外,init 文件的位置已经是一个变量)
  • 谢谢大家。对于第二个问题(正文)的示例,事实证明我需要在各个系统的 .emacs 中定义 user-emacs-directory ,然后在同步的 init 文件中使用(add-to-list 'custom-theme-load-path (concat user-emacs-directory "themes/")) 的效果以允许主题文件夹。不完全优雅,但它有效。哦,对不起,问题不明确,那是在凌晨。感谢@BruceConnor 提供有关如何修改我的初始化文件的信息。

标签: emacs cross-platform


【解决方案1】:

变量system-type 决定了Linux 和Windows 机器。变量user-login-name 是不言自明的。我使用这些来加载不同的文件,具体取决于我所在的机器。

(if (eq system-type 'gnu/linux)
   (when (string= user-login-name "home user name")
     (load-file "/path/to/file"))
   (when (string= user-login-name "work user name")
     (load-file "/path/to/other-file")))

老实说,这更适合其他配置。我不建议使用它来更改您的 emacs 目录位置。

相反,在每台机器上创建符号链接,以便假装您的 emacs 目录位于标准位置。 (几年来我一直在 4 台不同的机器上执行此操作,并且进展顺利)。


最后,回答你的最后一个问题。初始化文件已经是一个变量,它的名字是user-init-file。要提取它所在的目录,只需执行

 (file-name-directory user-init-file)

【讨论】:

  • 如果您的用户名相同,您还可以使用(getenv "variable name")查询环境变量,这应该足以区分任何两个系统。
【解决方案2】:

user-emacs-directory变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 2019-12-02
    • 1970-01-01
    • 2021-11-28
    相关资源
    最近更新 更多