# Julia REPL paste "magic" macro

**URL:** https://discourse.julialang.org/t/julia-repl-paste-magic-macro/3610
**Category:** General Usage
**Created:** [May 9, 2017, 4:34am UTC](https://discourse.julialang.org/t/julia-repl-paste-magic-macro/3610 "2017-05-09T04:34:41Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![joshbode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshbode/32/260_2.png) [@joshbode](https://discourse.julialang.org/u/joshbode)
#### Post date: [May 9, 2017, 4:34am UTC](https://discourse.julialang.org/t/julia-repl-paste-magic-macro/3610/1 "2017-05-09T04:34:41Z")

</div>

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:

```julia
# ~/.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
julia> @paste

```

and whatever text I have copied will be evaluated in the Julia REPL.

Hopefully this is useful to someone!

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [May 9, 2017, 1:58pm UTC](https://discourse.julialang.org/t/julia-repl-paste-magic-macro/3610/2 "2017-05-09T13:58:45Z")

</div>

Is this different from just typing Ctrl-V (or command-V on Mac)?

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [May 9, 2017, 3:38pm UTC](https://discourse.julialang.org/t/julia-repl-paste-magic-macro/3610/3 "2017-05-09T15:38:48Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [May 9, 2017, 7:56pm UTC](https://discourse.julialang.org/t/julia-repl-paste-magic-macro/3610/4 "2017-05-09T19:56:22Z")

</div>

And specially the history.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 9, 2017, 8:46pm UTC](https://discourse.julialang.org/t/julia-repl-paste-magic-macro/3610/5 "2017-05-09T20:46:25Z")

</div>

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.)

---

<div class="post-metadata">

### Author: ![joshbode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshbode/32/260_2.png) [@joshbode](https://discourse.julialang.org/u/joshbode)
#### Post date: [May 9, 2017, 9:22pm UTC](https://discourse.julialang.org/t/julia-repl-paste-magic-macro/3610/6 "2017-05-09T21:22:41Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![joshbode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshbode/32/260_2.png) [@joshbode](https://discourse.julialang.org/u/joshbode)
#### Post date: [May 9, 2017, 9:34pm UTC](https://discourse.julialang.org/t/julia-repl-paste-magic-macro/3610/7 "2017-05-09T21:34:14Z")

</div>

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:

```julia
# ~/.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
julia> paste

```
