Re-open *scratch* buffer in Emacs?

后端 未结 16 1353
北海茫月
北海茫月 2020-12-12 10:29

If I accidentally closed the scratch buffer in Emacs, how do I create a new scratch buffer?

16条回答
  •  清歌不尽
    2020-12-12 11:15

    I prefer to have my scratch buffer be an actual file that is automatically saved, and reopening it is as simple as opening a file. On startup, I kill the default and find my own like this.

    (add-hook 'emacs-startup-hook
      (lambda ()
        (kill-buffer "*scratch*")
        (find-file "/Users/HOME/Desktop/.scratch")))
    

    I have a custom kill-buffer function that does essentially the same thing -- reopens my personal scratch saved file and kills the default scratch if I killed the last visible buffer.

    I customized a few of the desktop.el functions to load after (kill-buffer "*scratch*") and (find-file "/Users/HOME/Desktop/.scratch") so that the file last visible on exiting Emacs doesn't get buried by the default scratch or buried by my custom scratch when launching Emacs.

    I enjoy using auto-save-buffers-enhanced, which automatically saves any file extension that is not specifically excluded:

    https://github.com/kentaro/auto-save-buffers-enhanced/blob/master/auto-save-buffers-enhanced.el

    (require 'auto-save-buffers-enhanced)
    (auto-save-buffers-enhanced t)
    (setq auto-save-buffers-enhanced-save-scratch-buffer-to-file-p 1)
    (setq auto-save-buffers-enhanced-exclude-regexps '("\\.txt" "\\.el" "\\.tex"))
    

    I use a slight variation of the function by @paprika when I want to create a no-file visiting buffer:

    (defun lawlist-new-buffer ()
      "Create a new buffer -- \*lawlist\*"
    (interactive)
      (let* (
        (n 0)
        bufname)
      (catch 'done
        (while t
          (setq bufname (concat "*lawlist"
            (if (= n 0) "" (int-to-string n))
              "*"))
          (setq n (1+ n))
          (if (not (get-buffer bufname))
            (throw 'done nil)) ))
      (switch-to-buffer (get-buffer-create bufname))
      (text-mode) ))
    

提交回复
热议问题