Have emacs automatically indent the whole code after it is all written

emacs

My emacs is set up to automatically indent my code as I write it and also re-indent things correctly if I push tab on any line that got misaligned somehow (usually because I changed the code). I am wondering, though, if there is any command in emacs that would allow me to take an entire file (with a code written in it) and indent the whole thing automatically the same way it normally indents my code real time as I write it without me having to go through and push tab on every line of the file?

I suppose I could define a macro that tabs and then advances to the next line and repeat that macro for the length of my file. I am wondering though if there is any command in emacs or some other feature that would do this automatically.

Best Answer

You can indent the region, to do this for the whole buffer:

  • mark whole buffer with C-x h (or M-x mark-whole-buffer)
  • run indent region with C-M-\ (or M-x indent-region)
Related Question