How to configure uncrustify in visual code?

6.3k Views Asked by At

I've installed uncrustify in my linux machine and installed it's the extension in Visual Code as well. I've configured the settings.json as follows:

"uncrustify.executablePath": "/usr/bin/uncrustify",
"uncrustify.configPath": "/media/rinaldi/Excess/Dropbox/Projects/uncrusify-cpp.cfg"

and added the line in the keybiddings.json:

{ "key": "ctrl+f6",               "command": "uncrustify"}

When I hit "ctrl+f6" it says that the uncrustify command wasn't found even though through a terminal accessing /usr/bin/uncrustify I get the uncrustify binary.

2

There are 2 best solutions below

0
On

There are a couple different concepts here that I'll explain.

Formatters:

VS Code has the concept of "formatter" extensions that will format your code. VS Code provides several ways to trigger this formatting, you can use some settings such as"editor.formatOnSave": true" or "editor.formatOnType": true, or use the editor.action.formatDocument command directly, it will ask any formatting extensions that are installed for that file to format it.

Commands:

In the keybindings above, the "command" part doesn't refer to a shell command/.exe command, it refers to a vscode command. These can be built in commands like the editor.action.formatDocument command, or it can be commands contributed by extensions. If you ever want to see what commands an extension contributes, you can look at the package.json. Uncrustify contributes commands here. As you can see, they are all related to configuration and none of the commands are called "uncrustify". That's why VS Code is saying that it can't find the uncrustify command, it simply doesn't exist.

So how do you get this to work?

Change the command for ctrl+f6 to editor.action.formatDocument. If you do that, as explained above, VS Code will ask your Uncrustify extension to format the document for you.

0
On

File→Preferences→Keyboard shortcuts→Format Document (editor.action.formatDocument). Overwrite it with your Ctrl+F6.