1

I'm a new Doom Emacs user that can't get used to having to save files manually.

I found a way to auto save evil buffers, but the problem with this approach is that I'm not able step out of vterm (an Emacs terminal emulator) without Emacs prompting me to save the vterm buffer, which obviously doesn't make any sense.

(add-hook 'evil-insert-state-exit-hook
      (lambda ()
        (call-interactively #'save-buffer)))

I was thinking maybe I could save specific file types only, so Emacs doesn't try to save the vterm buffer, how would I modify the above elisp to auto save files only for specific file types?

2
  • What you want to do is not clear to me: what is vterm and what does it have to do with the problem? What is the "vterm buffer"? What specific file types do you want to save? How exactly is Emacs supposed to know what you want to save and when?
    – NickD
    Oct 11 '20 at 14:56
  • vterm is a terminal application inside of Emacs, I'm not able to use this terminal properly with the auto save elisp code (example code in my question) enabled, when I'm trying to go out of the terminal Emacs prompts me to save the vterm (terminal) buffer. Example file types for which I would like to have auto save enabled: .js, .org, .json, .yaml etc. This way, hopefully, Emacs doesn't save every file type including the vterm buffer. Once I exit insert mode in evil, Emacs should auto save the file (equivalent to :w in evil/vim).
    – Hyperfocus
    Oct 11 '20 at 15:41
1

You may want to try the package super-save, which auto saves files when idle or Emacs lose focus.

My own auto save function saving when idle or lose focus, built from Emacs built-ins (github link in case I'm missing anything):

(defvar jester-auto-save-idle 1 "Time in seconds before auto-saving all buffers.")
(run-with-idle-timer jester-auto-save-idle t #'jester/save-all-buffers)
;; (cancel-function-timers 'jester/save-all-buffers) ; for debugging
(add-hook 'focus-out-hook #'jester/save-all-buffers)

(defun jester/save-all-buffers ()
  "Save all buffers."
  ;; yas overlay and company-select-next has problem with this.
  (when (and (not yas--active-snippets)
             (not company-candidates)
             (not (eq major-mode 'snippet-mode)))
    ;; https://github.com/manateelazycat/lazycat-emacs/commit/da13a688ef89f8ab2c577a3e9d2a7bcf0ef9b71d
    ;; https://emacs-china.org/t/topic/7687/30?u=jjpandari
    ;; this prevents blink of eldoc
    (with-temp-message
        (with-current-buffer " *Minibuf-0*" (buffer-string))
      ;; this prevents blink of ivy
      (let ((inhibit-message t))
        (save-some-buffers t #'(lambda () (and (buffer-file-name) (buffer-modified-p))))))))
1
  • Thanks, excellent solution
    – Hyperfocus
    Oct 18 '20 at 14:36
1

Emacs has some built in functionality for this already, so there is no need to write your own function. In case you do want to use your function, I would suggest adding a check for 'prog-mode. Programming modes should all be derived from prog-mode, so:

(when (derived-mode-p 'prog-mode)
  ;; Do stuff
  )

or, to include text-mode (org-mode, md, etc.):

(when (or (derived-mode-p 'prog-mode)
          (derived-mode-p 'text-mode))
  ;; Do stuff)

The built in way (I would recommend this) is using auto-save-mode. Let's say you're working on test.py; auto-save-mode will automatically save a copy of the buffer as #test.py# which will then be removed when you manually save the file. To auto save over test.py directly, use auto-save-visited-mode.

Something like this will work:

(add-hook 'prog-mode-hook
  (auto-save-visited-mode))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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