Carlos Sosa
Carlos Sosa

Reputation: 579

Auto format as you type

I have configured VS Code and ESLint to automatically lint and format my code. The problem is that currently VS Code auto formats only if I press Cmd + S (Save). I was wondering if there is a way to auto format as I type or after a couple of seconds after typing? VS Code automatically saves the file but that doesn't trigger the auto format. I have to specifically press Cmd + S to trigger the auto format.

Upvotes: 3

Views: 2013

Answers (1)

Before I move on, I want to question whether using ESLint to format code is a good idea?

Why do I ask? – Because ESLint is a linter and not a formatter.
A good alternative would be to use for example Prettier instead.

1. Existing options

The existing options are explained at the official VS Code documentation on formatting.

There are three options :

  • editor.formatOnType - format the line after typing,
  • editor.formatOnSave - format a file on save, and
  • editor.formatOnPaste - format the pasted content.

Each of these three options has its limitation and does not offer the functionality that I want.
(And it seems you want the same functionality as I do.)

In short, the only of the three options that formats the entire file is formatOnSave.
But to format a file on save, the file must not be saved after delay.

2. A desired new feature – Format on Delay

I would love the possibility to format with a time delay, just as it is possible to save with a time delay.

I want to have the freedom to both Format on Delay and to Auto Save (on Delay).

This is not possible in the current release of VS Code (April 2022).

To make this a Backlog milestone – we have until May 30 (2022) to get the needed 20 votes.
In case you want to vote on my suggestion, here is where you can vote by giving it a thumbs up.

Upvotes: 2

Related Questions