在 sublime/vscode 编辑时,经常会新开一个 tab (但没保存)来进行一些临时编辑,退出重新打开后这些临时 tab 也存在,想问问 Emacs 下有没有好用的插件可以实现类似效果,我测试了下面几个,效果都不如那两个编辑器。
可以试试另一个方向:
我home文件夹里有一个test文件夹,里面是各种test.el,test.py,等等。相当于scratch了,只不过叫test。我需要临时编辑的时候都是直接用这些文件。
或者你也可以用一个文件,不加后缀名,用的时候改major-mode。
Emacs 没有 Tab 的概念,你的需求跟 Emacs 的场景不匹配,Emacs 有 Buffer 的概念,但可能除了特殊的 Buffer(比如操作文件的 Buffer),一般的 Buffer 没有持久化的意义。
2 个赞
;;; persistent scratch
(defun persistent-scratch-scratch-buffer-p ()
"Return non-nil if the current buffer's name expect to persists."
(string-match-p
(concat (regexp-quote "*scratch*")
"\\|" (concat "^reading-[0-9][0-9]/[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]$"))
(buffer-name)))
;; To both enable autosave and restore the last saved state, if any, on Emacs start, add
(persistent-scratch-setup-default)
;; Copied from https://github.com/Fanael/persistent-scratch/issues/23
;;
;; Before killing scratch buffer, save the current scratch buffer,
;; and create a backup filename for next time back up
(defun persistent-scratch-kill-buffer-query-function ()
"Back up things before killing scratch buffer."
(when (funcall persistent-scratch-scratch-buffer-p-function)
(persistent-scratch-save)
(persistent-scratch-new-backup))
t)
(use-package persistent-scratch
:commands persistent-scratch-kill-buffer-query-function
:init
(add-hook 'kill-buffer-query-functions
#'persistent-scratch-kill-buffer-query-function)
:custom
(persistent-scratch-backup-directory
(expand-file-name ".persistent-scratchs" user-emacs-directory))
(persistent-scratch-scratch-buffer-p-function 'persistent-scratch-scratch-buffer-p))
(provide 'custom-persistent-scratch)
;; 定义什么 buffer 需要保存
(defun persistent-scratch-scratch-buffer-p ()
"Function determining whether the current buffer is a scratch buffer."
(not (and (buffer-file-name)
(file-exists-p (buffer-file-name))
(file-writable-p (buffer-file-name)))))
1 个赞
desktop-save
desktop-read
1 个赞
好像没这么复杂,我现在用的配置:
(use-package persistent-scratch
:config
(setq persistent-scratch-autosave-interval 5)
(ignore-errors
(persistent-scratch-setup-default)))
2 个赞