How can I add language-agnostic snippets in vscode?

≯℡__Kan透↙ 提交于 2019-12-24 07:49:26

问题


As an example I would like to add "lorem ipsum" as a snippet, so I can quickly insert it where I want. I don't want it do be tied to a specific language, as I might use it in multiple places like HTML, Jade, even JS/TS.

Is there like a global.json for snippets, or any other way to do this?


回答1:


Global snippets were added in v1.10 Feb., 2018 see 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:

From the documentation:

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



回答2:


VS Code doesn't have a global scope afaik. However, I made an npm package called Snipster that changes the way you write snippets and allows you to publish a global scope to all languages by naming the snippet with a .all extension. It also works with Atom and Sublime, not just VS Code.

For example: 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.


来源:https://stackoverflow.com/questions/40561853/how-can-i-add-language-agnostic-snippets-in-vscode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!