configuring-languages.md

  1# Configuring supported languages
  2
  3Zed offers powerful customization options for each programming language it supports. This guide will walk you through the various ways you can tailor your coding experience to your preferences and project requirements.
  4
  5Zed's language support is built on two main technologies:
  6
  71. Tree-sitter: This handles syntax highlighting and structure-based features like the outline panel.
  82. Language Server Protocol (LSP): This provides semantic features such as code completion and diagnostics.
  9
 10These components work together to provide Zed's language capabilities.
 11
 12In this guide, we'll cover:
 13
 14- Language-specific settings
 15- File associations
 16- Working with language servers
 17- Formatting and linting configuration
 18- Customizing syntax highlighting and themes
 19- Advanced language features
 20
 21By the end of this guide, you should know how to configure and customize supported languages in Zed.
 22
 23For a comprehensive list of languages supported by Zed and their specific configurations, see our [Supported Languages](./languages.md) page. To go further, you could explore developing your own extensions to add support for additional languages or enhance existing functionality. For more information on creating language extensions, see our [Language Extensions](./extensions/languages.md) guide.
 24
 25## Language-specific Settings
 26
 27Zed allows you to override global settings for individual languages. These custom configurations are defined in your `settings.json` file under the `languages` key.
 28
 29Here's an example of language-specific settings:
 30
 31```json
 32"languages": {
 33  "Python": {
 34    "tab_size": 4,
 35    "formatter": "language_server",
 36    "format_on_save": "on"
 37  },
 38  "JavaScript": {
 39    "tab_size": 2,
 40    "formatter": {
 41      "external": {
 42        "command": "prettier",
 43        "arguments": ["--stdin-filepath", "{buffer_path}"]
 44      }
 45    }
 46  }
 47}
 48```
 49
 50You can customize a wide range of settings for each language, including:
 51
 52- [`tab_size`](./configuring-zed.md#tab-size): The number of spaces for each indentation level
 53- [`formatter`](./configuring-zed.md#formatter): The tool used for code formatting
 54- [`format_on_save`](./configuring-zed.md#format-on-save): Whether to automatically format code when saving
 55- [`enable_language_server`](./configuring-zed.md#enable-language-server): Toggle language server support
 56- [`hard_tabs`](./configuring-zed.md#hard-tabs): Use tabs instead of spaces for indentation
 57- [`preferred_line_length`](./configuring-zed.md#preferred-line-length): The recommended maximum line length
 58- [`soft_wrap`](./configuring-zed.md#soft-wrap): How to wrap long lines of code
 59
 60These settings allow you to maintain specific coding styles across different languages and projects.
 61
 62## File Associations
 63
 64Zed automatically detects file types based on their extensions, but you can customize these associations to fit your workflow.
 65
 66To set up custom file associations, use the [`file_types`](./configuring-zed.md#file-types) setting in your `settings.json`:
 67
 68```json
 69"file_types": {
 70  "C++": ["c"],
 71  "TOML": ["MyLockFile"],
 72  "Dockerfile": ["Dockerfile*"]
 73}
 74```
 75
 76This configuration tells Zed to:
 77
 78- Treat `.c` files as C++ instead of C
 79- Recognize files named "MyLockFile" as TOML
 80- Apply Dockerfile syntax to any file starting with "Dockerfile"
 81
 82You can use glob patterns for more flexible matching, allowing you to handle complex naming conventions in your projects.
 83
 84## Working with Language Servers
 85
 86Language servers are a crucial part of Zed's intelligent coding features, providing capabilities like auto-completion, go-to-definition, and real-time error checking.
 87
 88### What are Language Servers?
 89
 90Language servers implement the Language Server Protocol (LSP), which standardizes communication between the editor and language-specific tools. This allows Zed to support advanced features for multiple programming languages without implementing each feature separately.
 91
 92Some key features provided by language servers include:
 93
 94- Code completion
 95- Error checking and diagnostics
 96- Code navigation (go to definition, find references)
 97- Code actions (Rename, extract method)
 98- Hover information
 99- Workspace symbol search
100
101### Managing Language Servers
102
103Zed simplifies language server management for users:
104
1051. Automatic Download: When you open a file with a matching file type, Zed automatically downloads the appropriate language server. Zed may prompt you to install an extension for known file types.
106
1072. Storage Location:
108
109   - macOS: `~/Library/Application Support/Zed/languages`
110   - Linux: `$XDG_DATA_HOME/languages`, `$FLATPAK_XDG_DATA_HOME/languages`, or `$HOME/.local/share`
111
1123. Automatic Updates: Zed keeps your language servers up-to-date, ensuring you always have the latest features and improvements.
113
114### Choosing Language Servers
115
116Some languages in Zed offer multiple language server options. You might have multiple extensions installed that bundle language servers targeting the same language, potentially leading to overlapping capabilities. To ensure you get the functionality you prefer, Zed allows you to prioritize which language servers are used and in what order.
117
118You can specify your preference using the `language_servers` setting:
119
120```json
121  "languages": {
122    "PHP": {
123      "language_servers": ["intelephense", "!phpactor", "..."]
124    }
125  }
126```
127
128In this example:
129
130- `intelephense` is set as the primary language server
131- `phpactor` is disabled (note the `!` prefix)
132- `...` preserves any other default language server settings
133
134This configuration allows you to tailor the language server setup to your specific needs, ensuring that you get the most suitable functionality for your development workflow.
135
136### Configuring Language Servers
137
138Many language servers accept custom configuration options. You can set these in the `lsp` section of your `settings.json`:
139
140```json
141  "lsp": {
142    "rust-analyzer": {
143      "initialization_options": {
144        "checkOnSave": {
145          "command": "clippy"
146        }
147      }
148    }
149  }
150```
151
152This example configures the Rust Analyzer to use Clippy for additional linting when saving files.
153
154#### Nested objects
155
156When configuring language server options in Zed, it's important to use nested objects rather than dot-delimited strings. This is particularly relevant when working with more complex configurations. Let's look at a real-world example using the TypeScript language server:
157
158Suppose you want to configure the following settings for TypeScript:
159
160- Enable strict null checks
161- Set the target ECMAScript version to ES2020
162
163Here's how you would structure these settings in Zed's `settings.json`:
164
165```json
166"lsp": {
167  "typescript-language-server": {
168    "initialization_options": {
169      // These are not supported (VSCode dotted style):
170      // "preferences.strictNullChecks": true,
171      // "preferences.target": "ES2020"
172      //
173      // These is correct (nested notation):
174      "preferences": {
175        "strictNullChecks": true,
176        "target": "ES2020"
177      },
178    }
179  }
180}
181```
182
183### Enabling or Disabling Language Servers
184
185You can toggle language server support globally or per-language:
186
187```json
188  "languages": {
189    "Markdown": {
190      "enable_language_server": false
191    }
192  }
193```
194
195This disables the language server for Markdown files, which can be useful for performance in large documentation projects. You can configure this globally in your `~/.zed/settings.json` or inside a `.zed/settings.json` in your project directory.
196
197## Formatting and Linting
198
199Zed provides support for code formatting and linting to maintain consistent code style and catch potential issues early.
200
201### Configuring Formatters
202
203Zed supports both built-in and external formatters. Configure formatters globally or per-language in your `settings.json`:
204
205```json
206"languages": {
207  "JavaScript": {
208    "formatter": {
209      "external": {
210        "command": "prettier",
211        "arguments": ["--stdin-filepath", "{buffer_path}"]
212      }
213    },
214    "format_on_save": "on"
215  },
216  "Rust": {
217    "formatter": "language_server",
218    "format_on_save": "on"
219  }
220}
221```
222
223This example uses Prettier for JavaScript and the language server's formatter for Rust, both set to format on save.
224
225To disable formatting for a specific language:
226
227```json
228"languages": {
229  "Markdown": {
230    "format_on_save": "off"
231  }
232}
233```
234
235### Setting Up Linters
236
237Linting in Zed is typically handled by language servers. Many language servers allow you to configure linting rules:
238
239```json
240"lsp": {
241  "eslint": {
242    "settings": {
243      "codeActionOnSave": {
244        "rules": ["import/order"]
245      }
246    }
247  }
248}
249```
250
251This configuration sets up ESLint to organize imports on save for JavaScript files.
252
253To run linter fixes automatically on save:
254
255```json
256"languages": {
257  "JavaScript": {
258    "code_actions_on_format": {
259      "source.fixAll.eslint": true
260    }
261  }
262}
263```
264
265### Integrating Formatting and Linting
266
267Zed allows you to run both formatting and linting on save. Here's an example that uses Prettier for formatting and ESLint for linting JavaScript files:
268
269```json
270"languages": {
271  "JavaScript": {
272    "formatter": {
273      "external": {
274        "command": "prettier",
275        "arguments": ["--stdin-filepath", "{buffer_path}"]
276      }
277    },
278    "code_actions_on_format": {
279      "source.fixAll.eslint": true
280    },
281    "format_on_save": "on"
282  }
283}
284```
285
286### Troubleshooting
287
288If you encounter issues with formatting or linting:
289
2901. Check Zed's log file for error messages (Use the command palette: `zed: open log`)
2912. Ensure external tools (formatters, linters) are correctly installed and in your PATH
2923. Verify configurations in both Zed settings and language-specific config files (e.g., `.eslintrc`, `.prettierrc`)
293
294## Syntax Highlighting and Themes
295
296Zed offers customization options for syntax highlighting and themes, allowing you to tailor the visual appearance of your code.
297
298### Customizing Syntax Highlighting
299
300Zed uses Tree-sitter grammars for syntax highlighting. Override the default highlighting using the `experimental.theme_overrides` setting.
301
302This example makes comments italic and changes the color of strings:
303
304```json
305"experimental.theme_overrides": {
306  "syntax": {
307    "comment": {
308      "font_style": "italic"
309    },
310    "string": {
311      "color": "#00AA00"
312    }
313  }
314}
315```
316
317### Selecting and Customizing Themes
318
319Change your theme:
320
3211. Use the theme selector (<kbd>cmd-k cmd-t|ctrl-k ctrl-t</kbd>)
3222. Or set it in your `settings.json`:
323
324```json
325"theme": {
326  "mode": "dark",
327  "dark": "One Dark",
328  "light": "GitHub Light"
329}
330```
331
332Create custom themes by creating a JSON file in `~/.config/zed/themes/`. Zed will automatically detect and make available any themes in this directory.
333
334### Using Theme Extensions
335
336Zed supports theme extensions. Browse and install theme extensions from the Extensions panel (<kbd>cmd-shift-e|ctrl-shift-e</kbd>).
337
338To create your own theme extension, refer to the [Developing Theme Extensions](./extensions/themes.md) guide.
339
340## Using Language Server Features
341
342### Inlay Hints
343
344Inlay hints provide additional information inline in your code, such as parameter names or inferred types. Configure inlay hints in your `settings.json`:
345
346```json
347"inlay_hints": {
348  "enabled": true,
349  "show_type_hints": true,
350  "show_parameter_hints": true,
351  "show_other_hints": true
352}
353```
354
355For language-specific inlay hint settings, refer to the documentation for each language.
356
357### Code Actions
358
359Code actions provide quick fixes and refactoring options. Access code actions using the `editor: Toggle Code Actions` command or by clicking the lightbulb icon that appears next to your cursor when actions are available.
360
361### Go To Definition and References
362
363Use these commands to navigate your codebase:
364
365- `editor: Go to Definition` (<kbd>f12|f12</kbd>)
366- `editor: Go to Type Definition` (<kbd>cmd-f12|ctrl-f12</kbd>)
367- `editor: Find All References` (<kbd>shift-f12|shift-f12</kbd>)
368
369### Rename Symbol
370
371To rename a symbol across your project:
372
3731. Place your cursor on the symbol
3742. Use the `editor: Rename Symbol` command (<kbd>f2|f2</kbd>)
3753. Enter the new name and press Enter
376
377These features depend on the capabilities of the language server for each language.
378
379When renaming a symbol that spans multiple files, Zed will open a preview in a multibuffer. This allows you to review all the changes across your project before applying them. To confirm the rename, simply save the multibuffer. If you decide not to proceed with the rename, you can undo the changes or close the multibuffer without saving.
380
381### Hover Information
382
383Use the `editor: Show Hover` command to display information about the symbol under the cursor. This often includes type information, documentation, and links to relevant resources.
384
385### Workspace Symbol Search
386
387The `workspace: Open Symbol` command allows you to search for symbols (functions, classes, variables) across your entire project. This is useful for quickly navigating large codebases.
388
389### Code Completion
390
391Zed provides intelligent code completion suggestions as you type. You can manually trigger completion with the `editor: Show Completions` command. Use <kbd>tab|tab</kbd> or <kbd>enter|enter</kbd> to accept suggestions.
392
393### Diagnostics
394
395Language servers provide real-time diagnostics (errors, warnings, hints) as you code. View all diagnostics for your project using the `diagnostics: Toggle` command.