# Auto indentation in ruby electric mode

**URL:** <https://www.ruby-forum.com/t/auto-indentation-in-ruby-electric-mode/84921>\
**Category:** Ruby\
**Created:** [February 8, 2007, 6:06pm UTC](https://www.ruby-forum.com/t/auto-indentation-in-ruby-electric-mode/84921 "2007-02-08T18:06:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 8, 2007, 6:06pm UTC](https://www.ruby-forum.com/t/auto-indentation-in-ruby-electric-mode/84921/1 "2007-02-08T18:06:44Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 8, 2007, 7:51pm UTC](https://www.ruby-forum.com/t/auto-indentation-in-ruby-electric-mode/84921/2 "2007-02-08T19:51:55Z")

</div>

On Feb 8, 2007, at 6:05 PM, removed\_email\_address@domain.invalid 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 8, 2007, 9:16pm UTC](https://www.ruby-forum.com/t/auto-indentation-in-ruby-electric-mode/84921/3 "2007-02-08T21:16:06Z")

</div>

On Feb 8, 1:50 pm, Xavier N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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!

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 9, 2007, 12:35am UTC](https://www.ruby-forum.com/t/auto-indentation-in-ruby-electric-mode/84921/4 "2007-02-09T00:35:30Z")

</div>

On Feb 8, 9:04 am, removed\_email\_address@domain.invalid 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))

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 11, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/auto-indentation-in-ruby-electric-mode/84921/5 "2007-02-11T18:11:33Z")

</div>

On Feb 8, 6:30 pm, removed\_email\_address@domain.invalid 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.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 9, 2007, 8:43am UTC](https://www.ruby-forum.com/t/auto-indentation-in-ruby-electric-mode/84921/6 "2007-02-09T08:43:04Z")

</div>

On Feb 9, 2007, at 12:35 AM, removed\_email\_address@domain.invalid 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
