# Capture a Julia output in the clipboard

**URL:** https://discourse.julialang.org/t/capture-a-julia-output-in-the-clipboard/91186
**Category:** General Usage
**Created:** [December 3, 2022, 6:37pm UTC](https://discourse.julialang.org/t/capture-a-julia-output-in-the-clipboard/91186 "2022-12-03T18:37:11Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![\_stla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_stla/32/35613_2.png) [@\_stla](https://discourse.julialang.org/u/_stla)
#### Post date: [December 3, 2022, 6:37pm UTC](https://discourse.julialang.org/t/capture-a-julia-output-in-the-clipboard/91186/1 "2022-12-03T18:37:11Z")

</div>

Hello,

Currently I don’t have a mouse and the pad of my laptop is really really bad: I can’t select some text.

But I need to copy-paste some Julia outputs. How can I capture the output of a function and write it in the clipboard?

In R I do `clipr::write_clip(capture.output(myfunction()))`. Is there a `capture.output` function in Julia, and a package to write in the clipboard?

Otherwise I suppose this should be possible in a terminal (I’m using Linux), but how?

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [December 3, 2022, 6:40pm UTC](https://discourse.julialang.org/t/capture-a-julia-output-in-the-clipboard/91186/2 "2022-12-03T18:40:47Z")

</div>

Have you tried Julia’s `clipboard` command?

```julia
  clipboard(x)

  Send a printed form of x to the operating system clipboard ("copy").

  ────────────────────────────────────────────────────────────────────────────────

  clipboard() -> AbstractString

  Return a string with the contents of the operating system clipboard ("paste").

```

You may need to install external clip commands in your OS for it to work.

---

<div class="post-metadata">

### Author: ![\_stla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_stla/32/35613_2.png) [@\_stla](https://discourse.julialang.org/u/_stla)
#### Post date: [December 3, 2022, 6:43pm UTC](https://discourse.julialang.org/t/capture-a-julia-output-in-the-clipboard/91186/3 "2022-12-03T18:43:00Z")

</div>

Thanks. I get “clipboard not defined”.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [December 3, 2022, 6:44pm UTC](https://discourse.julialang.org/t/capture-a-julia-output-in-the-clipboard/91186/4 "2022-12-03T18:44:17Z")

</div>

Please paste the output of the command here.

What version of Julia you are using?

---

<div class="post-metadata">

### Author: ![\_stla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_stla/32/35613_2.png) [@\_stla](https://discourse.julialang.org/u/_stla)
#### Post date: [December 3, 2022, 6:45pm UTC](https://discourse.julialang.org/t/capture-a-julia-output-in-the-clipboard/91186/5 "2022-12-03T18:45:09Z")

</div>

Ah no it works!!

It didn’t work when I ran my file with `julia myfile.jl` in a terminal.

Thanks!!

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [December 3, 2022, 6:45pm UTC](https://discourse.julialang.org/t/capture-a-julia-output-in-the-clipboard/91186/6 "2022-12-03T18:45:46Z")

</div>

No problem! Please mark the thread as solved by choosing an answer if possible. That way people can come here and quickly jump to the answer they need.

---

<div class="post-metadata">

### Author: ![\_stla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_stla/32/35613_2.png) [@\_stla](https://discourse.julialang.org/u/_stla)
#### Post date: [December 3, 2022, 7:11pm UTC](https://discourse.julialang.org/t/capture-a-julia-output-in-the-clipboard/91186/7 "2022-12-03T19:11:38Z")

</div>

Hmm… This is not the output I want actually.

I have a `Vector{Vector{Float64}}` named `vertices`. When I type `vertices` in Julia, this prints:

```julia
8-element Vector{Vector{Float64}}
 [4.0, -1.0, 3.0]
 ......

```

But when I do `clipboard(vertices)`, what I get in the clipboard is the same as `print(vertices)`, namely:

```julia
[[4.0, -1.0, 3.0], ......

```

Is there a way to get the first output in the clipboard?

---

<div class="post-metadata">

### Author: ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)
#### Post date: [December 3, 2022, 8:18pm UTC](https://discourse.julialang.org/t/capture-a-julia-output-in-the-clipboard/91186/8 "2022-12-03T20:18:51Z")

</div>

There may be an easier way but this seems to work:

```julia
julia> function myclipboard(x)
           iob = IOBuffer()
           show(iob, MIME"text/plain"(), x)
           str = String(take!(iob))
           clipboard(str)
       end
myclipboard (generic function with 1 method)

julia> x = [rand(3) for _ in 1:10]
10-element Vector{Vector{Float64}}:
 [0.0006688071063627454, 0.32438290031630856, 0.8963609977463138]
 [0.07447459439615989, 0.10858832326591139, 0.21761763049060934]
 [0.9474289304279342, 0.17597397164449635, 0.4534076639611243]
 [0.4745593192888291, 0.6805225170543437, 0.8428815292562719]
 [0.8008939242075624, 0.8933863723806169, 0.25332293783855564]
 [0.9801185523169914, 0.9014589729035086, 0.2976324614235306]
 [0.075404589462707, 0.17769135609527553, 0.54928725746172]
 [0.27625669533731967, 0.7821048947678795, 0.1840168069047895]
 [0.7481177179768426, 0.13077867820640932, 0.7906552800286413]
 [0.4480463507156114, 0.5866234993545199, 0.7985719519088222]

julia> myclipboard(x)

julia> clipboard()
"10-element Vector{Vector{Float64}}:\n [0.0006688071063627454, 0.32438290031630856, 0.8963609977463138]\n [0.07447459439615989, 0.10858832326591139, 0.21761763049060934]\n [0.9474289304279342, 0.17597397164449635, 0.4534076639611243]\n [0.4745593192888291, 0.6805225170543437, 0.8428815292562719]\n [0.8008939242075624, 0.8933863723806169, 0.25332293783855564]\n [0.9801185523169914, 0.9014589729035086, 0.2976324614235306]\n [0.075404589462707, 0.17769135609527553, 0.54928725746172]\n [0.27625669533731967, 0.7821048947678795, 0.1840168069047895]\n [0.7481177179768426, 0.13077867820640932, 0.7906552800286413]\n [0.4480463507156114, 0.5866234993545199, 0.7985719519088222]"

julia> println(clipboard())
10-element Vector{Vector{Float64}}:
 [0.0006688071063627454, 0.32438290031630856, 0.8963609977463138]
 [0.07447459439615989, 0.10858832326591139, 0.21761763049060934]
 [0.9474289304279342, 0.17597397164449635, 0.4534076639611243]
 [0.4745593192888291, 0.6805225170543437, 0.8428815292562719]
 [0.8008939242075624, 0.8933863723806169, 0.25332293783855564]
 [0.9801185523169914, 0.9014589729035086, 0.2976324614235306]
 [0.075404589462707, 0.17769135609527553, 0.54928725746172]
 [0.27625669533731967, 0.7821048947678795, 0.1840168069047895]
 [0.7481177179768426, 0.13077867820640932, 0.7906552800286413]
 [0.4480463507156114, 0.5866234993545199, 0.7985719519088222]

```

---

<div class="post-metadata">

### Author: ![\_stla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_stla/32/35613_2.png) [@\_stla](https://discourse.julialang.org/u/_stla)
#### Post date: [December 3, 2022, 9:22pm UTC](https://discourse.julialang.org/t/capture-a-julia-output-in-the-clipboard/91186/9 "2022-12-03T21:22:28Z")

</div>

Cool! Thanks.
