Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

indent ruby code on the fly in emacs

Tags:

emacs

ruby

How to make Emacs automatically reindent Ruby code on the fly?

for example, with this in Emacs,

def hello
  puts "hello"
  en

After I type 'd', I want it to turn into this,

def hello
  puts "hello"
end

This is the default in Vim, but how can I achieve that in Emacs?

like image 486
fangwen Avatar asked Jul 03 '12 06:07

fangwen


2 Answers

ruby-electric is old news. Emacs 24 has a built-in minor mode called electric-indent-mode that automatically inserts newlines after some chars and you can of course remap the RETURN key to newline-and-indent (it's mapped only to indent by default). In Emacs 24 you can get matching delims with electric-pairs-mode and ruby-end mode will insert automatically end for you when needed. You can have a look at the prelude-ruby.el for more details.

like image 178
Bozhidar Batsov Avatar answered Oct 12 '22 10:10

Bozhidar Batsov


If you add ruby-electric (also part of Rinari) you get the following:

  • Correctly indented "end" when you write "class", "def", "module" etc.
  • Matching delimiters when you type the opening one.

If you don't want to add extra modes, the end will be indented correctly once you press Enter. Or you press Tab to re-indent the current line.

like image 22
Michael Kohl Avatar answered Oct 12 '22 10:10

Michael Kohl