Apply function to all buffers in emacs

Suppose I want to apply delete-trailing-whitespace to all buffers in an Emacs session. How can i do this?

I have a lot of buffers in this session. So instead of manually applying Mx delete-trailing-whitespace for each buffer, I need a way to do this automatically.

Many thanks

+7
source share
2 answers

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 will not do anything in read-only buffers.

+9
source

ibuffer is another option. You can quickly select the desired buffers (perhaps by regular expression) and press E to evaluate the shape in each buffer. This works for any form.

+5
source

All Articles