如何在 Emacs 中将文件加载到缓冲区并在启动时切换到缓冲区

Posted

技术标签:

【中文标题】如何在 Emacs 中将文件加载到缓冲区并在启动时切换到缓冲区【英文标题】:How to load file into buffer and switch to buffer on start up in Emacs 【发布时间】:2011-08-16 18:32:03 【问题描述】:

我有一个 TODO 文件,我在 90% 的时间内加载了 emacs。当我加载 emacs 时,它默认加载暂存缓冲区。我希望它最初加载 TODO 文件。我对 Emacs 很陌生,并尝试过使用 .emacs 文件来寻找方法,但到目前为止没有任何效果。

这是我的尝试:

1:使用 find-file 获取文件并切换到缓冲区将其加载到屏幕

(switch-to-buffer (find-file "c:/Users/Seb/Documents/Emacs/TODO_List.org"))

2: 改为使用 pop-to-buffer 加载文件

(pop-to-buffer (find-file "c:/Users/Seb/Documents/Emacs/TODO_List.org"))

3:保存桌面以便下次加载

(desktop-save-mode 1)

这些都不起作用。

这是我的完整 .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-buffer-menu t)
'(inhibit-startup-screen t)
'(initial-buffer-choice t))
(custom-set-faces
  ;; custom-set-faces 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.
 )

;; Set the current directory to the Emacs Documents dir
(cd "C:/Users/Seb/Documents/Emacs")

;; Open TODO list on start up
(pop-to-buffer (find-file "c:/Users/Seb/Documents/Emacs/TODO_List.org"))

;; Turn off the annoying tool bar at startup - to turn back on 
;; just type M-x tool-bar-mode
(tool-bar-mode -1)

;; Move the mouse when cursor is near
(mouse-avoidance-mode 'cat-and-mouse)

;; This enables saving the current desktop on shutdown.
(desktop-save-mode 1)

;; XML Pretty Print
(defun xml-pretty-print (begin end)
  "Pretty format XML markup in region. You need to have nxml-mode
http://www.emacswiki.org/cgi-bin/wiki/NxmlMode installed to do
this.  The function inserts linebreaks to separate tags that have
nothing but whitespace between them.  It then indents the markup
by using nxml's indentation rules."
  (interactive "r")
  (save-excursion
      (nxml-mode)
      (goto-char begin)
      (while (search-forward-regexp "\>[ \\t]*\<" nil t) 
        (backward-char) (insert "\n"))
      (indent-region begin end))
    (message "Ah, much better!"))

【问题讨论】:

[+1] 非常好的问题,但没有得到应有的关注。干杯 【参考方案1】:

在你的启动文件中,你有这行:

'(initial-buffer-choice t))

作为“自定义设置变量”命令的一部分。 “initial-buffer-choice”的文档字符串是:

启动 Emacs 后显示的缓冲区。如果值为 nil 并且 inhibit-startup-screen' is nil, show the startup screen. If the value is string, visit the specified file or directory using 查找文件'。如果 t,则打开 `scratch' 缓冲区。

因此,您指定的值 ('t') 导致 *scratch* 缓冲区在启动后显示。将此行更改为以下内容,您的问题应该得到解决:

'(initial-buffer-choice "c:/Users/Seb/Documents/Emacs/TODO_List.org"))

【讨论】:

谢谢 Zev。那是一种享受。我现在查看了文档,并意识到有一些我不知道的初始命令。任何有兴趣的人都可以在这里找到文档:link 如何启动 Emacs 并让它打开两个窗口,每个窗口打开一个不同的文件? 感谢您的文档! @BebopSong

以上是关于如何在 Emacs 中将文件加载到缓冲区并在启动时切换到缓冲区的主要内容,如果未能解决你的问题,请参考以下文章

启动make并通过Emacs中的快捷方式转到错误位置

将信息保存到文件中并在启动时加载?

.emacs 文件权限?

使用 tramp 自动保存其他窗口时 Emacs 更改缓冲区

在 git 中切换分支时,如何让 Emacs 恢复所有未更改的缓冲区?

Emacs警告加载“... / .emacs.el”时发生错误: