Bind a keypress to a shell command that uses the current file in visual studio code

后端 未结 3 865
悲哀的现实
悲哀的现实 2021-01-25 07:16

Is there a way to create a keybinding to execute a shell command on a file? something like:

{
    \"key\": \"ctrl+shift+e\",
    \"command\": \"run\",
    \"comm         


        
3条回答
  •  醉话见心
    2021-01-25 07:40

    with Marks answer here I could resolve my case that was run an android command with some shortcut. After that I decided to create an guide on my GitHub, hope that helps.

    GitHub: LucasMMota

    Run Shell Script with Keyboard Shortcuts

    In this example I set a keyboard shortcut to run android reload on my react native project.

      1. Install the macros extension to execute multiple commands.
      1. After installed go to Preferences>Settings (or cmd+,)
      1. Find Macros Configuration and use to edit in User Settings, or simply add the following in your user settings:

      {

      //... other configs you could have
      
      "macros": {
          "runCommandInTerminal": [
              "editor.action.insertLineAfter", // go to new line below
              {
                  "command": "type",
                  "args": {
                      "text": "adb shell input text \"RR\" " // exec cmd to reload android device (could be any else)
                  }
              },
              // select text typed above
              {
                  "command": "cursorMove",
                  "args": {
                      "to": "wrappedLineStart",
                      "by": "wrappedLine",
                      "value": 1,
                      "select": true
                  }
              },
              "workbench.action.terminal.runSelectedText", //run command
              "editor.action.clipboardCutAction", // remove cmd typed
              //"editor.action.deleteLines", //couldn't use. When uncommented, command doesn't work
          ],
      }
      

      }

      1. Now that we have the command set, we need to set a trigger shortcut. So go to Preferences>Keyboard Shortcuts (or cmd+k), open keybindings.json and edit like this:

      {

      "key": "alt+s",

      "command":"macros.runCommandInTerminal"

      }

      1. Save your User Settings customizations and keybindings.json, respectively

    Bind file format and indenting with save shortcut

    keybindings.json:

    {
            "key": "cmd+s",
            "command": "macros.saveContentAndIndent",
            "when": "editorTextFocus && !editorReadonly"
        },
    

    User Settings:

    "macros": {
            "saveContentAndIndent": [
                "editor.action.formatDocument", // format
                "workbench.action.files.save", // as I used Save shorcut, I add the save bind again.
            ],
            //....
    }
    

提交回复
热议问题