Automatic clipboard

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.

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
2 Likes

Add a custom execution command to the vscode extension?

1 Like

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

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

keybindings.json

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

Also bind Julia: Select Code Block ?

can you give a bit more of an example

use multi-command - Visual Studio Marketplace and

[
    {
        "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'"
    }
]
3 Likes

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

its a nice feature for an Excell user.

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

1 Like

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.

1 Like

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() ?

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.

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

1 Like