Carson Myers
Carson Myers

Reputation: 38564

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

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

Upvotes: 22

Views: 31049

Answers (7)

Amol Gawai
Amol Gawai

Reputation: 3340

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

Upvotes: 17

jch
jch

Reputation: 5651

The easy way to do that is to use electric-indent-mode. If you want to enable it manually, just do

M-x electric-indent-mode

In order to enable it automatically, say the following in your .emacs:

(electric-indent-mode)

This will be the default in Emacs 24.4.

Upvotes: 1

miqqis
miqqis

Reputation:

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.

Upvotes: 19

wr.
wr.

Reputation: 2859

If you want to indent the whole c/c++ file, you just do

  1. mark everything: CTRL-x h
  2. press TAB

That, btw, works also for xml, python code and other types.

Upvotes: 11

Jouni K. Seppänen
Jouni K. Seppänen

Reputation: 44118

In addition to the other suggestions, try C-c C-a to toggle the auto-newline feature so that your modeline reads C/la. Then you don't even need to press Enter most of the time; instead, when you type a semicolon or a brace, newlines get inserted and your code gets indented. Sometimes you do need to press Enter, and you don't get autoindentation (unless you rebind RET, as suggested in another answer) but if you just start typing the next line anyway, you may notice that it gets indented properly when you type some punctuation character.

To get started with your "Lisp degree", type C-h i for the Info browser and go to the node for "CC Mode" (it is separate from the node for Emacs; type d for Directory and find CC Mode there).

Upvotes: 6

Peter Miehle
Peter Miehle

Reputation: 6070

copying: move to start, CTRL-space, move to end, ALT-W

pasting: CTRL-y

on unix: left mouse to copy, right mouse to paste

Upvotes: -1

DarkWulf
DarkWulf

Reputation: 369

Try [enter], (kbd "enter") or (read-kbd-macro "enter"), they all do subtly different things ;)

Also, for me copy/pasting just works. Can you try running emacs -q, and see if it works? If it does, it may be a problem with your .emacs.

I also have:

(when window-system
  ;; use extended compound-text coding for X clipboard
  (set-selection-coding-system 'compound-text-with-extensions))

but that might do something else ;)

Upvotes: 1

Related Questions