2

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 2

1

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 the runCommands command which enables you to run multiple commands at once, i.e., no extension is needed.

Try this keybinding (in your keybindings.json):

  {
    "key": "alt+i",                        // whatever keybinding you want
    "command": "runCommands",
    "args": {
    "commands": [
        "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

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your solution. It worked perfectly. Can you please give an addtional information? Can i add an extra text (a static text) with the selection text when duplicating the text.
Yes, but I am not sure exactly what you mean? I'll edit the answer to show adding some text after the pasted selection.
You got me right. Great and appreciat you for your help. One more thing to go. This time it add after the selection text. How can i add static text before and after the selected text at the same time. Thanks in advance.
Thanks. I have figured it out from your comment.
0

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

3 Comments

The solution you have given, it copied the whole line, not the specific string i have selected with cursor. However @Mark given the perfect solution.
I edited the question to make it clearer that the selected text may not be the entire line.
Thanks for edition. Your edited version is more clearer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.