24

I know you can enter a block of text with code snippets but can you configure keyboard shortcuts to enter some text? With "editor.action" you can move the cursor but I can't find if it's possible if you can get it to type some text.

Something like Ctrl+Enter would be "); then a new line

Maybe create a code snippet and then invoke it with a keyboard shortcut?

Is there a way to find what all the options are for "editor.action" ?

Mwiza
  • 7,780
  • 3
  • 46
  • 42
VladimirSD
  • 401
  • 3
  • 11

4 Answers4

42

You can insert a User Snippet on keypress:

Open keybindings.json (Preferences: Open Keyboard Shortcuts (JSON)), which defines all your keybindings, and add a keybinding passing "snippet" as an extra argument

{
    "key": "ctrl+enter",
    "command": "editor.action.insertSnippet",
    "when": "editorTextFocus",
    "args": {
        "snippet": "\");\n$0"
    }
}

Furthermore, you can specify languages in which it should work:

"when": "editorTextFocus && editorLangId == 'javascript'"

See here for more information.

Brian Burns
  • 20,575
  • 8
  • 83
  • 77
Alex
  • 59,571
  • 22
  • 137
  • 126
13

You can also use the simple command type in a keybinding like:

 {
    "key": "ctrl+enter",
    "command": "type",
    "args": { 
      "text": "myText" 
    },
    "when": "editorTextFocus"
 },
Mark
  • 143,421
  • 24
  • 428
  • 436
1

The list of available keyboard actions is available from here. You can consider to write an extension for VS Code if you have something specific in mind, with that you can create actions with keybindings that modify the editor contents.

Benjamin Pasero
  • 113,622
  • 14
  • 75
  • 54
  • Actually that link lists pretty much everything *except* the available keyboard actions. It does list all of the actions that are bound be default, but AFAIK, this is no complete listing of all available actions, and certainly not the available arguments either. – RBarryYoung Apr 16 '20 at 17:06
0

I just leave it here. An alias for triple backticks for people with non-English keyboards:

    {
        "key": "ctrl+shift+1",
        "command": "editor.action.insertSnippet",
        "when": "editorTextFocus && !editorReadonly && editorLangId == 'markdown'",
        "args":{
            "snippet": "```"
        }
    }
Evgeny Lukashevich
  • 1,500
  • 14
  • 26