如果不小心关闭了Emacs中的暂存缓冲区,如何创建新的暂存缓冲区?
如果不小心关闭了Emacs中的暂存缓冲区,如何创建新的暂存缓冲区?
Answers:
GNU Emacs的默认绑定:
C-xb
*scratch*
RET
或者,更详细地说
M-x
switch-to-buffer *scratch*
RET
该*scratch*
缓冲区是启动时选择的缓冲区,并且具有主模式Lisp Interaction。注意:*scratch*
缓冲区的模式由变量控制initial-major-mode
。
通常,您可以根据需要创建任意数量的“临时”缓冲区,并根据需要选择它们的名称。
C-xb
NAME
RET
切换到缓冲区NAME
,如果不存在则创建它。直到您使用C-xC-w(或M-x write-file
RET)选择应该保存文件的文件之后,新缓冲区才与磁盘上的文件关联。
M-x
text-mode
RET
将当前缓冲区的主模式更改为文本模式。要查找所有可用模式(即不需要任何新软件包),可以通过键入以下内容获取列表:
M-x
apropos-command -mode$
RET
.emacs
定义了其他默认的暂存模式,则该模式将是新暂存的模式-而不是列表交互模式。
我在.emacs中添加以下内容:
;; bury *scratch* buffer instead of kill it
(defadvice kill-buffer (around kill-buffer-around-advice activate)
(let ((buffer-to-kill (ad-get-arg 0)))
(if (equal buffer-to-kill "*scratch*")
(bury-buffer)
ad-do-it)))
如果我不想看到暂存缓冲区,请按Cx Ck,但它不会杀死它,只需将其放在缓冲区列表的末尾即可,因此下次无需再次创建新缓冲区时就需要它。
此EmacsWiki页面上有很多技巧。
这是第一个:
一个非常简单的函数来重新创建临时缓冲区:
(defun create-scratch-buffer nil
"create a scratch buffer"
(interactive)
(switch-to-buffer (get-buffer-create "*scratch*"))
(lisp-interaction-mode))
*scratch*
启用iswitchb模式的Cx b RET和RET。
*scratch*
否则,只需Cx b RET。
initial-major-mode
变量(默认情况下Lisp的互动模式)。
我几年前刚开始使用emacs时就发现了它。我不知道现在在哪里,但在我的个人.el文件中总有一个住所。它确实会在Google搜索中弹出。
;;; 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)
;;;----------------------------------------------------------------------
我有scratch
一个用于打开新暂存缓冲区的交互式命令(我喜欢几个):
(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
bufname
RET)相比,这有什么优势?
ido-mode
,通常会打开很多缓冲区。使用创建新的缓冲区C-x b
确实很繁琐。我将不得不提出一个唯一的名称,该名称与任何当前存在的缓冲区都不匹配。
就像文档字符串说的那样,此函数将:
切换到暂存缓冲区。如果缓冲区不存在,请创建它,然后将初始消息写入其中。”
这将启动一个新的暂存缓冲区,其外观类似于初始暂存缓冲区。
(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)
我更喜欢将暂存缓冲区设置为自动保存的实际文件,然后重新打开它就像打开文件一样简单。在启动时,我取消了默认值,并找到了自己的默认值。
(add-hook 'emacs-startup-hook
(lambda ()
(kill-buffer "*scratch*")
(find-file "/Users/HOME/Desktop/.scratch")))
我有一个自定义的kill-buffer函数,它的功能基本相同-重新打开我的暂存暂存文件,如果我杀死了最后一个可见缓冲区,则会杀死默认的暂存器。
我自定义了一些desktop.el
要在之后 加载的函数(kill-buffer "*scratch*")
,(find-file "/Users/HOME/Desktop/.scratch")
以便在启动Emacs时,退出Emacs时最后可见的文件不会被默认的草稿埋藏,也不会被我的自定义草稿埋藏。
我喜欢使用auto-save-buffers-enhanced
,它会自动保存未明确排除的任何文件扩展名:
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"))
当我想创建一个无文件访问缓冲区时,我使用@paprika对函数进行了一些改动:
(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) ))
我将到目前为止发布的解决方案合并为一个功能:
(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
快速打开缓冲区:
(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
局部变量:
(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-directory将始终返回您的makefile,TODO文件等的基本目录。如果没有桌面(--no-desktop命令行开关或无桌面文件),则该--scratch-directory
变量将保存目录Emacs曾经是开始下。