Ad

Swapping "Enter" Command With "Cmd + Enter" In VSCode

- 1 answer

I have made some changes in keybindings.json:

    {
    "key": "cmd+enter",
    "command": "editor.action.insertLine", // this doesn't work, command not found
    "when": "editorTextFocus"
},
{
    "key": "enter",
    "command": "editor.action.insertLineAfter", // to insert line below, this works correctly
    "when": "editorTextFocus"
}

My idea was to prevent the following situation:

print("sentence~") # here I am pushing "Enter" while cursor is in ~ place
print("sentence
")

Now, after the change, although cursor is before ") it skips to the new line after pushing "Enter", which is as expected (90% of usage).

My problem now is, that the previous command (after pressing "Enter") exist no more. So I can't halve the line in the middle (10% of usage) and go to the new line with the rest of the previous line after the cursor.

I am looking for a command which I could pin to "Cmd + Enter". I thought maybe just "editor.action.insertLine" but it's not correct.

Thank you for your help, sercheese

Ad

Answer

Seems that you can leverage "command": "editor.action.insertSnippet", "args": { "snippet": "\n" }

Thanks: Visual Studio Code snippet as keyboard shortcut key

Tried something like you requirements with…

  {
    "key": "enter",
    "command": "editor.action.insertLineAfter",
    "when": "editorTextFocus && !suggestWidgetVisible"
  },
  {
    "key": "ctrl+enter",
    "command": "editor.action.insertSnippet",
    "args": { "snippet": "\n" },
    "when": "editorTextFocus && !suggestWidgetVisible"
  },
  {
    "key": "shift+enter",
    "command": "editor.action.insertLineBefore",
    "when": "editorTextFocus && !suggestWidgetVisible"
  },

…and it seems to work as intended on Windows. (I've added more restrictive "when" rule to be still able to confirm suggestion.)

Ad
source: stackoverflow.com
Ad