Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a VS Code extension, how can I be notified when the user cuts/copies/or pastes?

For my extension I need to know when a cut/copy/paste happens and be able to get the text associated with those operations. I can probably get the text from the editor if I know when they happen.

I cannot find a listener for these operations. I suppose I can look for ctrl-x, ctrl-c, and ctrl-v keyboard inputs but some users may use the edit menu and not use the keyboard.

Is there a way to be notified when these operations happen either from the keyboard or the edit menu?

like image 888
Mark M Avatar asked Nov 15 '25 23:11

Mark M


2 Answers

Original asker here...

I came up with a solution that involves overriding the default cut/copy/paste actions in the editor. Here is the code for 'copy' in extension.js (I am using js not ts):

//override the editor.action.clipboardCopyAction with our own
var clipboardCopyDisposable = vscode.commands.registerTextEditorCommand('editor.action.clipboardCopyAction', overriddenClipboardCopyAction); 

context.subscriptions.push(clipboardCopyDisposable);

/*
 * Function that overrides the default copy behavior. We get the selection and use it, dispose of this registered
 * command (returning to the default editor.action.clipboardCopyAction), invoke the default one, and then re-register it after the default completes
 */
function overriddenClipboardCopyAction(textEditor, edit, params) {

    //debug
    console.log("---COPY TEST---");

    //use the selected text that is being copied here
    getCurrentSelectionEvents(); //not shown for brevity

    //dispose of the overridden editor.action.clipboardCopyAction- back to default copy behavior
    clipboardCopyDisposable.dispose();

    //execute the default editor.action.clipboardCopyAction to copy
    vscode.commands.executeCommand("editor.action.clipboardCopyAction").then(function(){

        console.log("After Copy");

        //add the overridden editor.action.clipboardCopyAction back
        clipboardCopyDisposable = vscode.commands.registerTextEditorCommand('editor.action.clipboardCopyAction', overriddenClipboardCopyAction);

        context.subscriptions.push(clipboardCopyDisposable);
    }); 
}

This definitely doesn't feel like the best solution... however it does seem to work. Any comments/suggestions? Are there any issues that repeatedly registering and unregistering will cause?

like image 97
Mark M Avatar answered Nov 17 '25 20:11

Mark M


There is no api to access the clipboard directly but some extensions override the default copy and paste shortcuts to customize the copy paste behavior. Here are two examples:

  • https://github.com/aefernandes/vscode-clipboard-history-extension/blob/master/src/clipboard.ts

  • https://github.com/stef-levesque/vscode-multiclip/blob/master/src/extension.ts

As you note, that approach will not work when copying using the context menu however. For supporting that as well, you could try intercepting the editor.action.clipboardCopyAction command. See how the Vim extension intercepts the type command for an example of this: https://github.com/VSCodeVim/Vim/blob/aa8d9549ac0d31b393a9346788f9a9a93187c222/extension.ts#L208

like image 39
Matt Bierner Avatar answered Nov 17 '25 20:11

Matt Bierner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!