Auto indentation in ruby electric mode

Hi,
I’m learning to use Emacs to program Ruby. The ruby-electric-mode
works well except that after I hit ENTER, my cursor will not be
automatically indented. For example, if I type “def foo” and then
ENTER, my cursor will be placed at the first column instead of being
properly indented, say, two spaces to the right of “def”. Is there any
way to make the electric mode do auto indentation? Thanks!

On Feb 8, 2007, at 6:05 PM, [email protected] wrote:

Hi,
I’m learning to use Emacs to program Ruby. The ruby-electric-mode
works well except that after I hit ENTER, my cursor will not be
automatically indented. For example, if I type “def foo” and then
ENTER, my cursor will be placed at the first column instead of being
properly indented, say, two spaces to the right of “def”. Is there any
way to make the electric mode do auto indentation? Thanks!

By default, in Emacs that is accomplished with C-j (newline-and-
indent). You may map RET to C-j if that’s more natural for you.

– fxn

On Feb 8, 1:50 pm, Xavier N. [email protected] wrote:

By default, in Emacs that is accomplished with C-j (newline-and-
indent). You may map RET to C-j if that’s more natural for you.

– fxn

Neat! Thank you very much, Xavier!

On Feb 8, 9:04 am, [email protected] wrote:

Hi,
I’m learning to use Emacs to program Ruby. The ruby-electric-mode
works well except that after I hit ENTER, my cursor will not be
automatically indented. For example, if I type “def foo” and then
ENTER, my cursor will be placed at the first column instead of being
properly indented, say, two spaces to the right of “def”. Is there any
way to make the electric mode do auto indentation? Thanks!

Here’s what I picked up from a website about a month ago (and my left
pinkey is much happier now):

;; Automatically indent the next line…
(mapcar
(lambda (mode)
(let ((mode-hook (intern (concat (symbol-name mode) “-hook”)))
(mode-map (intern (concat (symbol-name mode) “-map”))))
(add-hook mode-hook
`(lambda nil
(local-set-key (kbd “RET”)
(or (lookup-key ,mode-map “\C-j”)
(lookup-key global-map “\C-
j”)))))))
'(ada-mode c-mode c+±mode cperl-mode emacs-lisp-mode java-mode html-
mode
lisp-mode php-mode ruby-mode sh-mode sgml-mode))

On Feb 8, 6:30 pm, [email protected] wrote:

Here’s what I picked up from a website about a month ago (and my left
(or (lookup-key ,mode-map “\C-j”)
(lookup-key global-map “\C-
j”)))))))
'(ada-modec-modec+±modecperl-modeemacs-lisp-modejava-modehtml-mode
lisp-modephp-moderuby-modesh-modesgml-mode))

Thanks, James! I’m happily using this code in my emacs now. I’m not
familar with Emacs Lisp, so I didn’t run into a few road blocks. For
those who are not familiar with Emacs Lisp either, here are some tips
when you copy&paste the above code to your Emacs configuration file:
– the “comma” in front of mode-map in the expression (lookup-
key ,mode-map “\C-j”) is not a typo. I naively removed it, and was
supprised to get void-variable error. It turns out a comma in a list
means the symbol following the comma should be evaluated. It is
crucial here.The lambda expression in the add-hook function is not a
closure that contains the value of the mode-map, which is set by “let”
expression. Therefore, a comma in front of “mode-map” forces it to be
evaluated immediately so that it will retain the value of mode-map.
– There are a few hard line-breaks inside a list or a string. They
should be removed.

On Feb 9, 2007, at 12:35 AM, [email protected] wrote:

Here’s what I picked up from a website about a month ago (and my left
(or (lookup-key ,mode-map “\C-j”)
(lookup-key global-map “\C-
j”)))))))
'(ada-mode c-mode c+±mode cperl-mode emacs-lisp-mode java-mode html-
mode
lisp-mode php-mode ruby-mode sh-mode sgml-mode))

Just in case, if mapping RET globally is fine just put this single
line instead:

(global-set-key (kbd “RET”) 'newline-and-indent)

Less specific but much simple.

– fxn