Duplicate selected text to a new line VS code?

Viewed 135

I already found that the "command": "editor.action.duplicateSelection" will duplicate the selection right next to it.

I want to duplicate the selected text to a new line. The selection may not be the entire line.

2 Answers

If you are talking about a selection that is less than the entire line, there is no built-in way to duplicate selected text to the next line. It can be done with a macro extension which enables you to run multiple commands at once.

Using the macro extension multi-command try this keybinding (in your keybindings.json):

{
  "key": "alt+i",                        // whatever keybinding you want
  "command": "extension.multiCommand.execute",
  "args": {
    "sequence": [
      "editor.action.clipboardCopyAction",
      "editor.action.insertLineAfter",
      "editor.action.clipboardPasteAction",

      {                        // to add text after the selection
        "command": "type",     // you could also put this before the paste command
        "args": { "text": " myText here after paste " }
      }

    ]
  }
}

That will copy the selected text, insert a blank line after it and paste that text there. Demo:

copy selection down

Demo with adding static text to the duplicated text:

copy selection down with static text

Click File > Preferences > Keyboard Shortcuts: Look for the Copy Line Down keyboard shortcut. enter image description here

Related