Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

People also ask

What is Allman style?

Allman styleThis style puts the brace associated with a control statement on the next line, indented to the same level as the control statement. Statements within the braces are indented to the next level.


I'm just starting to get a feel for emacs, but I am frustrated with it's tendency to not indent when I press the return key. I know if I press C-j it will do it, but I can't get into that habit. I just can't. I need to hit return, and I hate re-tabbing every time.

I went into the options and found C mode hook, and C++ mode hook, etc--and they defined two keymappings (10 and 13, and I remembered that 0A and 0D are CR/LF because I used them a lot in assembly)--I figured since one said "(lambda nil (define-key lisp-mode-map [13] (quote newline-and-indent))" and the other the same but with (quote newline) instead, I just put the -and-indent at the end of it and figured it would work.

But it doesn't: I set and saved it, tried, to no avail. Restarted emacs, still no success. How do I make it indent my code? It's terribly insane that emacs requires a degree in lisp just to configure it for your basic needs.

Also, as a sort of side question: how do I copy to and paste from the clipboard? Killing/yanking is handy and all but I hate going edit->copy every time I want to paste to somewhere else.

-- EDIT --

okay, I put the following lines into my .emacs and it worked:

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

thanks for the help