且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-08 13:40:40

在您的启动文件中,您有以下一行:

 '(initial-buffer-choice t)) 

作为custom-set-variables命令的一部分。 initial-buffer-choice的文档字符串是:


启动Emacs后显示的缓冲区。如果值为零,
inhibit-startup-screen'为nil,则显示启动屏幕。如果
值是字符串,请使用
find-file'访问指定的文件或目录。如果t,打开' scratch '缓冲区。


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

 '(初始缓冲区选择c:/ Users / Seb / Documents / Emacs / TODO_List.org))


I have a TODO file that I load emacs up to use 90% of the time. When I load emacs though it defaults to loading the scratch buffer. I would like it to load the TODO file initially. I'm very new to Emacs and have tried searching round for ways to do this using the .emacs file but nothing has worked so far.

Here are my attempts:

1: Use find-file to get the file and switch-to-buffer to load it to the screen

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

2: Use pop-to-buffer to load the file instead

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

3: Save the desktop so it loads the next time

(desktop-save-mode 1)

None of these are working.

Here is my full .emacs file, as you can see it's barely used!

(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!"))

In your startup file, you have this line:

'(initial-buffer-choice t))

as part of your "custom-set-variables" command. The documentation string for "initial-buffer-choice" is:

Buffer to show after starting Emacs. If the value is nil and inhibit-startup-screen' is nil, show the startup screen. If the value is string, visit the specified file or directory using find-file'. If t, open the `scratch' buffer.

So, the value that you've specified ('t') is causing the *scratch* buffer to be displayed after startup. Change this line to the following and your issue should be resolved:

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