How do I make Emacs auto-indent my C code?

Have a look at emacswiki - autoindent

As suggested there, put following code in your .emacs

(defun set-newline-and-indent ()
  (local-set-key (kbd "RET") 'newline-and-indent))
(add-hook 'c-mode-hook 'set-newline-and-indent)

You can customize the variable C Default Styl. In your emacs go to Options->Customize Emacs->Specific Option, then type c-default-style and set to your choice. By doing this, you don't need to hit TAB. Type from start of the line and as you hit ";" , it will be automatically indented.

Hope this helps


I set the return key to globally act as a new-line-and-intent in my ~/.emacs file:

(global-set-key "\C-m" 'newline-and-indent)

Works for me.

Tags:

Emacs