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.


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)
  • If programming in Fortran (at least with f90-mode), C-M-q will indent the current program scope (subroutine, module, file, etc). – tpg2114 Oct 14 '14 at 21:13
  • C-M-q works in all the C-family modes as well, and I think is a recommended keybinding for programming modes in general. – zwol Oct 14 '14 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.