15

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.

DisplayName
  • 11,688
NeutronStar
  • 1,671
  • As a side note, there's a fairly new Stack Exchange site for emacs: http://emacs.stackexchange.com/ – Mitch Oct 14 '14 at 18:47

1 Answers1

24

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)
jimmij
  • 47,140
  • 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
  • How would one change the behavior of C-M-\\, say to make it indent 4 spaces instead of 2? – hepcat72 Sep 24 '20 at 18:33