348

Is there a way to format all files in a project without formatting each one individually?

0

13 Answers 13

282

Use the extension called ”Format Files”. Here are the steps:

  1. Download the extension called ”Format Files” on VSCode.
  2. Select and open the folder with files to format on VSCode.
  3. Press Ctrl+Shift+P to open command palette.
  4. Enter "Start Format Files: Workspace" and select this option.

Source: https://marketplace.visualstudio.com/items?itemName=jbockle.jbockle-format-files

Sign up to request clarification or add additional context in comments.

6 Comments

This 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.
@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: rockyourcode.com/…
Worth 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
or right click on the folder and click on "Start Format File. this folder"
Thank you, works like a charm on a C/C++ project. As pointed out by others, keep an eye on the excluded folders and file lists. The extension lists what it wants to reformat before actually reformatting, so you can tune and test your excludes thoroughly before any real action. And off course, make a back-up of you project first.
|
210

This works for me

Install prettier:

npm init 
npm i -D prettier --save-dev

Add following script in package.json:

"pretty": "prettier --write \"./**/*.{js,jsx,mjs,cjs,ts,tsx,json}\"" 

In this case only, i need to format my .js .jsx and .json files.

Run script:

npm run pretty

13 Comments

Probably want to add "--save-dev" to the npm install command, don't need this as a production dependency
For the record -D is the shorthand for --save dev so it's kind of redundant now, but I agree that the longer flags are better for explaining.
Implying the question is about javascript. I see no tags related to it.
You can also use eslint --fix if you use both eslint and prettier.
I 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
VSCode is not just for nodejs. Question is about general formatting, for all code files.
|
84

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)

3 Comments

This only works for js code and nothing about the question is asking about js, it's talking about formatting in general.
You 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.
the Javascript tag was added after, people just assumed it's for javascript then an editor added the tag
67

With npm, just run npx prettier --write . in the terminal if your language is supported

2 Comments

this is the best answer involving prettier, as this is what they say to do on the prettier docs
Great solution that worked for me without any additional installs. Don't forget the . at the end.
53

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 :

https://marketplace.visualstudio.com/items?itemName=lacroixdavid1.vscode-format-context-menu#overview

It might still have some issues, feel free to report them or to contribute.

2 Comments

Please 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.
Just need and found it is working... 131,113 installs and counting... Accept this as an Answer...
7

VSCode Workspace Formatter

I recommend VSCode Workspace Formatter. Just formatted a subdirectory with a few hundred source code files, and it worked very well.

  • Extension automates open+format+save steps.
  • Applies VSCode's formatting settings, including formatting extensions.
  • Format the workspace or a subdirectory (right-click in file tree).
  • Configurable include/exclude path globs.
  • Available in Open VSX and thus VSCodium.

See

1 Comment

Tried it. It works well for me. The extension provides a filter to exclude specific files and folders using regex.
5

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

1 Comment

appreciate how you included "." in the highlight :))
3

I do a simply trick:

  1. download this extension https://marketplace.visualstudio.com/items?itemName=ApceHHypocrite.OpenAllFiles
  2. open all files
  3. set "editor.formatOnSave": true
  4. save all files

Hope it helps

1 Comment

What if your project has 3k+ files ? This is ok for a pet project but not as general advice.
0

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

Comments

-1

Run this command and change the extension In my case I changed that to js, vue

npx prettier --write "**/*.js"

npx prettier --write "**/*.vue"

Comments

-1

You may decide to use prettier + eslint setup for this feature.

eslint.config.ts

const eslintConfigPrettier = require("eslint-config-prettier");
const eslintPluginPrettierRecommended = require('eslint-plugin-prettier/recommended');

module.exports = [
  ... (Other configs - should be before prettier ones)
  eslintConfigPrettier,
  eslintPluginPrettierRecommended,
];

Also you need .orettierrc file:

{
  "semi": false,
  "singleQuote": true,
  "trailingComma": "es5",
  "tabWidth": 2,
  ... (Other prettier settings)
}

Now you may run this via eslint src --config eslint.config.js --fix with fixing all warnings like incorrect tab width.

Works with eslint>=8.24.0. Flat config provided above. For earlier version you may decide to use older config files.

Comments

-4

#UPDATE OF ANSWER: 18/09/2023#

There is a great way to do that
I want to tell you about two formatters and then you can consider the others from these

For language C#, Csharpier is a great formatter
For many languages like Python, JS, HTML, CSS & ..., Prettier is a great formatter

1- First install the formatter

You can see how to install it here:
Csharpier: https://csharpier.com/docs/Installation
Prettier: https://prettier.io/docs/en/install
For other formatters, Please try to read their official website's documentation

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
prettier . --write or npm prettier . --write FINISH :)
Don't forget to commit 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 distinguish your changes and the changes that are made by Formatter.

Comments

-7

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.

6 Comments

How 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.
It'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.
I 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.
By 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.
This 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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.