Is there a way to format all files in a project without formatting each one individually?
12 Answers
Use the extension called ”Format Files”. Here are the steps:
- Download the extension called ”Format Files” on VSCode.
- Select and open the folder with files to format on VSCode.
- Press Ctrl+Shift+P to open command palette.
- Enter "Start Format Files: Workspace" and select this option.
Source: https://marketplace.visualstudio.com/items?itemName=jbockle.jbockle-format-files

- 2,069
- 2
- 15
- 35

- 2,605
- 1
- 13
- 11
-
Got about halfway through then said "`An error occurred while running Format Files: Invalid count value`" – Aaron Franke May 02 '19 at 08:50
-
16This caused all my files to be broken cuz it doesn't use prettier. It breaks JSX code PLEASE FOR THE LOVE OF ALL THAT IS GOOD DONT EVER DO THIS! YOu will regret it. – SweetFeet Nov 15 '20 at 03:17
-
18@jake_314: Sorry that you had such a bad experience with this. But I hope you're using Git or took a backup of your code before doing major scripted code changing operations? Anyways I haven't tried this with JSX, but here is a link that might be helpful: https://www.rockyourcode.com/format-and-fix-all-files-with-prettier-and-es-lint-recursively/ – Stellan Lindell Nov 16 '20 at 09:33
-
3Worth keeping an eye on the excluded items setting for this extension. It just tried to format my entire node_modules folder. But apart from that seems to work well – Liam Apr 05 '22 at 10:29
-
2or right click on the folder and click on "Start Format File. this folder" – Shalabyer Jul 28 '22 at 12:55
This works for me
Install prettier:
npm init
npm i -D prettier
Add following script in package.json:
"pretty": "prettier --write \"./**/*.{js,jsx,ts,tsx,json}\""
In this case only, i need to format my .js .jsx and .json files.
Run script:
npm run pretty
-
24Probably want to add "--save-dev" to the npm install command, don't need this as a production dependency – reggaeguitar Feb 25 '20 at 23:59
-
13
-
2You can also use `eslint --fix` if you use both eslint and prettier. – LucasBordeau May 06 '20 at 14:43
-
1I agree that this answer should be downvoted. At the time I was looking for the answer to this question, I was mainly working with js/jsx/json code – Hieu Thai Nov 17 '20 at 15:13
-
@HiếuTháiNgọc Will this touch node_modules? Only reason I am afraid to run it. – spencer741 Apr 22 '21 at 09:22
-
@spencer741 Yes it will install pretty in local `node_moudles`. If you're afraid to touch the dependency system in the local folder, just copy all the source code into another place, format there, and copy the content back later. – Hieu Thai Apr 27 '21 at 08:46
-
@HieuThai I was more asking “will it prettify code in node_modules” – spencer741 Apr 27 '21 at 14:38
-
2VSCode is not just for nodejs. Question is about general formatting, for all code files. – Ramesh-X Sep 13 '22 at 01:48
-
For people coding in with .NET, there also exists a editor-agnostic solution: simply run `dotnet format
` to format all files. In the end I figured it's better to look for an extension-less solution since this is something that could run in a Git hook or automated workflow. – Stefan May 30 '23 at 20:01 -
No solution "found on the internet" should be copy/pasted blindly. For this solution, modify the RegEx to suit Your purposes, whether TS, TSX, JS, JSX, or any other file types. This one solution creates a script/shortcut to re-use. For a one-shot, use the full command-line option by @piyush-balapure. Both of these are correct answers. – TonyG Jul 27 '23 at 23:46
The simplest solution that I have found is as below.
- Install prettier in vscode.
- Create the .prettierrc file and configure it the way you want.
- Run following command in vscode console.
npx prettier --write "**/*.ts"
(Add the file type regex as per the need)

- 1,023
- 9
- 14
-
1This only works for js code and nothing about the question is asking about js, it's talking about formatting in general. – Jason Axelson Mar 05 '22 at 09:16
-
1You can change the regex to format the files supported by prettier. And because the question has JavaScript tag, most answers include js, ts, jsx etc. – Piyush Balapure May 13 '22 at 07:12
-
1the Javascript tag was added after, people just assumed it's for javascript then an editor added the tag – Jimmar May 18 '22 at 13:17
I was out of luck finding an extension that was doing this the way I was expecting it so I made one. I suggest you take a look at the extension I just made :
It might still have some issues, feel free to report them or to contribute.

- 594
- 1
- 5
- 12
-
3
-
1Please all, install and upvote this extension! Really essential for large projects that need reformatting using settings in VS code running, for example, in a containerized environment where the console command line might have different settings for the formatter. – Dan Nissenbaum Feb 16 '22 at 04:19
-
-
-
Just need and found it is working... 131,113 installs and counting... Accept this as an Answer... – Muhammad Hassan Jul 30 '23 at 12:36
With npm
, just run npx prettier --write .
in the terminal if your language is supported

- 4,943
- 3
- 17
- 34
-
1this is the best answer involving prettier, as this is what they say to do on the [prettier docs](https://prettier.io/docs/en/install.html) – benmneb Jul 25 '21 at 07:09
I do a simply trick:
- download this extension https://marketplace.visualstudio.com/items?itemName=ApceHHypocrite.OpenAllFiles
- open all files
- set "editor.formatOnSave": true
- save all files
Hope it helps

- 47,390
- 120
- 299
- 507
-
10What if your project has 3k+ files ? This is ok for a pet project but not as general advice. – LucasBordeau May 06 '20 at 14:42
if you want format any specific folder then just move the that folder and run command npx prettier --write .
eg : if you are in 'xyz/frontend' folder all the file inside will be formatted
same if you want to format only src folder under your frontend project the move to src via cd src
then run commant npx prettier --write .
this will only format files inside src

- 3,032
- 3
- 24
- 31
As @herrbischoff said, there is currently no way to format all files in a project.
However it would be a useful feature.
What it can do is format all unsaved files by having auto-save and auto-format on.
Otherwise you would need a shell script or an extension or some other extern program (like a tslint checker which can auto-correct errors) which is capable of doing this.
Had problems with this myself and it sucks to open all files by hand
Enable format on save option, now make a tiny change that will affect all the files (replace all -> class to classic), and save all, and done.

- 137
- 7
Run this command and change the extension In my case I changed that to js, vue
npx prettier --write "**/*.js"
npx prettier --write "**/*.vue"

- 121
- 2
- 10
There is a great way for doing that
1- First install the formatter named Csharpier
You can see how to install it here: https://csharpier.com/docs/Installation
2- Open your project(no matter which IDE you use)
3- Open Terminal
Note: Make sure you're in the main directory of your project
Then type this code in your terminal:
dotnet-csharpier . --write
FINISH :)
Don't forget to commit your the changes you've made before doing formatting, Because after formatting, you'll have lots of changes in your git and it will be a little hard to distinct your own changes and the changes which are made by Csharpier.

- 9
- 3
There is currently no way to do that nor does it sound like a particularly useful feature to have. Or put another way: it would be a useful feature if you could completely trust it, which you can't.
You would have to put a lot of faith into the auto-formatting logic of the used languages to not screw up and possibly introduce errors. You would need to review the changes manually anyway, so this approach should not result in measurable productivity gains.
If you're working with a seriously f'ed up code base and don't care about possible problems, I would suggest running a simple shell command with the respective languages' CLI formatter. Example for C++ code, using clang-format:
find . -iname *.cpp -exec clang-format {} +
This command will find all cpp files recursively and will run them through the formatter with default settings.
The process is essentially the same for any language, for example JavaScript (with js-beautify):
find . -iname *.js -exec js-beautify {} +
Just make sure you review whatever comes out. Also, it may very well be possible to script this command into VScode — or just run it in the built-in terminal.

- 3,294
- 1
- 29
- 50
-
25How wouldn't it be a useful feature; if you change a formatting rule, it's better to apply it to all files in one go, rather than have formatting changes be mixed in with other code changes. – slikts Feb 22 '18 at 10:39
-
1It's rather obvious: because it may be dangerous. Every automatic formatting applied over all files, especially projects you don't know intimately, can break more than it fixes. Just think of languages like Python where indentation matters. Unchecked automatic reformatting *will* break things. You are of course free to find that out for your own the hard way. – herrbischoff Jun 04 '18 at 21:15
-
3I have been doing this for years with Intellij and Resharper and have never had an issue. No "hard way" really, if I trust the format enough to do it automatically for one file, I trust it enough for all the others. I do recommend an approach of explicitly choosing the rules I.E. defaulting rules to off and slowly turning them on. – Fortytwo Nov 07 '18 at 18:25
-
6By this argument, formatting a whole file would be too dangerous. You should only be able to format a block. If a whole file is OK, what if I select TWO files in the same directory? If that's OK, what if I select a directory? There are lots of use cases (e.g. @slikts ) where this makes perfect sense, and lots of environments (including python) where reformatters/prettifiers/code standardizers work fine. – Marvin Mar 12 '19 at 03:04
-
Well, it’s probably the old discussion of expectations from IDE vs. code editor users. I don’t expect my editor to be too smart for its own good but clearly many do. And apparently there is now an extension for that. – herrbischoff Mar 13 '19 at 08:48
-
6This seems like a pretty opinionated response. It isn't uncommong to come across a (non-Python) codebase with loads of indendtation errors. Default action is to format the file using the current rules, ensure project builds, tests pass, then review your file in git (possibly ignoring whitespace). I'd subscribe to a method that allows me to do this with a project rather than single files. – lorengphd Feb 16 '20 at 18:16