One of the tasks I leave entirely to my editor is indentation. I don’t like at all to indent by hand, except for the current line, but once I have two lines out of place, I tell Emacs to handle it for me.

For that reason I’ve written this simple command

(defun fdx/reindent-buffer ()
  "Indent the current buffer."
  (interactive)
  (delete-trailing-whitespace)
  (indent-region (point-min) (point-max))
  (untabify (point-min) (point-max)))

This will select the whole buffer and apply the correct indentation, cleaning white-spaces in the process.

I use it literally all the time and I hope you enjoy it.

Saluti.