Undoing auto-indentation
Asked Answered
N

1

8

Wnen you use irb with auto indent mode, the end statements get indented one level extra

 def foo
   ...
   end

instead of showing the ordinary indenting convention:

def foo
  ...
end

because you cannot tell irb in advance that you are going to escape one level in the next line. This question has been addressed elsewhere like here or here, but neither gives a satisfactory answer. They just suggest giving up.

However, if we can minimally overwrite some irb methods so that auto indent will insert white spaces not in the prompt area but at the beginning of the line you type in, then by default, irb will still be inserting spaces, but we will be able to erase some spaces with backspace. Is this possible?

Or, if that is not realistic, then is it possible to make irb erase the last line from the screen and redisplay it with proper indentation right after you press Enter on a line including end?

Nitza answered 20/5, 2011 at 23:35 Comment(0)
A
5

Rewriting the last line is possible. Doing it in irb is difficult due to its lack of documentation and consistent api across versions. An irb alternative, ripl, has already solved this issue for itself with an auto-indent plugin. If you want to give ripl and its auto-indenting a try:

$ gem install ripl-auto_indent
$ echo "require 'ripl/auto_indent'" >> ~/.riplrc

# Auto-indent away
$ ripl
>> def foo
>>   puts "it's auto-magic!"
>> end
Atc answered 22/5, 2011 at 18:23 Comment(1)
I like ripl and started to use it. Now, I am trying to customize the prompt characters, but I cannot find the instructions on the web. The irb style using IRB.conf[:DEFAULT] ... does not seem to work. Can you point me to a link for instruction or briefly explain how to do it?Nitza

© 2022 - 2024 — McMap. All rights reserved.