tags:

views:

2644

answers:

6

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

+1  A: 

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 ;)

DarkWulf
+8  A: 

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 '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

Amol Gawai
the c-default-style option gave me a list of languages behind checkboxes, and after the name of the language, was the name typed again... I don't understand how this makes CR call newline-and-indent
Carson Myers
That variable sets the indentation style; CC Mode is used for several C-like languages, and you can set the style of indentation for each language. Read the friendly manual to find out what the styles are.
Jouni K. Seppänen
A: 

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

pasting: CTRL-y

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

Peter Miehle
+2  A: 

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).

Jouni K. Seppänen
+1  A: 

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

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

Works for me.

I bumped this up, because the accepted answers don't work for me, while this does. I don't know if it's because I'm using Emacs 23 or what, but using (kbd "RET") always gets me "Invalid Function".
Matthew Talbert
A: 

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.

wr