17

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

David Dehghan
  • 22,159
  • 10
  • 107
  • 95

3 Answers3

12

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.

Silver Zachara
  • 2,901
  • 2
  • 16
  • 22
Dinko Pehar
  • 5,454
  • 4
  • 23
  • 57
  • You're welcome :) . Just to add, you can add any custom combination of keys, doesn't have to be ctrl + alt + c. Anything that doesn't overwrite already defined combinations. – Dinko Pehar Dec 17 '18 at 11:45
  • 1
    Yeah. I mapped it to shift+ctrl+c since it is more ergonomic :-) – David Dehghan Dec 17 '18 at 11:47
  • 5
    You can make it behave exactly like other editors by binding to `ctrl+c` only when there is no text selected,:`{ "key": "ctrl+c", "command": "macros.copyWithoutNewLine", "when": "editorTextFocus && !editorHasSelection" },` – nicstella Mar 28 '19 at 02:11
  • 1
    My 5+ years of life was wasted not knowing this.... – Lazy Ren Jul 06 '23 at 06:31
4

Having struggled with this for long myself too, I finally stumbled across the solution. Add these lines to keybindings.json:

{
    "key": "cmd+alt+ctrl+v", // insert your desired shortcut here
    "command": "editor.action.insertSnippet",
        "args": {
            "snippet": "$CLIPBOARD"},
    "when": "inputFocus"
},

Now, pressing cmd+option+ctrl+v (or whatever shortcut you define) should paste without newline, regardless of how it was copied.

For an explanation and more cool things you can do with snippets, see https://code.visualstudio.com/docs/editor/userdefinedsnippets#:~:text=In%20Visual%20Studio%20Code%2C%20snippets,%3A%20Enable%20it%20with%20%22editor.

Sippo_mu
  • 41
  • 3
  • 1
    Thank you so much. When I make paste operation it puts extra space, can you help to delete it? https://i.imgur.com/1mXyoff.png – Developer Mister Jan 16 '23 at 12:25
  • 1
    I Suggest this but do not know if it is possible and how to write correctly: "args": { "snippet": "$CLIPBOARD" - "\t" }, – Developer Mister Jan 16 '23 at 12:35
  • 2
    Hmm funny, for me it does not add an extra space. I would do this: `{ "key": "cmd+alt+ctrl+v", "command": "extension.multiCommand.execute", "args": { "sequence": [ { "command": "editor.action.insertSnippet", "args": { "snippet": "$CLIPBOARD" } }, "deleteLeft", ] }, "when": "inputFocus" },` Note that it requires https://marketplace.visualstudio.com/items?itemName=ryuta46.multi-command. – Sippo_mu Jan 17 '23 at 11:38
  • I've partially fix it. my mistake was that I've change hot key to: "cmd+shift+v". But your solution works only if you are pasting copied line next to the current line, but if you try to paste at another place then it does not works. So my modification of your solution (changing hot key) is better choice, but how to trim copied line, this is the question! P.S. Extension did not help me – Developer Mister Jan 17 '23 at 21:17
  • by the way, I'm searching answer on the issue long time, you may find GIF here to better understand my question: https://stackoverflow.com/questions/71401248/vscode-copy-paste-problem-it-removes-indentation-and-ads-extra-blank-line-belo – Developer Mister Jan 17 '23 at 21:22
  • Doesn't seem to work at all -- it pastes with newlines. I'm positive the actual keybinding is working because I'm using a shortcut that wasn't doing anything at all before, and so the keybinding works -- it's just that it includes newlines – Jon Mar 30 '23 at 18:06
1

Even easier: Go to VS Code > Settings. Search for "Editor: Copy with Syntax Highlighter" and uncheck it.

Now whenever you copy and paste it will copy the plain text. It won't add extra lines and it will preserve indenting.

Note: the other answers are more flexible because you can have a key command for the old behavior and the new behavior. But if you're like me and can't see a good reason to use the old behavior then it's faster and easier to just uncheck a box.

Daniel Lyons
  • 85
  • 1
  • 9