Prettier
Making my code look pretty.
Prettier is an opinionated code formatter that enforces a consistent style by parsing your code and re-printing it with its own rules. When using the Prettier extension in Visual Studio Code (VSCode), it integrates seamlessly with the editor to format your code automatically based on the settings you provide.
How Prettier Works with VSCode
- Installation
- : You need to install the Prettier extension from the VSCode marketplace.
- Configuration
- : Prettier can be configured using aĀ
- .prettierrc
- Ā file,Ā
- prettier.config.js
- , or directly in theĀ
- settings.json
- Ā file of VSCode.
- Formatting
- : Prettier formats your code based on the configuration whenever you save a file or manually trigger the formatting command.
Configuring Prettier for Different Projects
To achieve different Prettier settings for different projects, you can use the following approach:
- Global Settings
- : Configure the default settings in your global VSCode settings (
- settings.json
- ).
- Project-Specific Settings
- : Override the global settings with project-specific settings using aĀ
- .prettierrc
- Ā file orĀ
- prettier.config.js
- Ā in the project directory.
- Disable Prettier for Specific Projects
- : Use theĀ
- prettier.disableLanguages
- Ā setting in the project'sĀ
- settings.json
- Ā to disable Prettier for specific file types.
Step-by-Step Configuration
1. Global Settings
Set the global Prettier settings in your VSCodeĀ settings.json:
{
"editor.formatOnSave":Ā true,
"prettier.tabWidth":Ā 4,
"prettier.useTabs":Ā false
}
2. Project-Specific Settings
Create aĀ .prettierrcĀ file in the root of the project where you want custom settings:
{
"tabWidth":Ā 2,
"useTabs":Ā false
}
Or useĀ prettier.config.js:
module.exportsĀ =Ā {
tabWidth:Ā 2,
useTabs:Ā false
};
3. Disable Prettier for Specific Projects
In the project where you want to disable Prettier, create or update theĀ .vscode/settings.jsonĀ file:
{
"editor.formatOnSave":Ā false,
"prettier.disableLanguages":Ā ["javascript",Ā "typescript",Ā "json"]
}
Example Directory Structure
project-root/
.prettierrc
.vscode/
settings.json
src/
Summary
- Global Settings
- : Configure inĀ
- settings.json
- Ā for default behavior.
- Project-Specific Settings
- : UseĀ
- .prettierrc
- Ā orĀ
- prettier.config.js
- Ā in the project directory.
- Disable Prettier
- : UseĀ
- .vscode/settings.json
- Ā in the project directory to disable Prettier for specific file types.
By following these steps, you can manage Prettier settings effectively across different projects in your workspace.