Re-open *scratch* buffer in Emacs?

后端 未结 16 1319
北海茫月
北海茫月 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:16

    I found this years ago when I first started using emacs; I have no idea where now but it has always had a home in my personal .el files. It does pop up in google searches.

    ;;; Prevent killing the *scratch* buffer -- source forgotten
    ;;;----------------------------------------------------------------------
    ;;; Make the *scratch* buffer behave like "The thing your aunt gave you,
    ;;; which you don't know what is."
    (save-excursion
      (set-buffer (get-buffer-create "*scratch*"))
      (make-local-variable 'kill-buffer-query-functions)
      (add-hook 'kill-buffer-query-functions 'kill-scratch-buffer))
    
    (defun kill-scratch-buffer ()
      ;; The next line is just in case someone calls this manually
      (set-buffer (get-buffer-create "*scratch*"))
    
      ;; Kill the current (*scratch*) buffer
      (remove-hook 'kill-buffer-query-functions 'kill-scratch-buffer)
      (kill-buffer (current-buffer))
    
      ;; Make a brand new *scratch* buffer
      (set-buffer (get-buffer-create "*scratch*"))
      (lisp-interaction-mode)
      (make-local-variable 'kill-buffer-query-functions)
      (add-hook 'kill-buffer-query-functions 'kill-scratch-buffer)
    
      ;; Since we killed it, don't let caller do that.
      nil)
    ;;;----------------------------------------------------------------------
    

提交回复
热议问题