sercheese
sercheese

Reputation: 3

Swapping "Enter" command with "Cmd + Enter" in VSCode

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

Upvotes: 0

Views: 1280

Answers (2)

myf
myf

Reputation: 11293

Seems that you can leverage "insert snippet" command feature that calls snippet consisting of single line separator (\n):

"command": "editor.action.insertSnippet",
"args": { "snippet": "\n" }`

Thanks: Visual Studio Code snippet as keyboard shortcut key

Tried something like your 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.)

Upvotes: 1

Carter_383
Carter_383

Reputation: 1

cmd shift P >> Open keyboard shortcuts

Upvotes: 0

Related Questions