Auto Code Formatting in Cursor
tip
Cursor provides powerful code formatting capabilities that help maintain consistent code style across your projects.
Setting Up Formatters
Built-in Formatter
Cursor comes with a built-in formatter that supports multiple languages. To use it:
- Open Command Palette (Ctrl/Cmd + Shift + P)
- Type "Format Document"
- Press Enter
Popular Formatters
- Prettier
- ESLint
# Install Prettier
npm install --save-dev prettier
# Create config file
echo {} > .prettierrc.json
# Install ESLint
npm install --save-dev eslint
# Initialize ESLint
npx eslint --init
Configuration Files
Prettier Configuration
.prettierrc.json
{
"semi": true,
"trailingComma": "es5",
"singleQuote": true,
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"bracketSpacing": true,
"arrowParens": "avoid"
}
ESLint Configuration
.eslintrc.json
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"prettier"
],
"rules": {
"indent": ["error", 2],
"linebreak-style": ["error", "unix"],
"quotes": ["error", "single"],
"semi": ["error", "always"]
}
}
Format on Save
Enable Auto-formatting
.vscode/settings.json
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
Language-specific Settings
{
"[python]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "ms-python.python"
},
"[java]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "redhat.java"
}
}
Keyboard Shortcuts
Default formatting shortcuts:
Action | Windows/Linux | macOS |
---|---|---|
Format Document | Shift + Alt + F | Shift + Option + F |
Format Selection | Ctrl + K Ctrl + F | Cmd + K Cmd + F |
Advanced Configuration
Ignore Files
.prettierignore
# Build output
dist/
build/
# Dependencies
node_modules/
# Generated files
*.generated.*
ESLint Integration
.eslintrc.json
{
"extends": [
"eslint:recommended",
"plugin:prettier/recommended"
],
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error"
}
}
Custom Formatting Rules
Style Guide Enforcement
.prettierrc.json
{
"overrides": [
{
"files": "*.component.ts",
"options": {
"printWidth": 120,
"singleQuote": true
}
},
{
"files": "*.spec.ts",
"options": {
"printWidth": 80,
"semi": true
}
}
]
}
Project-specific Rules
.editorconfig
root = true
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 2
[*.{js,ts}]
quote_type = single
[*.py]
indent_size = 4
Troubleshooting
Common Issues
-
Formatting Not Working
- Check formatter installation
- Verify configuration files
- Ensure file is not ignored
-
Conflicts Between Tools
.vscode/settings.json{
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"eslint.format.enable": false
} -
Performance Issues
{
"editor.formatOnSaveTimeout": 5000,
"editor.formatOnPaste": false
}
Best Practices
1. Version Control
.gitignore
# Don't ignore formatting configs
!.prettierrc
!.eslintrc
!.editorconfig
# Ignore cache files
.prettier-cache
.eslintcache
2. Team Consistency
Create a shared configuration:
.vscode/extensions.json
{
"recommendations": [
"esbenp.prettier-vscode",
"dbaeumer.vscode-eslint"
]
}
3. Pre-commit Hooks
package.json
{
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,ts,tsx}": [
"prettier --write",
"eslint --fix"
]
}
}
IDE Integration
Custom Keybindings
keybindings.json
[
{
"key": "ctrl+shift+i",
"command": "editor.action.formatDocument",
"when": "editorHasDocumentFormattingProvider"
}
]
Workspace Settings
.vscode/settings.json
{
"editor.formatOnSave": true,
"editor.formatOnPaste": true,
"editor.formatOnType": true,
"files.trimTrailingWhitespace": true,
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true
}
tip
Remember to commit your formatting configuration files to ensure consistent formatting across your team!