Ruby indentation code on the fly in emacs

How to make Emacs automatically re-specify 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 value in Vim, but how can I achieve this in Emacs?

+6
source share
4 answers

ruby-electric is old news. Emacs 24 has a built-in secondary mode called electric-indent-mode , which automatically inserts new lines after some characters, and you can, of course, reassign the RETURN key to newline-and-indent (by default, it is displayed only by indentation). In Emacs 24, you can get the corresponding dividends using electric-pairs-mode and ruby-end mode will automatically be inserted end for you when necessary. You can look at prelude-ruby.el for more details.

+3
source

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

  • Correctly indent "end" when you write "class", "def", "module", etc.
  • Matching delimiters when entering an opener.

If you do not want to add additional modes, end will be formatted correctly as soon as you press Enter . Or you press Tab to re-enter the current line.

+2
source

Try Auto-indent-mode !

  • Return automatically assigns the code accordingly (if enabled)
  • Insert / Yanking accordingly postpones
  • The kill line will remove unnecessary spaces (if enabled)
  • In the visit file, indent accordingly, but DO NOT SAVE. (Pretend nothing happened if turned on)
  • When saving, optionally unttabify, remove the trailing spaces and, of course, release the file (if included).
0
source

This will not work, because Ruby does not know if you want to type the "end" of any variables starting with "end". Therefore, typing Tab for re-indentation is necessary. And the following configuration works well for me.

 ; auto indent (define-key global-map (kbd "RET") 'newline-and-indent) (add-hook 'ruby-mode-hook (lambda () (local-set-key "\r" 'newline-and-indent))) 
0
source

Source: https://habr.com/ru/post/919554/


All Articles