# Automatic clipboard

**URL:** <https://discourse.julialang.org/t/automatic-clipboard/123949>\
**Category:** VS Code\
**Tags:** question\
**Created:** [December 18, 2024, 6:20am UTC](https://discourse.julialang.org/t/automatic-clipboard/123949 "2024-12-18T06:20:02Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [December 18, 2024, 6:20am UTC](https://discourse.julialang.org/t/automatic-clipboard/123949/1 "2024-12-18T06:20:03Z")

</div>

I often want to paste output to Excel. So I’ve written this function `toclip` below.  
Is it possible to call this function automatically when a code block is executed via CTRL+Enter.

```julia
toclip(x::String) = InteractiveUtils.clipboard(x)
toclip(x::DataFrame) = sprint(show, "text/tab-separated-values", x ) |> InteractiveUtils.clipboard
toclip(x::Matrix) = sprint(show, "text/tab-separated-values", x ) |> InteractiveUtils.clipboard
toclip(x::Any) = (toclip ∘ ( (isstructtype ∘ typeof)(x) ? (to_df ∘ ntfromstruct) : to_df))(x)
toclip(x::KeyedArray) = (toclip ∘ DataFrame)(x)
toclip(x::Vector{<:AbstractString}) = DataFrame(;x) |> toclip
toclip(x::Vector) = ( (isstructtype ∘ typeof ∘ first)(x) ? DataFrame(x) : DataFrame(;x) ) |> toclip

```

---

<div class="post-metadata">

**Author:** ![xgdgsc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xgdgsc/32/608_2.png) [@xgdgsc](https://discourse.julialang.org/u/xgdgsc)\
**Post date:** [December 18, 2024, 7:18am UTC](https://discourse.julialang.org/t/automatic-clipboard/123949/2 "2024-12-18T07:18:34Z")

</div>

Add a custom execution command to the vscode extension?

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [December 19, 2024, 1:26am UTC](https://discourse.julialang.org/t/automatic-clipboard/123949/3 "2024-12-19T01:26:27Z")

</div>

thank you.  
The below uses `${selectedText}`. It works when the code is selected. When I hit CTRL+Enter within a function or other code block, Julia executes the whole block. is it possible to get this block as a variable like `selectedText` ?

**toclip.jl**

```julia
function execute_and_clip(expr)
    result = eval(Meta.parse(expr))
    toclip(result)
    return result
end

```

**keybindings.json**

```julia
[
    {
        "key": "ctrl+enter",
        "command": "workbench.action.terminal.sendSequence",
        "args": {
            "text": "include( \"General/toclip.jl\" ); execute_and_clip(\"${selectedText}\\n\")\n"
        },
        "when": "editorLangId == 'julia'"
    }
]

```

---

<div class="post-metadata">

**Author:** ![xgdgsc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xgdgsc/32/608_2.png) [@xgdgsc](https://discourse.julialang.org/u/xgdgsc)\
**Post date:** [December 19, 2024, 2:19am UTC](https://discourse.julialang.org/t/automatic-clipboard/123949/4 "2024-12-19T02:19:03Z")

</div>

Also bind `Julia: Select Code Block` ?

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [December 19, 2024, 2:24am UTC](https://discourse.julialang.org/t/automatic-clipboard/123949/5 "2024-12-19T02:24:49Z")

</div>

can you give a bit more of an example

---

<div class="post-metadata">

**Author:** ![xgdgsc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xgdgsc/32/608_2.png) [@xgdgsc](https://discourse.julialang.org/u/xgdgsc)\
**Post date:** [December 19, 2024, 2:55am UTC](https://discourse.julialang.org/t/automatic-clipboard/123949/6 "2024-12-19T02:55:59Z")

</div>

use [multi-command - Visual Studio Marketplace](https://marketplace.visualstudio.com/items?itemName=ryuta46.multi-command) and

```julia
[
    {
        "key": "ctrl+enter",
        "command": "extension.multiCommand.execute",
        "args": {
            "sequence": [
                {
                    "command": "language-julia.selectBlock",
                },
                {
                    "command": "workbench.action.terminal.sendSequence",
                    "args": {
                        "text": "include( \"General/toclip.jl\" ); execute_and_clip(\"${selectedText}\\n\")\n"
                    },
                    "variableSubstitution": true
                }
            ]
        },
        "when": "editorLangId == 'julia'"
    }
]

```

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [December 19, 2024, 5:13am UTC](https://discourse.julialang.org/t/automatic-clipboard/123949/7 "2024-12-19T05:13:07Z")

</div>

Would they consider adding this to the Julia VS Code extension?

its a nice feature for an Excell user.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 19, 2024, 2:35pm UTC](https://discourse.julialang.org/t/automatic-clipboard/123949/8 "2024-12-19T14:35:03Z")

</div>

Have you seen ClipData.jl? It seems like it has what you want, for the most part.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 19, 2024, 3:50pm UTC](https://discourse.julialang.org/t/automatic-clipboard/123949/9 "2024-12-19T15:50:32Z")

</div>

I think Lincoln’s point is mostly about having this run automatically in VSCode. To answer your question, given that this is something that 99% of users won’t need it strikes me as unlikely that this would be set as default in VSCode (I certainly would find it odd if stuff was automatically put on the clipboard), but you can certainly make a PR to the VSCode extension to see if the devs are up for it.

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [December 19, 2024, 11:05pm UTC](https://discourse.julialang.org/t/automatic-clipboard/123949/10 "2024-12-19T23:05:57Z")

</div>

An issue: `language-julia.selectBlock` will select the block even if I’ve already selected a section within. (so my selection will be overwritten).

Is it possible to use the existing `executeCodeBlockOrSelection` command and send the output of this to `toclip()` ?

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [December 19, 2024, 11:09pm UTC](https://discourse.julialang.org/t/automatic-clipboard/123949/11 "2024-12-19T23:09:14Z")

</div>

Thanks Peter. This is a nice package I wasn’t aware of.  
I prefer my approach though of one function, multiple methods. Seems a pity not to use Julia’s famous feature.

---

<div class="post-metadata">

**Author:** ![xgdgsc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xgdgsc/32/608_2.png) [@xgdgsc](https://discourse.julialang.org/u/xgdgsc)\
**Post date:** [December 20, 2024, 1:26am UTC](https://discourse.julialang.org/t/automatic-clipboard/123949/12 "2024-12-20T01:26:52Z")

</div>

Not I’m aware of without modifying the extension. Maybe just bind two keys for selection/without selection?
