I would like emacs to check for a
~/emacs.d/scratch.txt and use it's contents instead of the default message when emacs starts.
How can this be done?
This will do the job.
(let ((filename "~/.emacs.d/startup.txt")) (when (and (file-exists-p filename) (get-buffer "*scratch*")) (with-current-buffer "*scratch*" (erase-buffer) (insert-file-contents filename))))
initial-buffer-choice allows to specify a path to a file or directory:
(let ((filename "~/.emacs.d/startup.txt")) (when (file-exists-p filename) (setq initial-buffer-choice filename)))
This can be done using
initial-buffer-choice can be set to a filename, this will load the file as well as any files passed via the command line (splitting the window or not even showing the buffer depending on your setup).
So reading file data into
*scratch* buffer has the advantage that exact behavior is preserved, just replacing the text.
;; no startup screen (setq inhibit-startup-screen t) ;; use startup.txt instead (setq initial-buffer-choice (lambda () (let ((oldbuf (current-buffer))) ;; optional, I prefer only do this when no files are loaded. (if (buffer-file-name) oldbuf ;; only run when no files are loaded. (let ((filename "~/.emacs.d/startup.txt")) (with-current-buffer (get-buffer-create "*scratch*") (erase-buffer) (if (file-exists-p filename) (insert-file-contents filename) (insert (format ";; Scratch buffer, create '%s' to replace this text on startup." filename))) (set-buffer oldbuf)))))))
Others might suggest how this can be done better, it seems to work well enough though.