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 😉
86 thoughts on “VS Code: Keep Your Code Clean by Running Extension Commands on Save”
From blog post to company policy in 3… 2… 1…
🥰 Thanks, this is great!
SortVariables! Sweet 👍
We use it already, just the “run on it” – extension looks great.
Thank you for the great post!
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.
Sounds good 🙂
This is great!
Do I miss something after installing “RunItOn”? There’s no execution. If I run it from command pallet I get an error:
Do you use it in the user settings of each developer or in the workspace settings of each project to enforce the policy?
I get the same error when trying to run it from the command pallete. Is AZ AL DevTools installed? Did you add this code in your settings.json?
Yes and yes. I double checked the settings. There were some glitches with indentation and comma. Now it works as described.
Is it intended that once you hit “Save” and all fixes were applied, you have to hit “Save” again to save the fixes?
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.
Great post and good for adapting this method.