如何在 vscode 中添加与语言无关的片段?

How can I add language-agnostic snippets in vscode?

例如,我想添加 "lorem ipsum" 作为一个片段,这样我就可以快速将它插入到我想要的地方。我不希望它与特定语言绑定,因为我可能会在多个地方使用它,例如 HTML、Jade,甚至 JS/TS.

是否有类似 global.json 的代码片段,或任何其他方法?

VS Code 没有全局范围 afaik。但是,我制作了一个名为 Snipster 的 npm 包,它改变了您编写代码片段的方式,并允许您通过使用 [=11] 命名代码片段来将全局范围发布到 所有 语言=] 扩展名。它也适用于 Atom 和 Sublime,而不仅仅是 VS Code。

例如: lorem.all

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ultrices nulla eget lectus pharetra dignissim. Curabitur molestie odio sed odio porttitor efficitur. Nulla vulputate porta quam, nec aliquam nisi gravida ac. Donec quis risus sed odio accumsan commodo id vel enim. Vivamus quam mauris, rutrum at vulputate quis, hendrerit eu velit.

2018 年 2 月 v1.10 中添加了全局代码段,请参见global snippets

VS Code now supports global snippets meaning snippets that aren't scoped to a single language but can target any kind of files. Using the Preferences: Configure User Snippets command, select the New Global Snippets file... option which will open a .code-snippets file for new snippets. Use the scope attribute to list the languages that are targeted by a snippet. For instance, the snippet below can add a copyright header for JavaScript and TypeScript files:

来自文档:

"JS & TS Stub": {
  "scope": "javascript,typescript",
  "prefix": "stub",
  "body": [
    "/*--------------------------------------------------------------",
    " *  Copyright (c) Your Corporation. All rights reserved.",
    " *  Licensed under the MIT License.",
    " *-------------------------------------------------------------*/",
    "",
    "'use strict';",
    "",
    "[=10=]"
  ],
  "description": "Insert Copyright Statement"
}