如何在vscode中添加与语言无关的代码段? [英] How can I add language-agnostic snippets in vscode?

查看:162
本文介绍了如何在vscode中添加与语言无关的代码段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个示例,我想添加 lorem ipsum作为摘要,因此我可以快速将其插入所需的位置。我不希望它与特定语言绑定,因为我可能会在HTML,Jade甚至JS / TS等多个地方使用它。

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.

片段的 global.json 还是其他任何方式?

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

推荐答案

全局代码段已在2018年2月10日的v1.10中添加,请参见全局代码段

Global snippets were added in v1.10 Feb., 2018 see global snippets


VS代码现在支持全局代码段,这意味着这些代码段的范围不限于一种语言,但可以定位任何类型的文件。使用首选项:配置用户代码段命令,选择新建全局代码段文件...选项,这将为新代码段打开.code-snippets文件。使用scope属性可以列出代码片段所针对的语言。例如,下面的代码片段可以为JavaScript和TypeScript文件添加版权标题:

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';",
    "",
    "$0"
  ],
  "description": "Insert Copyright Statement"
}

这篇关于如何在vscode中添加与语言无关的代码段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆