I use Python and Julia and use the REPL a lot in both.
One thing I really like in the Python ecosystem is IPython (yes, I’m aware of IJulia ).
One particular feature I use a lot of in IPython is the %paste
magic command, which takes the contents of your copy-buffer and submits them to the REPL, without showing the pasted text or entering it in your history.
I wanted something similar in the plain Julia REPL and came up with this macro:
# ~/.juliarc.jl
module __magic__
export @paste
"""Evaluate the copy buffer"""
macro paste()
include_string(clipboard());
end
end
importall __magic__
Then, in the REPL I can simply do:
julia> @paste
and whatever text I have copied will be evaluated in the Julia REPL.
Hopefully this is useful to someone!
10 Likes
Is this different from just typing Ctrl-V (or command-V on Mac)?
I think the idea is that this doesn’t display the pasted text, which would be useful if one were pasting a large block of text and doesn’t want it cluttering up the screen.
1 Like
And specially the history.
Why should this be a macro and not a function? (A lot of things IPython accomplishes with “magic” macros, i.e. extensions to Python syntax, can be accomplished with regular Julia functions.)
Exactly
A lot of the time I build up something larger in a text editor and since I’m iterating towards something that actually works, I don’t need any of the intermediate steps on screen or in history.
True - it could definitely be a function - I was looking for something that looked similar to what I was familiar with in IPython (%paste -> @paste
) - I also didn’t want to have to type the empty brackets (e.g. paste()
) so a macro worked well for that.
An earlier version I’d played around with was this:
# ~/.juliarc.jl
module __magic__
export paste
"""Evaluate the copy buffer"""
function _paste()
include_string(clipboard());
end
"""Wrapper to call function when displayed at the REPL"""
type DisplayWrapper{F <: Function}
f::F
end
(x::DisplayWrapper)(args...; kwargs...) = x.f(args...; kwargs...)
Base.display(x::DisplayWrapper) = x.f()
paste = DisplayWrapper(_paste)
end
importall __magic__
which abused the display
method at the REPL to allow you to paste by simply executing:
julia> paste
2 Likes