Search code examples
emacsemacs23

Apply a function to all buffers in emacs


Suppone that I want to apply delete-trailing-whitespace to all buffers in an Emacs session. How can I do that?

I have in this session many buffers. So instead to apply M-x delete-trailing-whitespaceto manually each buffer, I need some way to make it automatically.

Thank you very much


Solution

  • This should do it:

    (defun delete-trailing-whitespace-each-buffer ()
      (interactive)
      (mapc (lambda (buffer)
              (condition-case nil
                  (with-current-buffer buffer
                    (delete-trailing-whitespace))
                (buffer-read-only nil)))
            (buffer-list)))
    

    It won't do anything on read-only buffers.