incandescentman
incandescentman

Reputation: 6388

how to silently "Save All Buffers" in Emacs?

How do I create a command to "Silently Save All Buffers" in Aquamacs Emacs?

I found the command save-some-buffers which is nice, but the problem is that it prompts me, separately, to confirm every buffer I want to save.

Through Googling I found some documentation on a variable called save-silently-p but that doesn't seem to exist (in Aquamacs version 2.4, i.e. Emacs 23.3.50.1).

So how do I get it to save all buffers silently without prompting?

Upvotes: 25

Views: 8868

Answers (4)

Brian Burns
Brian Burns

Reputation: 22062

If you pass non-nil to save-some-buffers it will save all files without prompting, so you can do something like

(defun save-all () (interactive) (save-some-buffers t))
(global-set-key (kbd "C-S") 'save-all)

Upvotes: 20

lawlist
lawlist

Reputation: 13467

In this example, the first argument is set to a non-nil value -- "[o]ptional argument (the prefix) non-nil means save all with no questions." Although it could be just plain old t, I chose to make up a more meaningful symbol to remind myself of what it stands for -- i.e., 'no-confirm. If I wanted to receive a confirmation dialog, then I would instead use nil for the first argument. See the doc-string -- M-x describe-function RET save-some-buffers RET -- for additional information regarding how this function behaves depending upon whether the first argument is nil or non-nil.

The example below uses save-some-buffers with a custom zero argument function for the second argument -- i.e, PRED -- to choose which buffers should be saved. The doc-string for save-some-buffers states that the second argument -- i.e., PRED -- may be one of three possibilities -- (1) If PRED is nil, all the file-visiting buffers are considered; (2) If PRED is t, then certain non-file buffers will also be considered; or (3) If PRED is a zero-argument function, it indicates for each buffer whether to consider it or not when called with that buffer current.

The example uses a keyboard shortcut of f5, but the user is free to call this custom function interactively -- M-x my-save-some-buffers -- or assign a different keyboard shortcut entirely. There are a few possible matches that have been hard-coded, and the user is free to add/remove/modify the conditions:

•  file-visiting-buffer + the file name matches the value of the variable abbrev-file-name.

• file-visiting-buffer + the major-mode is latex-mode from the built-in tex-mode.el -- not AUCTeX.

• file-visiting-buffer + the major-mode is markdown-mode.

• file-visiting-buffer + the major-mode is emacs-lisp-mode.

• file-visiting-buffer + the derived-mode is org-mode.

(defun my-save-some-buffers ()
(interactive)
  (save-some-buffers 'no-confirm (lambda ()
    (cond
      ((and buffer-file-name (equal buffer-file-name abbrev-file-name)))
      ((and buffer-file-name (eq major-mode 'latex-mode)))
      ((and buffer-file-name (eq major-mode 'markdown-mode)))
      ((and buffer-file-name (eq major-mode 'emacs-lisp-mode)))
      ((and buffer-file-name (derived-mode-p 'org-mode)))))))

(global-set-key [f5] 'my-save-some-buffers)

Upvotes: 5

ocodo
ocodo

Reputation: 30319

There's no need to set any custom variables, you simply:

C-u C-x s

Or in long-hand:

Ctrl+u Ctrl+x s

In addition you can just do, C-x s and press ! at the prompt answer yes to for all saveable file buffers. Which isn't strictly silent, but it's still very fast/unobstrusive. (credit: @phils)

Upvotes: 29

Nicolas Dudebout
Nicolas Dudebout

Reputation: 9262

From the info page of save-some-buffers:

(save-some-buffers &optional ARG PRED)

Save some modified file-visiting buffers. Asks user about each one. You can answer y' to save,n' not to save, C-r' to look at the buffer in question withview-buffer' before deciding or d' to view the differences usingdiff-buffer-with-file'.

This command first saves any buffers where `buffer-save-without-query' is non-nil, without asking.

You should therefore set the variable buffer-save-without-query to t (for example) for all your files. (add-hook 'find-file-hook (lambda () (setq buffer-save-without-query t))) should do the trick.

Upvotes: 3

Related Questions