首页 > 解决方案 > 在 Visual Studio Code 中创建文件时自动添加代码片段

问题描述

有没有办法在基于文件类型/扩展名创建文件时将代码片段添加到文件中。我想在创建新的 C++ 头文件时添加头保护。例子:

#ifndef DIRECTORY_FILENAME_H
#define DIRECTORY_FILENAME_H

#endif // DIRECTORY_FILENAME_H

我已经创建了一个添加此文本的代码片段,但我不知道如何在创建文件时自动添加它。

标签: visual-studio-code

解决方案


https://marketplace.visualstudio.com/items?itemName=Gruntfuggly.auto-snippet

例子:

autoSnippet.snippets: [
    { "pattern": "ut-*.cpp", "snippet": "ut-template",
    { "pattern": "*.h", "snippet": "header-template" },
    { "pattern": "*.cpp", "snippet": "body-template",
    { "language": "javascript", "snippet": "template", "commands":"[ editor.action.commentLine" ] }
]

推荐阅读