如何在Emacs中设置默认工作目录-故障排除

20
我知道这个问题已经被发布很多次了,但是这些解决方案对我没有用。我尝试了此帖子中的以下解决方案(更改Emacs中的默认文件夹):
  • 解决方案1:在.emacs文件中添加(cd "C:/Users/Name/Desktop")
  • 解决方案2:在.emacs文件中添加(setq default-directory "C:/Documents and Settings/USER NAME/Desktop/")
  • 解决方案3:右键单击emacs快捷方式,点击属性并将“启动位置”字段更改为所需目录。
只有解决方案3有效。不幸的是,我需要使用解决方案1或2。
我相信我的.emacs文件是正确的,因为当我从.emacs文件中添加/删除(load-theme 'light-blue t)时,我的主题会被正确地加载/卸载。
以下是我最近的.emacs文件:
(message "Default Dir: %S" default-directory)
(setq-default default-directory "C:/Users/Lucas/")
(message "Default Dir: %S" default-directory)
(setq-default indent-tabs-mode nil)
(add-hook 'ruby-mode-hook
      (lambda ()
        (define-key ruby-mode-map "\C-c#" 'comment-or-uncomment-region)
        )
 )  
(defadvice comment-or-uncomment-region (before slick-comment activate compile)
  "When called interactively with no active region, comment a single line instead."
  (interactive
   (if mark-active (list (region-beginning) (region-end))
     (list (line-beginning-position)
       (line-beginning-position 2)))))
(load-theme 'light-blue t)
;setq default-directory "~/")
(setq-default default-directory "C:/Users/Lucas/")

(defun xp-read-file-name (prompt &optional dir default-filename mustmatch initial predicate)
(setq last-nonmenu-event nil)
  (funcall (or read-file-name-function #'read-file-name-default)
           prompt dir default-filename mustmatch initial predicate))

(defun xp-save-as (filename &optional confirm)
  (interactive
   (list (if buffer-file-name
       (xp-read-file-name "Write file: " nil nil nil nil)
     (xp-read-file-name "Write file: " "C:/Users/Lucas/"
         (expand-file-name
          (file-name-nondirectory (buffer-name))
          "C:/Users/Lucas/")
         nil nil))
   (not current-prefix-arg)))
  (or (null filename) (string-equal filename "")
      (progn
  (if (file-directory-p filename)
      (setq filename (concat (file-name-as-directory filename)
           (file-name-nondirectory
            (or buffer-file-name (buffer-name))))))
  (and confirm
       (file-exists-p filename)
       (not (and (eq (framep-on-display) 'ns)
           (listp last-nonmenu-event)
           use-dialog-box))
       (or (y-or-n-p (format "File `%s' exists; overwrite? " filename))
     (error "Canceled")))
  (set-visited-file-name filename (not confirm))))
  (set-buffer-modified-p t)
  (and buffer-file-name
       (file-writable-p buffer-file-name)
       (setq buffer-read-only nil))
  (save-buffer)
  (vc-find-file-hook))
(defun comment-or-uncomment-region-or-line ()
    "Comments or uncomments the region or the current line if there's no active region."
    (interactive)
    (let (beg end)
        (if (region-active-p)
            (setq beg (region-beginning) end (region-end))
            (setq beg (line-beginning-position) end (line-end-position)))
        (comment-or-uncomment-region beg end)
        (next-line)))
(global-set-key (kbd "C-x C-;") 'comment-region)
(setq lazy-highlight-cleanup nil)
(message "Default Dir: %S" default-directory)

我的emacs.exe保存在C:\emacs\emacs-24.3\bin下(这也是我的默认目录,但我想将其更改为C:/Users/Lucas/),我的.emacs位于我的主目录下 - C:\Users\Lucas
更新
最近我发现我的命令(setq-default default-directory "C:/Users/Lucas/"),它应该设置我的default-directory,但没有生效。在启动后立即查看我的*Messages*缓冲区:
Default Dir: "C:\\emacs\\emacs-24.3\\bin/" [3 times]
For information about GNU Emacs and the GNU system, type C-h C-a.

它的行为就像我在覆盖.emacs文件一样。我进行了双倍/三倍检查,并且在我的路径中找不到另一个.emacs。此外,我没有从快捷方式运行Emacs。如果有任何建议,将不胜感激。感谢所有支持。


Emacs会在你的桌面上以这种方式启动,除非你打开了一个文件。否则它通常会在当前缓冲区中的文件所在的同一目录中启动。 - Bleeding Fingers
你试图解决什么问题?(即为什么default-directory的值是一个问题?) - phils
我正在尝试将默认工作目录设置为 C:/Users/Lucas/。我的当前设置似乎忽略了我在 .emacs 文件中的 (setq default-directory "C:/Users/Lucas/") 这一行。 - modulitos
你的 HOME 环境变量设置了冲突的值吗? - SlowLearner
加载工作正常与此无关。事先load-pathdefault-directory或者说invocation-directory之间没有任何联系。 - Drew
@SlowLearner 在我的“环境变量”(我正在使用Windows 7)下,我的HOME“系统变量”设置为“C:\Users\Lucas”,我认为这是正确的。 - modulitos
7个回答

13

对我而言问题出在了 Emacs 的初始启动屏幕

可以通过以下方法取消:

;; no startup msg  
(setq inhibit-startup-message t)

似乎emacs的默认欢迎屏幕(包含教程链接)强制你在$HOME路径下启动。


之后你可以使用cdsetq default-directory来更改路径,例如:

(setq default-directory "~/")

或者

(cd "~/")

2
是的,你说得对。我尝试了上面提到的所有其他方法,但都没有起作用。如果我从搜索提示符中启动emacs,则默认目录为System32。如果我从桌面上的快捷方式(运行emacs.com)启动它,则会打开安装emacs的bin文件夹。在禁用初始启动屏幕后,默认目录可以正常工作。我必须使用(setq default-directory“C:\ Path \ To \ My Folder”)。 - Nick_F
有人知道为什么启动画面会导致这种行为吗?感觉很奇怪。 - undefined

9

从您的描述来看,您正在运行 Windows 系统。请问您是否确认 Emacs 实际上正在找到并加载 C:\Users\Lucas 目录下的 .emacs 文件呢?如果是这样的话,那意味着 Windows 已经将您的主目录设定为 C:\Users\Lucas。您可以通过查看环境变量 HOME 来进行验证。在 Emacs 中,请尝试以下命令:

(getenv "HOME")

在你的 .emacs 文件中,你可以输入以下内容:
(setq default-directory "~/")

这听起来像是一个好的检查 - 但我该如何运行 (getenv "HOME")?我应该将 (getenv "HOME") 放在我的初始化(.emacs)文件中,还是有一种方法可以直接从我的编辑器作为 Lisp 代码运行它?(抱歉,我有点新手对于 Emacs)我最终计划使用 ~/ 目录使我的初始化文件跨平台工作。 - modulitos
此外,我相信我的HOME系统变量已经正确设置了。当我运行C-x C-f ~/.emacs时,我会进入到我的.emacs文件,它位于c:/Users/Lucas/.emacs下。 - modulitos
2
在emacs中,进入scratch缓冲区。它处于lisp交互模式下。也就是说,您可以键入emacs-lisp并对其进行评估。因此,只需键入(getenv "HOME")并按C-j(按住控制键并按下'j'字符)。Emacs将评估您键入的内容并返回结果。 - Jeffrey DeLeo
谢谢!是的,它有效 :) - 它返回 "C:\Users\Lucas" - modulitos

1
我一直在Windows 10上使用Emacs 24.3.1时遇到类似的问题。解决方法更加奇怪:自定义Emacs以抑制启动屏幕。这可以通过启动自定义按钮完成,因此您最终会在.emacs文件中得到以下内容:
(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(inhibit-startup-screen t))

有了启动屏幕(例如,在上面将t更改为nil),Windows快捷方式“开始位置”字段(设置为“E:\ Users \ Rob \”)占主导地位,Ctrl-x Ctrl-f会产生E:\ Users \ Rob /(注意奇怪的斜杠方向)。

如果禁用了启动屏幕(上面的t),则.emacs

 (setq default-directory "E:/Users/Rob/me/")

控制DOM并使用Ctrl-x Ctrl-f可获取E:/Users/Rob/me/(所有正斜杠)。

1
这是我所做的,而且没有出现任何问题:

setq default-directory "C:\\Path\\To\\Directory\\"

已保存并重新启动。我认为在Windows环境中,需要使用双反斜杠。


8
发布问题的人已经说明这个解决方案对他们不起作用。在回复之前请仔细阅读问题。 - i alarmed alien
然而,这个海报正在使用setq,而不是setq-default。 - fiacobelli

1

我刚刚测试了一下,关于默认目录的问题它表现得很好。我的设置如下:

c:\bin\emacs\bin中的emacs.exe

%PATH%包括c:\bin\emacs\bin\

%HOME%设置为%USERPROFILE(C:\Users\)

直接从运行框中打开

Win+remacs -qRET

(message "%s" default-directory) => "C:\Users\jonpe/"

从更改目录后的cmd.exe中打开

Win+rcmdRET

cd c:\users\publicRET

emacs -qRET

(message "%s" default-directory) => "C:\Users\Public/"

您是否将emacs添加到%PATH%变量中?

如果我尝试通过浏览到emacs的位置来启动它,则默认目录将设置为可执行文件位置。

0

在你的init文件中,将default-directory设置为最后一个,如果你希望在加载init文件后该设置有用。

如果你还需要在init文件的较早阶段进行设置(例如,以使一些init文件代码获得正确的值),那么也可以更早地进行设置。当你在init文件中执行操作时,这些操作可能会更改变量值。

如果你希望每个缓冲区的默认目录都是相同的,也可以使用(setq-default default-directory "C:/Documents and Settings/USER NAME/Desktop/")(或任何其他目录名称)。


测试一下在加载了你的 init 文件等之后它是什么。如果它不是你在 init 文件中设定的内容(在末尾),那么你需要查看 init 文件以外的其他内容。当我说“在末尾”,我的意思是真正的末尾——在 init 文件可能加载其他库等之后。如果在加载 init 文件后立即出现正确结果,那么说明在此之后你所做的某些操作导致了变化。 - Drew
你对如何测试我的 init 文件末尾的 default-directory 有什么建议吗?我应该通过某种方式打印 default-directory 进行测试吗?还是应该从 scratch buffer 中打印 default-directory?我对 Emacs 还有点陌生,所以我想确保我采取了正确的方法 :) - modulitos
在你的.emacs文件中添加以下代码(可以放在任何位置):(message "Default Dir: %S" default-directory)。然后在启动后查看你的*Messages*缓冲区,以查看它的输出。 - Drew
谢谢- 我尝试了这个,并使用我的新.emacs*Messages*缓冲区更新了OP。是什么阻止了我的(setq-default default-directory "C:/Users/Lucas/")命令? - modulitos
Windows的快捷方式可以正常工作;我只是无法弄清楚为什么我的.emacs文件不能做到这一点(我现在仍然无法)。我想我从现在开始只会使用Windows的快捷方式,但这让我感到困惑,因为我似乎无法将我的“default-directory”更改为“C:\ emacs \ emacs-24.3 \ bin”。 - modulitos
显示剩余4条评论

-1

我认为你只需要使用正确的反斜杠(并转义它们)。这在我的.emacs中有效:

(setq default-directory "C:\windows")


我尝试过这个,但结果并没有什么不同。当然,如果我不进行转义,那么加载 init 文件时会出现错误。 - modulitos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接