1

Is there a way in visual studio code to edit each line of text from the clipboard before pasting?

I want to create a snippet that will run at the appropriate key combination, edit the text that is copied, and then paste it.

For example:

  1. INPUT - I have a few lines of text. I copy them.

    BANANA
    APPLE
    ORANGE
    
  2. I paste the text using a combination of keys, such as ctrl + w, and I am expecting the output:

    1 - BANANA - SOME TEXT,
    2 - APPLE - SOME TEXT,
    3 - ORANGE - SOME TEXT,
    

I know that I can set the cursor at the beginning of each line and paste the text, but I'm looking for a faster solution that can additionally be extended with more conditions. E.g.:

  • if the word "large" occurs in the text, replace it with "small",
  • number the lines of text,etc.
rioV8
  • 24,506
  • 3
  • 32
  • 49
  • use [HyperSnips](https://marketplace.visualstudio.com/items?itemName=draivin.hsnips), add a feature request to get the clipboard content as variable – rioV8 Nov 30 '22 at 11:15
  • This is easy to do with the extension https://marketplace.visualstudio.com/items?itemName=ArturoDent.find-and-transform (`Find and Transform`). I'll show an example later today. – Mark Nov 30 '22 at 17:53

2 Answers2

0

Using the extension Find and Transform, which I wrote, make this keybinding in your keybindings.json (or it could be a setting):

{
  "key": "alt+f",                  // whatever keybinding you want
  "command": "findInCurrentFile",
  "description": "modify the clipboard",
  "args": {

    "run": [
      "$${",
        "const selectedText = document.getText(vscode.window.activeTextEditor.selection);",
        "const lines = selectedText.split(/\\r?\\n/);",  // to handle different OS's
        "let newStr = '';",

        "lines.forEach((line, index) => {",
          "if (line === 'APPLE') line = 'small';",
          "newStr += `${index+1} - ${line} - some text,\\n`;",  // double-escape \\n
        "});",
        "newStr = newStr.slice(0, -1);",    // remove last \n from newStr

        "vscode.env.clipboard.writeText(newStr);",
        // "return newStr",
      "}$$"
    ]
  }
}

In the body of the run value you can do any string manipulation you want. It ends by writing the new string to the clipboard as you requested.

If you just wanted to immediately replace what you have selected with the result change the run line to

    // "run": [
    "replace": [

and instead of writing to the clipboard just return the result:

        // "vscode.env.clipboard.writeText(newStr);",
        "return newStr",
      "}$$"
Mark
  • 143,421
  • 24
  • 428
  • 436
0

Variable transforms

These allow you to modify snippet variables with regular expressions before pasting them in.

This doesn't allow for you exact use case unfortunately, as I don't see how to do the 1. 2. 3. part. But just for the googlers, the following global snippet:

"snippet test": {
    "prefix": "snpt",
    "body": [
        "${CLIPBOARD/^(.*)$/1 - $1 - SOME TEXT/gm}",
    ],
    "description": "Log output to console"
},

transforms:

BANANA
APPLE
ORANGE

into:

1 - BANANA - SOME TEXT
1 - APPLE - SOME TEXT
1 - ORANGE - SOME TEXT

Docs: https://code.visualstudio.com/docs/editor/userdefinedsnippets#_variable-transforms

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985