3

I'm using Emacs and Windows 10 and I am fairly new to both Emacs and elisp (and I am not a programmer).

I'm looking for a way to automatically save a buffer immediately after any modification regardless of how small it is.

One method I found to do this involves using the package "real–auto–save" found in the melpa package library which can save a file up to a second after a modification.

Is there a way to automatically save buffers without the use of extraneous packages?

6
  • 1
    Are you aware of the standard autosave files? Could these achieve what you need? Do you really need to save to the original file after each keystroke? Or do you really want to minimise the risk of losing any significant change?
    – mikado
    Commented Oct 8, 2018 at 19:29
  • No I don't know anything about the standard autosave files will look it up. I need autosave after any change to a buffer because it is the first step of a larger setup involving editing tex files with AUCTeX and having instant preview of the pdf output. My grand aim is to do math using my voice which for me requires a 'live preview' of the output of latex source files. To do that requires that I first save the latex source file after a change, then compile the latex file and then auto refresh/revert the buffer with the pdf output. But step one is to autosave the file
    – John Doyle
    Commented Oct 8, 2018 at 20:04
  • I know that there a number of latex preview modes around. I don't have experience of them, but you might find that your problem has already been solved.
    – mikado
    Commented Oct 8, 2018 at 20:18
  • 1
    Why don't you want to use packages that don't come with Emacs?
    – zck
    Commented Oct 8, 2018 at 21:21
  • @mikado the only package that looked suitable was "latex preview pane" but this does not support the real-time preview I'm looking for
    – John Doyle
    Commented Oct 9, 2018 at 14:48

1 Answer 1

2

To automatically save buffer after each modification, add the following elisp snippet to emacs config

(defun savebuf(begin end length)
  (if (and (buffer-file-name) (buffer-modified-p))
       (save-buffer)))
(add-hook 'after-change-functions 'savebuf)

documentation for change hooks: https://www.gnu.org/software/emacs/manual/html_node/elisp/Change-Hooks.html

example snippets: https://www.emacswiki.org/emacs/ChangeHook

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.