Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs file edit history

Tags:

emacs

diff

Is there an emacs extension that takes periodic snapshots (once a minute, once every x keystrokes, whatever) of a file while it is being edited similar to the change history in Eclipse or the edit history in Google Docs and other programs?

I'm hoping for something that'll let me easy navigate through the changes I've made from day to day - Is there anything like this already written?

Edit

I should be more specific - I'm not looking for a VCS. I'm looking for a minor-mode or something similar I can just switch on and have hard copies of the revisions on disk.

like image 253
dfb Avatar asked May 10 '12 22:05

dfb


People also ask

How do I edit .emacs files?

The emacs editing mode is entered when you enable either the emacs or gmacs option. The only difference between these two modes is the way each handles the Ctrl-T edit command. To edit, move the cursor to the point needing correction and insert or delete characters or words, as needed.

How do I read a file in Emacs?

To read a disk file into an emacs buffer, type the command Control-X-Control-F. Emacs will ask you for the name of the file. As you type the name of the file, it will be displayed in the mini buffer.

How do I create a .emacs file?

To create a file, just visit it with C-x C-f as if it already existed. This creates an empty buffer, in which you can insert the text you want to put in the file. Emacs actually creates the file the first time you save this buffer with C-x C-s .

How do I save and exit Emacs?

To enter Emacs, type emacs at the shell prompt. When you want to leave Emacs for a short time, type a C-z and Emacs will be suspended. To get back into Emacs, type %emacs at the shell prompt. To quit Emacs permanently, type C-x C-c.


3 Answers

There's a built-in feature called autosave that saves after N keystrokes (and maybe after M seconds, I'm not sure). I generally use this if Emacs crashes, not for looking at what edits I've made; undo is better for that. Here's my config:

(setq autosave-dir (concat user-emacs-directory "autosaves/")
      auto-save-list-file-prefix (concat emacs-persistence-directory
                                         "autosave-list"))
(if (not (file-exists-p autosave-dir))
    (make-directory autosave-dir t))
(add-to-list 'auto-save-file-name-transforms
             `("\\`/?\\([^/]*/\\)*\\([^/]*\\)\\'" ,(concat autosave-dir "\\2") t))
;; tramp autosaves
(setq tramp-auto-save-directory (concat user-emacs-directory "tramp-autosaves/"))
(if (not (file-exists-p tramp-auto-save-directory))
    (make-directory tramp-auto-save-directory))

There's also a backup system that creates a copy after every save (not autosave). I use this for what I think you're asking for - looking at history since my last VCS commit. Here's my config:

(setq make-backup-files t
      vc-make-backup-files t
      version-control t
      kept-new-versions 256
      kept-old-versions 0
      delete-old-versions t
      backup-by-copying t)
(setq backup-dir (concat user-emacs-directory "backup/"))
(if (not (file-exists-p backup-dir))
    (make-directory backup-dir))
(add-to-list 'backup-directory-alist
             `(".*" . ,backup-dir))
(defun force-backup-of-buffer ()
  (setq buffer-backed-up nil))
(add-hook 'before-save-hook 'force-backup-of-buffer)
;; this is what tramp uses
(setq tramp-backup-directory-alist backup-directory-alist)

(add-to-path "backup-walker")
(autoload 'backup-walker-start "backup-walker"
  "start walking with the latest backup" t)

I use the excellent backup-walker to navigate through the backups.

like image 110
jpkotta Avatar answered Nov 06 '22 11:11

jpkotta


The best solution I have found for this is undo-tree, which is the Emacs equivalent to gundo for vim: it lets you visualise the tree of undo/redo, navigate through the changes and go back and forth between different versions.

undo-tree can be installed using ELPA; once it is installed, add the following to .emacs:

(require 'undo-tree)
(global-undo-tree-mode)

The undo tree can then be visualised using Ctrl-x u (undo-tree-visualize). The different “versions” can be navigated in a very intuitive manner, using arrow keys.

The history can also be made persistent using undo-tree-save-history.

like image 43
Sébastien Le Callonnec Avatar answered Nov 06 '22 09:11

Sébastien Le Callonnec


If you really value keeping your changes, I would highly recommend starting to use git. It will work for your windows and linux coding environments. I use it to port changes from code back and forth. It does a great job of fixing all the little line endings and merging changes.

If you really just want it to keep old versions, then emacs can create a new backup every time that you save. It just creates another file right next to your current one. That way you can control how often it makes a new backup (every time you save).

Here's a good page that talks about the options:

ftp://ftp.gnu.org/pub/old-gnu/Manuals/emacs-20.7/html_chapter/emacs_18.html#SEC109

like image 4
qwerty9967 Avatar answered Nov 06 '22 09:11

qwerty9967