Clean up buffer or region in Emacs

Before I commit code I want to: Replace all tabs with spaces, make sure indentation is correct and delete trailing whitespace.

Here’s a function that does just that (inspired by cleanup-buffer from Emacs starter kit).

(defun clean-up-buffer-or-region ()
  "Untabifies, indents and deletes trailing whitespace from buffer or region."
    (unless (region-active-p)
    (untabify (region-beginning) (region-end))
    (indent-region (region-beginning) (region-end))
      (narrow-to-region (region-beginning) (region-end))

The function either operates on a region (if any region is selected) or the whole buffer.

My binding:

(global-set-key (kbd "C-c n") 'clean-up-buffer-or-region)

blog comments powered by Disqus Back to Top

Fork me on GitHub