save and compile automatically

Put (setq compilation-ask-about-save nil) in your .emacs to automatically save (without asking) before you compile or recompile.


On every Emacs I use (from the 19.34 to 24), compile do ask for saving unsaved buffer before proceeding. I'm very surprise it is not the case for you.

So just M-x compile (f5 for you) will ask to save unsaved buffer.

you can customize compilation-ask-about-save to nil if you want compile to unconditionally save all buffer, or let it to it's default non nil value if you want to be asked.


You can use (save-some-buffers 1) to save all buffers containing changes.

Wrap it up with a function together with compile like so:

(defun save-all-and-compile ()
  (save-some-buffers 1)
  (compile compile-command))

Then set F5 to run this function instead of plain compile.

Use (save-some-buffers) (no argument) if you prefer Emacs to ask you about each buffer to be saved. Also, you might want to make the compilation-command customisable, as it is with compile... I'll leave that to you though.


Getting it to work

You also have to add (interactive), see full example below.

(defun save-all-and-compile ()
  (interactive)
  (save-some-buffers 1)
  (compile compile-command))
(global-set-key [f5] 'save-all-and-compile)

Tags:

Emacs