Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode copy and paste 1 line without newline character being prepended

enter image description here

In VScode when you copy a line of text and then put your cursor in the middle of quotes and hit Ctrl+V it pastes the new line above where you intended it to go.

In IntelliJ and PyCharm when you copy a line of code with Ctrl+C without selecting any text then they intelligently remove the \n character at the end of the string while it is in memory. So when you paste it in middle of quotes you get the desired behavior.

Since VS team is not likely going to fix this anytime soon I was wondering if anyone has a macro for it.

https://github.com/Microsoft/vscode/issues/61840

like image 498
David Dehghan Avatar asked Dec 15 '18 10:12

David Dehghan


1 Answers

A combination of keys would help you:

  1. Home
  2. Shift + End
  3. Ctrl + Alt + C

But since you want to do it with just Ctrl + Alt + C, you can install extension called macros to make a macro, recorded multiple key combinations.

Create your own custom macros by adding them to your settings.json:

"macros": {
    "copyWithoutNewLine": [
        "cursorHome",
        "cursorEndSelect",
        "editor.action.clipboardCopyAction",
        "cancelSelection",
        "cursorUndo",
        "cursorUndo",
        "cursorUndo"
    ]
}

Created macro can have a custom name, in this example it's copyWithoutNewLine. And this macro executes all above stated commands to copy line.

After creating macro, you need to add it to keybindings.json to run it:

{
    "key": "ctrl+alt+c",
    "command": "macros.copyWithoutNewLine",
    "when": "editorTextFocus && !editorHasSelection"
}

When key combination of Ctrl + Alt + C is pressed, it will copy it without a new line, and you can paste it where ever you want.

like image 57
Dinko Pehar Avatar answered Oct 13 '22 06:10

Dinko Pehar