Skip to content

Instantly share code, notes, and snippets.

@perusio
Created July 8, 2011 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perusio/1071894 to your computer and use it in GitHub Desktop.
Save perusio/1071894 to your computer and use it in GitHub Desktop.
How to have your files always tidy: no trailing spaces, no tabs and properly indented on emacs
;;; Clean up the current buffer: indent it, remove tabs and delete the
;;; trailing whitespace.
(defun tidy-up-buffer ()
" Do a cleanup of the current buffer: tabs, spaces and indent
it."
(interactive)
(indent-buffer)
(delete-trailing-whitespace)
(untabify-buffer))
;;; Do a general buffer cleanup before save. Markdown and text modes don't dig "blind" automatic indentation.
(add-hook 'before-save-hook #'(lambda() (unless (member major-mode '(fundamental-mode debian-changelog-mode debian-control-mode markdown-mode text-mode)) (tidy-up-buffer))))
@perusio
Copy link
Author

perusio commented Jul 8, 2011

If you want to enable on a local buffer basis do:

(add-hook 'php-mode-hook #'(lambda() (set (make-local-var'before-save-hook) #'tidy-up-buffer)))

Replace php-mode-hook with whichever mode you want this to tidying up to be active.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment