VS Code: Keep Your Code Clean by Running Extension Commands on Save

VS Code: Keep Your Code Clean by Running Extension Commands on Save

There are many VS Code Extensions that make our lives as developers easier. If I think of the AL programming language, for example, they help us with the following things:

  • Replace missing brackets
  • Correct the casing of words
  • Sort fields in tables
  • …and the list could go on forever.

That’s why I thought a while back that it would actually be cool to automatically execute a few of these commands when I save a file in Visual Studio Code. The VS Code Extension AZ AL Dev Tools for example has a setting “alOutline.codeActionsOnSave” which allows us to define a few commands.

That was a good start, but I wanted to be able to use similar functionality for other extensions. So I went searching and found a cool VS Code extension that helped me do that: Run It On

After installing the extension, open your “User Settings” via the Command Palette:

And enter this code here:

Match defines for which files in Visual Studio Code the command should be executed. The command at “cmd” is interesting for you here. There you enter the command of the extension that should be executed. You can find the command by looking in the “Feature Contributions” of the extension:

In the example I have chosen the RunEditorCodeCleanup command of the AZ Al Dev Tools Extension, which in turn executes these commands:

Maybe it is also useful for one of you. Have fun with it 😉

    Mentions

  • 💬 Daniel Rimmelzwaan

88 thoughts on “VS Code: Keep Your Code Clean by Running Extension Commands on Save

  1. We created a vscode account and added necessary (company) settings and extensions to it. So, if a new developer joins he just needs to login sync the settings, logout and voila … everything prepared for the start.

  2. Good point. I would put it in the project. If you have just one person with different settings your git history will be a mess.
    Also you can add the setting and perform the task i one PR.



  3. Pingback: admin
  4. Hi! Is it possible to modify RunEditorCodeCleanup command of the AZ Al Dev Tools Extension? For my type of work I do not need some of these commands being execute.

    1. Yes. You can do that in your user settings in VS Code:
      – Open VS Code
      – Hit F1 (Command Palette)
      – Type in “Preferences: Open User Settings (JSON)
      – Change the values for the setting “alOutline.codeCleanupActions”

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: