Is there a way to format all files in a project without formatting each one individually?
13 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
6 Comments
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
-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.eslint --fix if you use both eslint and prettier.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
With npm, just run npx prettier --write . in the terminal if your language is supported
2 Comments
. at the end.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.
2 Comments
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
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
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
1 Comment
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
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
#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
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.
