Luke Miles
Luke Miles

Reputation: 1160

How to use VS Code's formatter from the command line?

Someone in my team uses vim, but the rest of us use VS Code's default formatter (i.e. vscode.typescript-language-features). I'd like to add a git precommit hook or something that invokes the formatter without opening up the app.

See also:

Upvotes: 5

Views: 2670

Answers (4)

starball
starball

Reputation: 50035

VS Code doesn't have "a formatter". It has multiple builtin formatting providers and extensions can implement their own formatting providers as well.

There's no easy way to do what is being asked for as far as I am aware at the time of this writing.

I'd suggest to figure out if there's an underlying formatting tool that whichever formatter you're using in VS Code is built on top of / wrapping, and then find out if you can invoke that tool from the commandline. This will probably require reading some docs and/or source code. I give an example of the process for doing this in my answer to What JSON formatter does Visual Studio Code use to format JSON?. It's a bit involved and the exact process very likely varies significantly depending on the formatter / VS Code extension providing the formatting capability, so this is kind of the best answer I can imagine giving without giving a thorough answer for every formatter under the sun for all time here, which would be too broad.

But to try to generalize,

  • Find out which extension is providing the formatting functionality- it may be one you manually installed, or one that is built in to VS Code. The formatter should be indicated in the status bar- click the {} icon to get a popup with that info. Or check if you've touched the editor.defaultFormatter setting for any language override settings
  • Go to that extension's page in the Extensions View in VS Code
  • If the docs mention that there's an underlying formatter tool, then you can skip a lot of the below steps
  • Find the link to the extension's source code repository (it will only be there if the extension provides that link in its extension manifest
  • If the extension is closed-source, you'll have to work with the extension as it is installed (see the .vscode/extensions/ directory under your user home directory)
  • From this point, you'll have to figure out how the formatting is implemented and hope that it's implemented by calling an underlying formatting tool that you can use via commandline or write a commandline wrapper for. You can try looking to see if the depndencies property of the extension manifest (package.json file) gives any info, or otherwise grep for things related to DocumentFormattingEditProvider and see if there are any calls to a non-JS program.

This could theoretically be simpler if VS Code made it so, but it does not- at least- not at the time of this writing. See Possible to invoke a VSCode extension command from command line? and Can a command be send to VSCode, directly from the integrated terminal (integrated terminal -> VSCode API)?, for example.

Upvotes: 4

kritisingh7488
kritisingh7488

Reputation: 1

Format Document (ctrl+shift+I) - Format the entire active file.
Format Selection (ctrl+K - ctrl+F) - Format the selected text.

Upvotes: -3

Luke Miles
Luke Miles

Reputation: 1160

As of July 2021, it seems vscode does not provide any way to run its formatter outside of the app itself

Upvotes: 2

A Webb
A Webb

Reputation: 711

There is a great git hooks tool called Husky and its documentation can be found here.

Here is an example of it inside of a package.json file that uses pretty-quick to execute prettier whenever a git pre-commit is executed.

"husky": {
    "hooks": {
      "pre-commit": "pretty-quick --staged"
    }
  },
....

I find this method far superior to running prettier on every file save.

Upvotes: 1

Related Questions