要添加到已接受的答案,如果您启用了ILO模式(并且之后是自动完成的 C-X b 因此不要让你写 *scratch* ),然后尝试:
*scratch*
C-X b C-B *scratch* RET
C-x b C-b *scratch* RET
只是要注意emacs包 unkillable-scratch 在MELPA中会这样做。还有 scratch-persist 这将自动保存和恢复会话之间的缓冲区。
unkillable-scratch
scratch-persist
我已将目前发布的解决方案合并为一个功能:
(defun --scratch-buffer(&optional reset) "Get the *scratch* buffer object. Make new scratch buffer unless it exists. If RESET is non-nil arrange it that it can't be killed." (let ((R (get-buffer "*scratch*"))) (unless R (message "Creating new *scratch* buffer") (setq R (get-buffer-create "*scratch*") reset t)) (when reset (save-excursion (set-buffer R) (lisp-interaction-mode) (make-local-variable 'kill-buffer-query-functions) (add-hook 'kill-buffer-query-functions '(lambda()(bury-buffer) nil) ))) R))
要将此功能应用于您的 的.emacs 使用:
(--scratch-buffer t) (run-with-idle-timer 3 t '--scratch-buffer)
这将使暂存缓冲区首先变得坚不可摧,如果保存,它将被重新创建。另外我们可以使用快捷功能 scratch 快速调出缓冲区:
scratch
(defun scratch() "Switch to *scratch*. With prefix-arg delete its contents." (interactive) (switch-to-buffer (--scratch-buffer)) (if current-prefix-arg (delete-region (point-min) (point-max)) (goto-char (point-max))))
在过去,了解启动Emacs的原始启动目录已经证明是有用的。这是价值 desktop-dirname 或者 default-directory 临时缓冲区的局部变量:
desktop-dirname
default-directory
(defvar --scratch-directory (save-excursion (set-buffer "*scratch*") default-directory) "The `default-directory' local variable of the *scratch* buffer.") (defconst --no-desktop (member "--no-desktop" command-line-args) "True when no desktop file is loaded (--no-desktop command-line switch set).") (defun --startup-directory () "Return directory from which Emacs was started: `desktop-dirname' or the `--scratch-directory'. Note also `default-minibuffer-frame'." (if (and (not --no-desktop) desktop-dirname) desktop-dirname --scratch-directory))
所以 --startup目录 将始终返回您的makefile,TODO文件等的基本目录。如果没有桌面( --no桌面 命令行开关或没有桌面文件) --scratch-directory 变量将保存目录Emacs曾经在下面启动过。
--scratch-directory
就像文档字符串所说,这个函数将:
切换到临时缓冲区。如果缓冲区不存在,则创建它并将初始消息写入其中。“
这将带来一个新的临时缓冲区,看起来像初始临时缓冲区。
(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)
C-X b 然后输入 *scratch* ??
创建一个处于lisp交互模式的新缓冲区。
GNU Emacs默认绑定:
C-X b *scratch* RET
或者,更详细
M-X switch-to-buffer *scratch* RET
switch-to-buffer *scratch*
该 *scratch* buffer是启动时选择的缓冲区,具有主模式 Lisp交互 。注意:模式为 *scratch* 缓冲区由变量控制 initial-major-mode 。
initial-major-mode
通常,您可以根据需要创建任意数量的“临时”缓冲区,并根据您的选择命名它们。
C-X b NAME RET
NAME
切换到缓冲区 NAME ,如果它不存在则创建它。在使用之前,新缓冲区与磁盘上的文件无关 C-X C-w ^ (要么 M-X write-file RET )选择应保存的文件。
write-file
M-X text-mode RET
text-mode
将当前缓冲区的主要模式更改为文本模式。要查找所有可用模式(即不需要任何新包),可以通过键入以下内容获取列表:
M-X apropos-command -mode$ RET
apropos-command -mode$
在EmacsWiki中找到答案: http://www.emacswiki.org/emacs/RecreateScratchBuffer
(defun create-scratch-buffer nil "create a scratch buffer" (interactive) (switch-to-buffer (get-buffer-create "*scratch*")) (lisp-interaction-mode))