Re-open *scratch* buffer in Emacs?

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

    To add to the accepted answer, if you have ILO mode on (and it is autocompleting after C-x b, thus not letting you write *scratch*), then try:

    C-xb C-b*scratch* RET

    C-x b C-b *scratch* RET

    0 讨论(0)
  • 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)
    ;;;----------------------------------------------------------------------
    
    0 讨论(0)
  • 2020-12-12 11:16

    I used to use dwj's solution, and I was quite happy about it, until the day I realized that it failed when you actually rename the scratch buffer (for example by saving it).

    Then I adopted this, which works well for me :

      (run-with-idle-timer 1 t
        '(lambda () (get-buffer-create "*scratch*")))
    
    0 讨论(0)
  • 2020-12-12 11:20

    Like the docstring says, this function will:

    Switch to the scratch buffer. If the buffer doesn't exist create it and write the initial message into it."

    This will bring a new scratch buffer up which looks like the initial scratch buffer.

    (defun switch-buffer-scratch ()
      "Switch to the scratch buffer. If the buffer doesn't exist,
    create it and write the initial message into it."
      (interactive)
      (let* ((scratch-buffer-name "*scratch*")
             (scratch-buffer (get-buffer scratch-buffer-name)))
        (unless scratch-buffer
          (setq scratch-buffer (get-buffer-create scratch-buffer-name))
          (with-current-buffer scratch-buffer
            (lisp-interaction-mode)
            (insert initial-scratch-message)))
        (switch-to-buffer scratch-buffer)))
    
    (global-set-key "\C-cbs" 'switch-buffer-scratch)
    
    0 讨论(0)
提交回复
热议问题