在 JetBrains 产品中,有一个非常方便的键绑定,可让您访问您进行编辑的所有位置。按一次键可转到最后一次编辑(文件和位置),并继续按此键可返回先前的编辑。通常在编辑时,您希望一遍又一遍地编辑相同的地方,如果一个有许多缓冲区打开,其中许多没有被编辑,这将更加有用。
Emacs 有一个标记环,但这并不完全相同。
在相关说明中,magit(emacs git 插件)中是否有跳转到编辑的功能?
GotoLastChange允许您沿着撤销位置链移动。您可以将其分配给一个键:
(global-set-key "\C-x\C-\\" 'goto-last-change)
There is GotoChg which allows you to travel back and forth the chain of undo locations. Sample init code snippet:
(require 'goto-chg)
(global-set-key [(control ?.)] 'goto-last-change)
(global-set-key [(control ?,)] 'goto-last-change-reverse)
(Just like the other alternatives, GotoLastChange and session.el, it can not jump between buffers)
session.el中有命令session-jump-to-last-change,它允许您沿着撤销位置链移动。初始化代码片段:
(require 'session)
(setq session-jump-undo-threshold 80) ; default was 240
(global-set-key [(control ?.)] 'session-jump-to-last-change)
(就像其他替代方案,GotoLastChange 和 GotoChg,它不能在缓冲区之间跳转)
跟踪编辑并返回到它们发生的位置取决于它们的类型。
如果你的编辑添加了一些东西,你可以用一个相当简单的方法返回它:
(goto-char (car(cadr buffer-undo-list)))
如果您删除了,您可以通过以下方式返回:
(goto-char (abs (cdr(cadr buffer-undo-list))))
并且您可能希望显示您在 minibuffer 中删除的内容:
(progn
(goto-char (abs (cdr(cadr buffer-undo-list))))
(message "DEL->: %s" (substring-no-properties (car(cadr buffer-undo-list)))))
加起来:
(defun last-edit ()
"Go back to last add/delete edit"
(interactive)
(let* ((ubuf (cadr buffer-undo-list))
(beg (car ubuf))
(end (cdr ubuf)))
(cond
((integerp beg) (goto-char beg))
((stringp beg) (goto-char (abs end))
(message "DEL-> %s" (substring-no-properties beg)))
(t (message "No add/delete edit occurred")))))
阅读C-h v buffer-undo-list
后,您可能会将其集成到不太细微的编辑中,例如设置文本属性(假设您确实需要它)。
我使用buffer-undo-list
变量来执行任务。每个缓冲区都有一个不同的列表,据我所知,没有全局撤消列表。很可能您知道您在哪个缓冲区中输入了某些内容,并且您希望 Emacs 将其带到编辑的位置。在这种情况下,有一个global-mark-ring
变量记录了您曾经使用过的缓冲区序列。
连续使用命令 Meta-Xpop-global-mark
或简单地
Ctrl- X Ctrl-Space将带您到较早访问的缓冲区(和标记位置)。一旦到达目标缓冲区,就可以触发Meta- Xlast-edit
(或绑定键)。
全局,多缓冲区goto-last-change
:
;;; record two different file's last change. cycle them
(defvar feng-last-change-pos1 nil)
(defvar feng-last-change-pos2 nil)
(defun feng-swap-last-changes ()
(when feng-last-change-pos2
(let ((tmp feng-last-change-pos2))
(setf feng-last-change-pos2 feng-last-change-pos1
feng-last-change-pos1 tmp))))
(defun feng-goto-last-change ()
(interactive)
(when feng-last-change-pos1
(let* ((buffer (find-file-noselect (car feng-last-change-pos1)))
(win (get-buffer-window buffer)))
(if win
(select-window win)
(switch-to-buffer-other-window buffer))
(goto-char (cdr feng-last-change-pos1))
(feng-swap-last-changes))))
(defun feng-buffer-change-hook (beg end len)
(let ((bfn (buffer-file-name))
(file (car feng-last-change-pos1)))
(when bfn
(if (or (not file) (equal bfn file)) ;; change the same file
(setq feng-last-change-pos1 (cons bfn end))
(progn (setq feng-last-change-pos2 (cons bfn end))
(feng-swap-last-changes))))))
(add-hook 'after-change-functions 'feng-buffer-change-hook)
;;; just quick to reach
(global-set-key (kbd "M-`") 'feng-goto-last-change)
来自http://shenfeng.me/emacs-last-edit-location.html
此实现适用于任何缓冲区中的最后两个更改。我想将其更改列表的长度扩展到两个以上不会太难。