zed
zed copied to clipboard
Language server agnostic snippets
Check for existing issues
- [X] Completed
Describe the feature
Other editors have the ability to define snippet without setting up in the LSP. For example, one can still define snippets for editing plain text files and that does not require user to setup initialization options of a LSP. (And we don't generally have a LSP for plain text files.)
Also some snippets might shared across different file types/LSPs.
If applicable, add mockups / screenshots to help present your vision of the feature
One example use-case is authoring HTML embedded in a string/template within another language. By default, Sublime maps Ctrl+W to an 'insert tag' snippet which makes this very easy and provides a consistent workflow across languages.
Hi, when will this happen?
Is there any ongoing work on this feature? Would be happy to join/support here
is this implement? how to add custom snippets in zed like we in vscode javascript.json?
+1 for requesting this feature. It's a huge time saver!
A very important feature for me is luasnip's auto snippets. Snippets marked as "auto" will trigger the moment the prefix is typed out, without having to select the snippet in the autocomplete menu and press enter. While this might sound useless, it's super handy when having to type LaTeX quickly (i.e.: when taking live notes during a lecture)
I want to switch fully to Zed for Go projects and this feature prevents me from switching. Would be great to have it.
Snippet support is now available as "Snippets" extension in the store. Give it a go; it should work with both Stable and Preview versions of Zed.
You can now store your snippets in ~/.config/zed/snippets subdirectory. Language-agnostic snippets should reside in snippets.json file; you can also have snippets that apply only to a specific language, in which case the file should be named after that language (e.g. python.json).
I'd recommend checking out friendly-snippets for starters.
Under the hood we use our own fork of simple-completion-language-server.
I have added the zed/snippets/snippets.json - and when I restarted Zed the snippet is showing with typing the prefix in a hovering selector (awesome!)- but when I press enter or click the option it is not populating into the buffer - is there a specific command for populating the snippet from the options?
Can you share your snippets file? It should work just like normal completions, so you shouldn't have to do anything special.
Can you share your snippets file? It should work just like normal completions, so you shouldn't have to do anything special.
{
"getVars": {
"prefix": "var",
"body": "${getVars({ name : '' })}"
}
}
I'm not sure if that's a valid syntax; does it work for you in other editors, e.g. VSC?
Edit: I've checked it myself - it is inserted verbatim in VSC.
I think you might want to escape the leading $
I think it is bad syntax on my part (${} is used for arguments for snippets it looks like)- thanks!
@osiewicz Thanks for the work! I tested and it works perfectly for Python and Markdown but Dockerfile. Is the snippet file name dockerfile.json correct for Dockerfile ?
Hey @failable, Currently we can only launch a language server for a known subset of languages (we don't have an ability to launch a language server for any file); we've simply missed Dockerfile in a list. I'll try to figure out if we could lift that limitation somehow by the next weeks release; I realize the current approach is not really scalable.
I copied my snippets over from VS code and they did not show up at first either. But then I validated the json of the file and it had errors. When I had fixed those errors and I restarted Zed, the snippets appeared.
Thanks for adding this feature.
How do we trigger snippets? For instance, here’s one I have:
{ "Horizontal Rule": { "prefix": "---", "body": ["- - - "], "description": "Add horizontal rule" } }
I've put the following code in ~/.config/zed/snippets/markdown.json, but it doesn't work in 123.md file
{
"Create new md file": {
"prefix": "md",
"body": [
"+++",
"title = '$1'",
"categories = ['LearnOn${2:line}']",
"tags = ['${3:go}']",
"date = 2024-$4-$5",
"draft = true",
"+++"
],
"description": "Create new md file"
}
}
@fuzenco It seems the current implementation only works if the prefix is provided in the completion menu while prefixs like --- are not provided in the menu.
@osiewicz It would also nice to have customizable key binding to trigger snippets manually.
@fuzenco It seems the current implementation only works if the
prefixis provided in the completion menu whileprefixs like---are not provided in the menu.
@failable thanks but where can I find the completion menu?
@fuzenco It seems the current implementation only works if the
prefixis provided in the completion menu whileprefixs like---are not provided in the menu.@failable thanks but where can I find the completion menu?
@failable Disregard. I’d been working on newly opened files which I didn’t have language formatting applied to. Once I saved my file as .md, the snippet appeared. Works very nicely too. Great job Zed team!
UPDATE: '---' does work to invoke the snippet. Again, my problem was not correctly setting the document type.
I'm encountering an issue with Zed snippets in LaTeX files. The same snippet works fine in Markdown but fails to trigger when writing LaTeX files. Here's an example: I've added the following snippet to both markdown.json and latex.json:
{ "Horizontal Rule": { "prefix": "---", "body": ["- - - "], "description": "Add horizontal rule" } }
When writing Markdown files, the snippet triggers as expected. However, when writing LaTeX files, no snippet is triggered at all. Has anyone else experienced this issue or know how to resolve it? Any help would be appreciated.
I had to restart Zed before the snippet in the added markdown.json file would work…
I had to restart Zed before the snippet in the added
markdown.jsonfile would work…
this works for mardown, but not for latex for me. also i am using the latex extension, maybe there is some clash?
@failable there's a editor: show completions action.
@ivansigmund see https://github.com/zed-industries/zed/issues/4611#issuecomment-2179009910, LaTeX has to be added to the list manually for now.
@osiewicz Hello! I mean trigger expansion directly when prefix is inserted instead of showing the completion menu.
@failable there's a
editor: show completionsaction. @ivansigmund see #4611 (comment), LaTeX has to be added to the list manually for now.
thanks!
Issue: Backslashes are removed in snippet expansion
When using the following snippet in Zed:
{
"equation": {
"prefix": "BEQ",
"body": "\\begin{equation}\n\t$0\n\\end{equation}",
"description": "equation environment"
}
}
Expected behavior: Typing "BEQ" should expand to:
\begin{equation}
\end{equation}
Actual behavior: Typing "BEQ" expands to:
begin{equation}
end{equation}
The backslashes are missing in the expanded snippet. Steps to reproduce:
Add the above snippet to Zed In a new file, type "BEQ" and trigger snippet expansion
Possible cause: The backslashes might be getting escaped or removed during snippet processing.
@osiewicz Thank you for this! Some things I think would be really useful:
- The docs should mention the required nomenclature somewhere:
Hey @failable, Currently we can only launch a language server for a known subset of languages (we don't have an ability to launch a language server for any file); we've simply missed Dockerfile in a list. I'll try to figure out if we could lift that limitation somehow by the next weeks release; I realize the current approach is not really scalable.
Took me like 30 minutes of fumbling around locally/searching the web to find your comment and discover how to properly set things up (.tsx snippets needing to go in typescriptreact.json in particular).
-
Would be great for QoL if servers for superset languages automatically included the snippets of subset languages — for example, I'd expect my
snippets/javascript.jsonfile to apply to.tsfiles, instead of having to duplicate everything into mysnippets/typescript.jsonfile. Likewise withjs→tsxandts→tsx. -
Noticed that comments do invalidate JSON here. Not always the case these days so maybe worth calling out
Is it possible to use variables like in VSCode, like TM_FILENAME_BASE? My attempts failed.