Re-open *scratch* buffer in Emacs?

后端 未结 16 1288
北海茫月
北海茫月 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 10:55
    (global-set-key (kbd "C-x M-z")
                    '(lambda ()
                       (interactive)
                       (switch-to-buffer "*scratch*")))
    

    This will not only quickly switch to *scratch* buffer(since I do this frequently), but recreate a *scratch* buffer and enable lisp-interaction-mode automatically if you kill it accidentally. Change the binding as you like.

    0 讨论(0)
  • 2020-12-12 10:58

    GNU Emacs default bindings:

    C-xb *scratch* RET

    or, more verbosely

    M-x switch-to-buffer *scratch* RET

    The *scratch* buffer is the buffer selected upon startup, and has the major mode Lisp Interaction. Note: the mode for the *scratch* buffer is controlled by the variable initial-major-mode.

    In general you can create as many "scratch" buffers as you want, and name them however you choose.

    C-xb NAME RET

    switches to a buffer NAME, creating it if it doesn't exist. A new buffer is not associated with a file on disk until you use C-xC-w (or M-x write-file RET) to choose a file where it should be saved.

    M-x text-mode RET

    changes the current buffer's major mode to Text mode. To find all the modes available (that is, without requiring any new packages), you can get a list by typing:

    M-x apropos-command -mode$ RET

    0 讨论(0)
  • 2020-12-12 10:59

    There are a whole bunch of tips on this EmacsWiki page.

    Here's the first one:

    A very simple function to recreate the scratch buffer:

    (defun create-scratch-buffer nil
       "create a scratch buffer"
       (interactive)
       (switch-to-buffer (get-buffer-create "*scratch*"))
       (lisp-interaction-mode))             
    
    0 讨论(0)
  • 2020-12-12 11:00

    I have scratch as an interactive command for opening a new scratch buffer (I like to have several):

    (defun scratch ()
      "create a new scratch buffer to work in. (could be *scratch* - *scratchX*)"
      (interactive)
      (let ((n 0)
            bufname)
        (while (progn
                 (setq bufname (concat "*scratch"
                                       (if (= n 0) "" (int-to-string n))
                                       "*"))
                 (setq n (1+ n))
                 (get-buffer bufname)))
      (switch-to-buffer (get-buffer-create bufname))
      (if (= n 1) initial-major-mode))) ; 1, because n was incremented
    

    adopted from: http://everything2.com/index.pl?node_id=1038451

    0 讨论(0)
  • 2020-12-12 11:06

    C-x b *scratch* RET y RET with iswitchb-mode enabled.

    Just C-x b *scratch* RET otherwise.

    0 讨论(0)
  • 2020-12-12 11:06

    Just to note emacs package unkillable-scratch in MELPA will do this. There is also scratch-persist that will automatically save and restore the buffer between sessions.

    0 讨论(0)
提交回复
热议问题