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

时间:2014-01-11 07:32:01

标签: emacs working-directory

我知道这已被多次发布,但解决方案对我不起作用。我已经尝试过这篇文章中的以下解决方案(Changing the default folder in Emacs):

  • 解决方案1:将(cd "C:/Users/Name/Desktop")添加到.emacs文件
  • 解决方案2:将(setq default-directory "C:/Documents and Settings/USER NAME/Desktop/" )添加到.emacs文件
  • 解决方案3:右键单击emacs快捷方式,点击属性并将字段中的开头更改为所需目录。

并且只有解决方案3正在运行。不幸的是,我需要使用解决方案1或2。

我相信我的.emacs文件是正确的,因为当我从(load-theme 'light-blue t)文件添加/删除.emacs时,我的主题正在正确加载/卸载。

这是我最近的.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。任何建议,将不胜感激。感谢所有的支持。

7 个答案:

答案 0 :(得分:9)

从你写的内容来看,这听起来像是在运行Windows。 您是否已验证emacs实际上是从目录C:\ Users \ Lucas中查找并加载.emacs?如果是,这意味着Windows了解您的主目录是C:\ Users \ Lucas。您可以通过查看环境变量HOME来检查。在emacs中,尝试:

(getenv "HOME")

在.emacs文件中,您可以输入:

(setq default-directory "~/")

答案 1 :(得分:5)

我遇到的问题是emacs的初始启动屏幕

可以通过以下方式停用:

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

似乎emacs的默认欢迎屏幕(包含教程链接)强制您从$HOME路径开始。

之后你可以cdsetq default-directory这样:

(setq default-directory "~/")

(cd "~/")

答案 2 :(得分:2)

这就是我所做的,并且没有任何问题:

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

保存并重新启动。我认为在windows环境中,你需要有双反斜杠。

答案 3 :(得分:1)

我刚刚测试过,它在默认目录方面表现良好。我的设置如下:

<{1}}

中的

emacs.exe

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

c:\bin\emacs\bin\设置为%HOME%(C:\ Users \)

直接从运行框

  

赢+ R %USERPROFILE RET

     

emacs -q =&gt; &#34; C:\用户\ jonpe /&#34;

更改目录后从cmd.exe

  

赢+ R (message "%s" default-directory) RET

     

cmd RET

     

cd c:\users\public RET

     

emacs -q =&gt; &#34; C:\用户\公共/&#34;

您是否已将emacs添加到(message "%s" default-directory)变量中?

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

答案 4 :(得分: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快捷方式&#34;启动&#34;字段(设置为&#34; E:\ Users \ Rob \&#34;)占主导地位,Ctrl-x Ctrl-f产生E:\ Users \ Rob /(注意奇怪的斜线方向)。

禁止启动屏幕(上方).emacs

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

支配并且Ctrl-x Ctrl-f产生E:/ Users / Rob / me /(所有正斜杠)。

答案 5 :(得分:0)

如果您希望在加载init文件后该设置有用,请在初始文件中设置default-directory

如果您还需要在init文件中先前设置它(例如,以便您的某些初始文件代码获得正确的值),那么也可以更早地设置它。当您在init文件中执行操作时,这些内容可以更改变量值。

如果您希望将其作为每个缓冲区的默认值,也可以使用(setq-default default-directory "C:/Documents and Settings/USER NAME/Desktop/")(或目录名称)。

答案 6 :(得分:-1)

我认为你只需要使用正确的反斜杠(并逃避它们)。这适用于我的.emacs:

(setq default-directory“C:\ windows”)