Everyone who’s used Emacs for more than 10 minutes knows that you can indent the selected region in a buffer with C-M-\ (bound to the command indent-region). While this is pretty useful I find that it’s more useful to have a command that indents the current region if present and the entire buffer otherwise. Therefore I’ve devised the simple commands er-indent-buffer and er-indent-region-or-buffer.

(defun er-indent-buffer ()
  "Indent the currently visited buffer."
  (indent-region (point-min) (point-max)))

(defun er-indent-region-or-buffer ()
  "Indent a region if selected, otherwise the whole buffer."
    (if (region-active-p)
          (indent-region (region-beginning) (region-end))
          (message "Indented selected region."))
        (message "Indented buffer.")))))

Now that you have er-indent-region-or-buffer you don’t actually need indent-region or er-indent-buffer that much (if at all). That’s why I typically bind er-indent-region-or-buffer to C-M-\.

(global-set-key (kbd "C-M-\\") #'er-indent-region-or-buffer)