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- [`show_completions_on_input`](./configuring-zed.md#show-completions-on-input): Whether or not to show completions as you type
 60- [`show_completion_documentation`](./configuring-zed.md#show-completion-documentation): Whether to display inline and alongside documentation for items in the completions menu
 61
 62These settings allow you to maintain specific coding styles across different languages and projects.
 63
 64## File Associations
 65
 66Zed automatically detects file types based on their extensions, but you can customize these associations to fit your workflow.
 67
 68To set up custom file associations, use the [`file_types`](./configuring-zed.md#file-types) setting in your `settings.json`:
 69
 70```json
 71"file_types": {
 72  "C++": ["c"],
 73  "TOML": ["MyLockFile"],
 74  "Dockerfile": ["Dockerfile*"]
 75}
 76```
 77
 78This configuration tells Zed to:
 79
 80- Treat `.c` files as C++ instead of C
 81- Recognize files named "MyLockFile" as TOML
 82- Apply Dockerfile syntax to any file starting with "Dockerfile"
 83
 84You can use glob patterns for more flexible matching, allowing you to handle complex naming conventions in your projects.
 85
 86## Working with Language Servers
 87
 88Language servers are a crucial part of Zed's intelligent coding features, providing capabilities like auto-completion, go-to-definition, and real-time error checking.
 89
 90### What are Language Servers?
 91
 92Language 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.
 93
 94Some key features provided by language servers include:
 95
 96- Code completion
 97- Error checking and diagnostics
 98- Code navigation (go to definition, find references)
 99- Code actions (Rename, extract method)
100- Hover information
101- Workspace symbol search
102
103### Managing Language Servers
104
105Zed simplifies language server management for users:
106
1071. 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.
108
1092. Storage Location:
110
111   - macOS: `~/Library/Application Support/Zed/languages`
112   - Linux: `$XDG_DATA_HOME/zed/languages`, `$FLATPAK_XDG_DATA_HOME/zed/languages`, or `$HOME/.local/share/zed/languages`
113
1143. Automatic Updates: Zed keeps your language servers up-to-date, ensuring you always have the latest features and improvements.
115
116### Choosing Language Servers
117
118Some 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.
119
120You can specify your preference using the `language_servers` setting:
121
122```json
123  "languages": {
124    "PHP": {
125      "language_servers": ["intelephense", "!phpactor", "..."]
126    }
127  }
128```
129
130In this example:
131
132- `intelephense` is set as the primary language server
133- `phpactor` is disabled (note the `!` prefix)
134- `...` expands to the rest of the language servers that are registered for PHP
135
136This 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.
137
138### Toolchains
139
140Some language servers need to be configured with a current "toolchain", which is an installation of a specific version of a programming language compiler or/and interpreter, which can possibly include a full set of dependencies of a project.
141An example of what Zed considers a toolchain is a virtual environment in Python.
142Not all languages in Zed support toolchain discovery and selection, but for those that do, you can specify the toolchain from a toolchain picker (via {#action toolchain::Select}). To learn more about toolchains in Zed, see [`toolchains`](./toolchains.md).
143
144### Configuring Language Servers
145
146Many language servers accept custom configuration options. You can set these in the `lsp` section of your `settings.json`:
147
148```json
149  "lsp": {
150    "rust-analyzer": {
151      "initialization_options": {
152        "check": {
153          "command": "clippy"
154        }
155      }
156    }
157  }
158```
159
160This example configures the Rust Analyzer to use Clippy for additional linting when saving files.
161
162#### Nested objects
163
164When 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:
165
166Suppose you want to configure the following settings for TypeScript:
167
168- Enable strict null checks
169- Set the target ECMAScript version to ES2020
170
171Here's how you would structure these settings in Zed's `settings.json`:
172
173```json
174"lsp": {
175  "typescript-language-server": {
176    "initialization_options": {
177      // These are not supported (VSCode dotted style):
178      // "preferences.strictNullChecks": true,
179      // "preferences.target": "ES2020"
180      //
181      // These is correct (nested notation):
182      "preferences": {
183        "strictNullChecks": true,
184        "target": "ES2020"
185      },
186    }
187  }
188}
189```
190
191#### Possible configuration options
192
193Depending on how a particular language server is implemented, they may depend on different configuration options, both specified in the LSP.
194
195- [initializationOptions](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#version_3_17_0)
196
197Sent once during language server startup, requires server's restart to reapply changes.
198
199For example, rust-analyzer and clangd rely on this way of configuring only.
200
201```json
202  "lsp": {
203    "rust-analyzer": {
204      "initialization_options": {
205        "checkOnSave": false
206      }
207    }
208  }
209```
210
211- [Configuration Request](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_configuration)
212
213May be queried by the server multiple times.
214Most of the servers would rely on this way of configuring only.
215
216```json
217"lsp": {
218  "tailwindcss-language-server": {
219    "settings": {
220      "tailwindCSS": {
221        "emmetCompletions": true,
222      },
223    }
224  }
225}
226```
227
228Apart of the LSP-related server configuration options, certain servers in Zed allow configuring the way binary is launched by Zed.
229
230Language servers are automatically downloaded or launched if found in your path, if you wish to specify an explicit alternate binary you can specify that in settings:
231
232```json
233  "lsp": {
234    "rust-analyzer": {
235      "binary": {
236        // Whether to fetch the binary from the internet, or attempt to find locally.
237        "ignore_system_version": false,
238        "path": "/path/to/langserver/bin",
239        "arguments": ["--option", "value"],
240        "env": {
241          "FOO": "BAR"
242        }
243      }
244    }
245  }
246```
247
248### Enabling or Disabling Language Servers
249
250You can toggle language server support globally or per-language:
251
252```json
253  "languages": {
254    "Markdown": {
255      "enable_language_server": false
256    }
257  }
258```
259
260This disables the language server for Markdown files, which can be useful for performance in large documentation projects. You can configure this globally in your `~/.config/zed/settings.json` or inside a `.zed/settings.json` in your project directory.
261
262## Formatting and Linting
263
264Zed provides support for code formatting and linting to maintain consistent code style and catch potential issues early.
265
266### Configuring Formatters
267
268Zed supports both built-in and external formatters. See [`formatter`](./configuring-zed.md#formatter) docs for more. You can configure formatters globally or per-language in your `settings.json`:
269
270```json
271"languages": {
272  "JavaScript": {
273    "formatter": {
274      "external": {
275        "command": "prettier",
276        "arguments": ["--stdin-filepath", "{buffer_path}"]
277      }
278    },
279    "format_on_save": "on"
280  },
281  "Rust": {
282    "formatter": "language_server",
283    "format_on_save": "on"
284  }
285}
286```
287
288This example uses Prettier for JavaScript and the language server's formatter for Rust, both set to format on save.
289
290To disable formatting for a specific language:
291
292```json
293"languages": {
294  "Markdown": {
295    "format_on_save": "off"
296  }
297}
298```
299
300### Setting Up Linters
301
302Linting in Zed is typically handled by language servers. Many language servers allow you to configure linting rules:
303
304```json
305"lsp": {
306  "eslint": {
307    "settings": {
308      "codeActionOnSave": {
309        "rules": ["import/order"]
310      }
311    }
312  }
313}
314```
315
316This configuration sets up ESLint to organize imports on save for JavaScript files.
317
318To run linter fixes automatically on save:
319
320```json
321"languages": {
322  "JavaScript": {
323    "formatter": {
324      "code_action": "source.fixAll.eslint"
325    }
326  }
327}
328```
329
330### Integrating Formatting and Linting
331
332Zed 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:
333
334```json
335"languages": {
336  "JavaScript": {
337    "formatter": [
338      {
339        "code_action": "source.fixAll.eslint"
340      },
341      {
342        "external": {
343          "command": "prettier",
344          "arguments": ["--stdin-filepath", "{buffer_path}"]
345        }
346      }
347    ],
348    "format_on_save": "on"
349  }
350}
351```
352
353### Troubleshooting
354
355If you encounter issues with formatting or linting:
356
3571. Check Zed's log file for error messages (Use the command palette: `zed: open log`)
3582. Ensure external tools (formatters, linters) are correctly installed and in your PATH
3593. Verify configurations in both Zed settings and language-specific config files (e.g., `.eslintrc`, `.prettierrc`)
360
361## Syntax Highlighting and Themes
362
363Zed offers customization options for syntax highlighting and themes, allowing you to tailor the visual appearance of your code.
364
365### Customizing Syntax Highlighting
366
367Zed uses Tree-sitter grammars for syntax highlighting. Override the default highlighting using the `experimental.theme_overrides` setting.
368
369This example makes comments italic and changes the color of strings:
370
371```json
372"experimental.theme_overrides": {
373  "syntax": {
374    "comment": {
375      "font_style": "italic"
376    },
377    "string": {
378      "color": "#00AA00"
379    }
380  }
381}
382```
383
384### Selecting and Customizing Themes
385
386Change your theme:
387
3881. Use the theme selector ({#kb theme_selector::Toggle})
3892. Or set it in your `settings.json`:
390
391```json
392"theme": {
393  "mode": "dark",
394  "dark": "One Dark",
395  "light": "GitHub Light"
396}
397```
398
399Create custom themes by creating a JSON file in `~/.config/zed/themes/`. Zed will automatically detect and make available any themes in this directory.
400
401### Using Theme Extensions
402
403Zed supports theme extensions. Browse and install theme extensions from the Extensions panel ({#kb zed::Extensions}).
404
405To create your own theme extension, refer to the [Developing Theme Extensions](./extensions/themes.md) guide.
406
407## Using Language Server Features
408
409### Inlay Hints
410
411Inlay hints provide additional information inline in your code, such as parameter names or inferred types. Configure inlay hints in your `settings.json`:
412
413```json
414"inlay_hints": {
415  "enabled": true,
416  "show_type_hints": true,
417  "show_parameter_hints": true,
418  "show_other_hints": true
419}
420```
421
422For language-specific inlay hint settings, refer to the documentation for each language.
423
424### Code Actions
425
426Code 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.
427
428### Go To Definition and References
429
430Use these commands to navigate your codebase:
431
432- `editor: Go to Definition` (<kbd>f12|f12</kbd>)
433- `editor: Go to Type Definition` (<kbd>cmd-f12|ctrl-f12</kbd>)
434- `editor: Find All References` (<kbd>shift-f12|shift-f12</kbd>)
435
436### Rename Symbol
437
438To rename a symbol across your project:
439
4401. Place your cursor on the symbol
4412. Use the `editor: Rename Symbol` command (<kbd>f2|f2</kbd>)
4423. Enter the new name and press Enter
443
444These features depend on the capabilities of the language server for each language.
445
446When 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.
447
448### Hover Information
449
450Use the `editor: Hover` command to display information about the symbol under the cursor. This often includes type information, documentation, and links to relevant resources.
451
452### Workspace Symbol Search
453
454The `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.
455
456### Code Completion
457
458Zed 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.
459
460### Diagnostics
461
462Language servers provide real-time diagnostics (errors, warnings, hints) as you code. View all diagnostics for your project using the `diagnostics: Toggle` command.