Emacs provides a fair few commands (
delete-trailing-whitespace, for instance) that can be used to clean up buffers, but sometimes we cannot use them and risk compromising the version history of
I just spent time on a particularly cumbersome merge made much harder because of whitespace changes and thought of ways to overcome this.
I'd like to implement a
before-save-hook that runs
delete-trailing-whitespace on every modified line in a buffer.
How can I get hold of a list of all the modified lines in a buffer?
An alternative solution using Basil's approach:
(require 'diff) (defun delete-trailing-whitespace-at-modified-lines () (when buffer-file-name (when (derived-mode-p 'prog-mode) (save-excursion (with-current-buffer (diff-no-select buffer-file-name (current-buffer) nil 'noasync) (diff-delete-trailing-whitespace) (kill-buffer)))))) (when (eq system-type 'gnu/linux) (add-hook 'before-save-hook 'delete-trailing-whitespace-at-modified-lines))