Skip to content

Instantly share code, notes, and snippets.

@bergmark
Created August 16, 2013 19:02
Show Gist options
  • Select an option

  • Save bergmark/6252591 to your computer and use it in GitHub Desktop.

Select an option

Save bergmark/6252591 to your computer and use it in GitHub Desktop.
If there are two linebreaks at the end of a file, keep them, if there's only one it is kept as is to not add extra changes to commits.
(defun delete-trailing-whitespace-and-extra-nl ()
(interactive)
(let ((two? (ends-with-two-linebreaks?)))
(progn
(delete-trailing-whitespace)
(if (and two? (string-match "silk" (buffer-file-name)))
(save-excursion
(let ((undo-list (undo-copy-list buffer-undo-list)))
(progn
(buffer-disable-undo)
(goto-char (point-max))
(insert ?\n)
(buffer-enable-undo)
(setq buffer-undo-list undo-list)
(set-buffer-modified-p nil)
)))))))
(defun ends-with-two-linebreaks? ()
(equal '(?\n ?\n)
(list (char-before (point-max))
(char-before (1- (point-max)))
)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment