Capture a Julia output in the clipboard

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?

Have you tried Julia’s clipboard command?

  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.

Thanks. I get “clipboard not defined”.

Please paste the output of the command here.

What version of Julia you are using?

Ah no it works!!

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

Thanks!!

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.

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:

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:

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

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

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

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]

Cool! Thanks.