Emacs auto-save on switch buffer

autosaveemacs

Call me lame, but I'm tired of my subconscious C-x C-s nervous twitch. I am switching buffers often enough and I think I would like to save a certain buffer as soon as I switch to another. I have not had the time yet to learn Emacs-Lisp basics.

Any hints on how to do this, or better solutions?

(On a related note, I found an autosave workaround that can save the current buffer as soon as you are idle for a given amount of time.)

Best Answer

To expand on Seth's answer, I'd do this:

(defadvice switch-to-buffer (before save-buffer-now activate)
  (when buffer-file-name (save-buffer)))
(defadvice other-window (before other-window-now activate)
  (when buffer-file-name (save-buffer)))
(defadvice other-frame (before other-frame-now activate)
  (when buffer-file-name (save-buffer)))

The check for buffer-file-name avoids saving buffers w/out files. You need to figure out all the entry points you use for switching buffers that you care about (I'd also advise other-window).