search
HomeDevelopment ToolsVSCodeLet's talk about the use of Snippet plug-in in VSCode (to improve programming efficiency)

本文介绍的是 VSCode Snippet 插件,一种代码片段配置插件,可以节省你写样板代码的时间。

Let's talk about the use of Snippet plug-in in VSCode (to improve programming efficiency)

你可能之前学习过 VSCode Snippet 插件,知道它的作用也学习了配置的语法,但是你想不到落地使用的场景。本文也将解决你的这个疑惑~

本文将介绍 VSCode Snippet 的使用介绍、配置语法、引入方式和使用场景。【推荐学习:vscode教程编程教学

VSCode Snippet 插件使用简介

Lets talk about the use of Snippet plug-in in VSCode (to improve programming efficiency)

(图片来自于 VSCode 官网)

如 gif 图所示,当你想编写一段 ajax 代码时,只需要在编辑器里输入 ajax,再点击下 Tab 键,就会为你自动生成 ajax 代码的模板。

Snippet 支持我们配置自己想要的任意代码片段,因此我们可以把项目中常见的代码片段抽出来,提高编程效率。同时可以共享给所在的团队,提高整个团队的编程效率。

Snippet 配置语法

我们拿 antd 组件库举例,比如我们想配置 antd 的 Snippet 代码片段:

Antd Selet 组件代码如下:

<Select
  defaultValue="lucy"
  style={{ width: 100 }}
  onChange={handleChange}
  options={[
    {
      value: &#39;jack&#39;,
      label: &#39;Jack&#39;,
    },
    {
      value: &#39;lucy&#39;,
      label: &#39;Lucy&#39;,
    }
  ]}
/>

对应的 Snippet 配置代码如下:

{
    "antd/Select": {
        "prefix": ["Select"],
        "body": [
            "<Select",
            "    defaultValue={$1}",
            "    style={{ width: ${2|100,200,300,400|} }}",
            "    onChange={${3:handleChange}}",
            "    options={[",
            "        {",
            "            value: $4,",
            "            label: $5,",
            "        }",
            "    ]}",
            "/>"
        ],
        "description": "Antd Select UI 组件"
    }
}

字段含义:

  • prefix 是触发 snippets 的前缀,可以通过数组指定多个

  • body 是填入到编辑器的内容

  • description 是 snippets 的描述

body 部分可以通过 ${} 的方式指定光标位置、顺序、占位字符串、可用的值等

${} 语法简介:

  • 光标跳转:112:

  • 占位符:${1: placeholder}

  • 可选值:${1|text1,text2,text3|}

  • 变量:$变量名

    在模版可编辑位置填入内容的时候,有的时候需要用到选中的值、剪贴板的值、文件名、日期等,这些信息通过 snippets 中支持的变量来取。

    比如:*   TM_FILENAME: 文件名*   TM_CURRENT_LINE: 当前行的内容*   CLIPBOARD: 剪贴板内容*   WORKSPACE_NAME:workspace 的名字*   WORKSPACE_PATH:workspace 的路径*   CURRENT_YEAR:当前年*   CURRENT_MONTH:当前月*   CURRENT_DATE:当前日*   RANDOM: 随机数*   RANDOM_HEX: 6 位随机 16 进制数*   UUID: 唯一 id

    可以取这些变量的值来填入到光标位置,方式就是使用 TM_FILENAMETM\_FILENAME、CURRENT_YEAR 的方式。

Snippets 引入方式:

  • 方式一:直接在项目中生成 .vscode/xxx.code-snippets 文件,格式为本文描述的 JSON 格式,语法如上述介绍。操作方式:

  • 方式二:发布 VS Code 插件:

Sinppets plug-in usage scenarios:

  • #Use some open source component libraries to find out whether the Snippet plug-in already exists, such as antd's snippet plug-in :github.com/bang88/antd…

  • If your project uses an open source framework/component library, this open source project does not have a matching Snippets plug-in , then you who have learned to encapsulate the Snippets plug-in, quickly start using your hard-working little hands, it is time to make some contributions to your team or community~

  • If your project is based on UI component libraries such as antd also encapsulate some public components of their own business, so you can encapsulate a Snippets plug-in for these business-related public components to improve the efficiency of your team

  • In addition to JSX Class components, some commonly used tool functions can also abstract Snippets plug-in code snippets

For more related knowledge about VSCode, please visit: vscode Basic Tutorial!

The above is the detailed content of Let's talk about the use of Snippet plug-in in VSCode (to improve programming efficiency). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete
Visual Studio and VS Code: Understanding Their Key DifferencesVisual Studio and VS Code: Understanding Their Key DifferencesApr 19, 2025 am 12:16 AM

VisualStudio is suitable for large-scale projects and enterprise-level application development, while VSCode is suitable for rapid development and multilingual support. 1. VisualStudio provides a comprehensive IDE environment and supports Microsoft technology stack. 2.VSCode is a lightweight editor that emphasizes flexibility and scalability, and supports cross-platform.

Is Visual Studio Still Free? Understanding the AvailabilityIs Visual Studio Still Free? Understanding the AvailabilityApr 18, 2025 am 12:05 AM

Yes, some versions of VisualStudio are free. Specifically, VisualStudioCommunityEdition is free for individual developers, open source projects, academic research, and small organizations. However, there are also paid versions such as VisualStudioProfessional and Enterprise, suitable for large teams and enterprises, providing additional features.

Using Visual Studio: Developing Software Across PlatformsUsing Visual Studio: Developing Software Across PlatformsApr 17, 2025 am 12:13 AM

Cross-platform development with VisualStudio is feasible, and by supporting frameworks like .NETCore and Xamarin, developers can write code at once and run on multiple operating systems. 1) Create .NETCore projects and use their cross-platform capabilities, 2) Use Xamarin for mobile application development, 3) Use asynchronous programming and code reuse to optimize performance to ensure efficient operation and maintainability of applications.

How to format json with vscodeHow to format json with vscodeApr 16, 2025 am 07:54 AM

The ways to format JSON in VS Code are: 1. Use shortcut keys (Windows/Linux: Ctrl Shift I; macOS: Cmd Shift I); 2. Go through the menu ("Edit" > "Format Document"); 3. Install JSON formatter extensions (such as Prettier); 4. Format manually (use shortcut keys to indent/extract blocks or add braces and semicolons); 5. Use external tools (such as JSONLint and JSON Formatter).

How to compile vscodeHow to compile vscodeApr 16, 2025 am 07:51 AM

Compiling code in VSCode is divided into 5 steps: Install the C extension; create the "main.cpp" file in the project folder; configure the compiler (such as MinGW); compile the code with the shortcut key ("Ctrl Shift B") or the "Build" button; run the compiled program with the shortcut key ("F5") or the "Run" button.

How to install vscodeHow to install vscodeApr 16, 2025 am 07:48 AM

To install Visual Studio Code, please follow the following steps: Visit the official website https://code.visualstudio.com/; download the installer according to the operating system; run the installer; accept the license agreement and select the installation path; VSCode will start automatically after the installation is completed.

How to enlarge fonts with vscodeHow to enlarge fonts with vscodeApr 16, 2025 am 07:45 AM

The methods to enlarge fonts in Visual Studio Code are: open the settings panel (Ctrl, or Cmd,). Search and adjust "Font Size". Choose "Font Family" with the right size. Install or select a theme that provides the right size. Use keyboard shortcuts (Ctrl or Cmd) to enlarge the font.

How to connect to a remote server with vscodeHow to connect to a remote server with vscodeApr 16, 2025 am 07:42 AM

How to connect to a remote server through VSCode? Install Remote - SSH Extended Configuration SSH Create a Connection in VSCode Enter connection information: Host, Username, Port, SSH Key Double-click the saved connection in Remote Explorer

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.