Running code in repl whenever file is saved

Hello everyone.

I’m aware or Revise.jl 's capabilities when it comes to editing code and having the changes immediately reflected in the repl.

I was wondering whether there would be a straightforward way to actually run some code whenever a file is saved.
E.g., I’m editing a function foo(x), and every time I save the file, I want foo(3) to be sent into the active repl I’m working on.

There’s things like watchexec and neovim autocommands, and these can be used to run something like julia filename.jl in the shell every time filename.jl is saved, but I’m not sure how they could be used to run things directly into the repl.

Any advice would be appreciated!

Hi! If you’re using NeoVim, I guess you could setup an AutoCmd to send the whole buffer on save to the repl using vim-slime or nvim-smuggler.

1 Like

Thank you for the tip @klafyvel.

I’m currently using yeet in neovim to yeet the line include("filename.jl") into the repl (yeet is very similar to slime).

Recently I was considering trying out the helix editor, so I wanted to see if we could have a solution which does depend on the editor.

Would it be reasonable for such a feature to be implemented directly into Revise.jl perhaps?

Does Revise.entr help?

Assuming you have a source file /tmp/foo.jl:

foo(x) = 1

then you could do something like this in the REPL:

julia> using Revise

julia> includet("/tmp/foo.jl")

julia> entr(["/tmp/foo.jl"]) do
         @show foo(3)
       end
foo(3) = 1
# (at this point, edit foo.jl and save it)
foo(3) = 2
3 Likes

Exactly what I was looking for, thanks a lot!

After playing around a bit, I’m a bit curious about this.

Why does the do block above work fine but the following:

entr( () -> foo(3) , ["tmp/foo.jl"] )

does not work?

Nothing wrong with do blocks, I’m just wondering whether I’m missing something.

This computes foo(3) but does not display the result. That was the purpose of @show in my answer.

This one works fine though:

entr(["/tmp/foo.jl"]) do
   foo(3)
end

(as in, doesn’t throw any errors, unlike the one liner entr( () -> foo(3) , ["tmp/foo.jl"] ) )

One uses "tmp/foo.jl" and the other "/tmp/foo.jl".

2 Likes

Oh sorry, that was just a typo!

I was using ["test.jl"] instead of ["/tmp/foo.jl"], so the typo wouldn’t matter.
However, after playing around with it a bit more on a fresh repl session to confirm, I don’t get the same errors I was getting previously.
Perhaps I did something weird while messing around with the foo function in the previous session which was causing the errors I was getting previously.
Sorry for the confusion!