Disabling auto-indentation of code in Julia REPL

By: Bogumił Kamiński

Re-posted from: https://juliasnippets.blogspot.com/2018/11/disabling-auto-indentation-of-code-in.html

With the recent release of Julia 1.0.2 there is still a small annoyance in the Julia REPL on Windows. If you copy-paste a code like this:

function f()
    for i in 1:10
        if i > 5
            println(i)
        end
    end
end

from your editor to your Julia REPL, you get the following result:

julia> function f()
           for i in 1:10
                   if i > 5
                               println(i)
                                       end
                                           end
                                           end
f (generic function with 1 method)

Notice, that Julia automatically indents the code which is pasted, but the code is already indented so the result does not look nice. This gets really bad when you paste 50 lines of highly nested code.

There is an open PR to fix this issue here, but since it did not get into Julia 1.0.2 I thought that I would post the hack I use to disable auto-indentation. Run the following lines in your Julia REPL:

import REPL
REPL.GlobalOptions.auto_indent = false
REPL.LineEdit.options(s::REPL.LineEdit.PromptState) = REPL.GlobalOptions

and now if you copy-paste to Julia REPL the code we have discussed above you get:

julia> function f()
           for i in 1:10
               if i > 5
                   println(i)
               end
           end
       end
f (generic function with 1 method)

and all is formatted as expected.

The solution overwrites REPL.LineEdit.options method to make sure that we always use REPL.GlobalOptions with auto-indentation disabled. It is not ideal, but I find it good enough till the issue is resolved.

If you would want to use this solution by default you can put the proposed code in your ~/.julia/config/startup.jl file.