Deleting trailing whitespace for auto savers

real-auto-save is a great package if you like that sort of thing. For example, I like every file to always be saved without me worrying about doing it myself, so I stick with the default save occurring every 10 seconds. A really nice function to call on write-file-hooks is delete-trailing-whitespace, but, with 10s saves this means that in the middle of typing you have spaces eaten and this is clearly unacceptable!
Here is an attempt at a tweaked cleanup function that cleans up every line in the file but for the current line on which your cursor sits:

(defun gcr/delete-trailing-whitespace ()
  "Apply delete-trailing-whitespace to everything but the current line."
  (interactive)
  (let ((first-part-start (point-min))
        (first-part-end (point-at-bol))
        (second-part-start (point-at-eol))
        (second-part-end (point-max)))
    (delete-trailing-whitespace first-part-start first-part-end)
    (delete-trailing-whitespace second-part-start second-part-end)))

Leave a Reply

Your email address will not be published. Required fields are marked *