2

I use the Sorting HTML and Jade attributes extension which offers the command attrsSorter.execute. I would like to setup my workspace settings.json so that "editor.codeActionsOnSave" section runs this every time before saving, then formatting / linting so that we get the sorted benefit.

Have looked all over the place and cannot figure out how to simply execute this each time save is performed. Appreciate the help!

Larry Weidig
  • 141
  • 1
  • 7

3 Answers3

4

I needed to format CSS files on save by executing the PostCSS Sorting extension and I found this tutorial: Automatic CSS sorting in VS Code

According to it you have to install the extension: ryuta46.multi-command and then modify the keybindings.json file. (F1 + Preferences: Open Keyboard Shortcuts (JSON))

Mine looks like this:

// Place your key bindings in this file to override the defaults
[
    {
        "key": "ctrl+s",
        "command": "extension.multiCommand.execute",
        "args": {
          "sequence": ["postcssSorting.execute", "workbench.action.files.save"]
        },
        "when": "editorLangId == css"
      }
]

I hope it helps someone! :)

dandelionn
  • 153
  • 2
  • 7
3

I just lost way too many hours of my life figuring out how to run the command test-explorer.run-all defined by this plugin. If you substitute editor.codeActionsOnSave for test-explorer.run-all everywhere below I think that should work.

What I've hacked together is to first of all install the Trigger Task on Save extension which, instead of bash commands as per the other answer, runs VS code tasks on save.

As per the VS code docs you can then define a custom task to wrap the command. For me, this looks like

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "label": "mocha-explorer",
      "command": "${command:test-explorer.run-all}",
    }
  ]
}

Here mocha-explorer is my own arbitrary label for this. Finally, configure the settings.json file to run your task on save by its label, which for me looks like:

    "triggerTaskOnSave.tasks": {
        "mocha-explorer": ["*.js"]
    }

I'm a total newb to all this VS code config but I hope that helps someone.

A related useful answer that may be helpful for more complex tasks: Is there a way to run a command with VS Code tasks?

rwold
  • 2,216
  • 1
  • 14
  • 22
  • In my case I used this to execute auto formatting all markdown tables on save, which works - however I do run into the issue of the file constantly being in a state of 'unsaved' because the task is executed after the save command is run - not sure how to tackle this yet. – decoder247 May 13 '22 at 06:55
-2

You can use this extension

Run On Save for Visual Studio Code

This extension allows configuring commands that get run whenever a file is saved in vscode.

Sam
  • 723
  • 5
  • 18
  • 1
    I am aware of this extension and it works for issuing command line commands on save. However, it does NOT allow for you to execute the "commands" provided by other extensions and if you look at the issues on the github repo for the extension you reference it has been asked for a couple of times and not implemented. – Larry Weidig Sep 16 '20 at 20:30