


Let's talk about how to customize code snippets in vscode to make coding so fast!
vscode中怎么定制代码片段?下面本篇文章给大家介绍一下给VSCode中定制属于自己的代码片段,让编码快到飞起的方法,希望对大家有所帮助!
vscode的用户片段非常的方便,比如我想规范注释、快速生成代码呀,vscode的用户片段都可以帮我实现,而且是那种非常定制化去实现。定义好片段后,你还可以通过脚手架去生成一个vscode插件,并不复杂。【推荐学习:《vscode入门教程》】
基本指南
先找到文件的菜单 -> 找到首选项 -> 用户片段 -> 选择新建 xx 文件夹
的代码片段文件 -> 输入片段名称 -> 开始定制。
这个 xx 文件夹
就是你当前目录,当然你也可以新建全局的或者某一个文件类型的,都可以的。无非是片段文件存放的目录不同。
然后你就在当前目录下看到这个了。
只要把它放到当前vscode打开的文件夹的根目录下面,只要vscode能读取到这个文件.vscode
即可。
还有一种方式就是你把它放到vscode的用户全局目录下也行,vscode会首先去那里面去找,然后再到当前目录下去找。
至于如何去找vscode的用户全局目录,你新建一个全局的代码片段,然后从vscode的面包屑导航中就能看到了。
开始定制自己的代码片段
目前我只先做一个通用的,可以在js、ts、scss、lesss中能用到的。
{ "单行注释:start": { "scope": "javascript,typescript,javascriptreact,typescriptreact,scss,less", "prefix": "ts", "body": [ "// PROJECT 一段自定义描述 start", "$0" ], "description": "单行注释:start" }, "单行注释:end": { "scope": "javascript,typescript,javascriptreact,typescriptreact,scss,less", "prefix": "te", "body": [ "// PROJECT 一段自定义描述 end", "$0" ], "description": "单行注释:end" }, "单行注释:start&end": { "scope": "javascript,typescript,javascriptreact,typescriptreact,scss,less", "prefix": "tse", "body": [ "// PROJECT 一段自定义描述 start", "", "${1:你即将填充的内容}", "", "// PROJECT 一段自定义描述 end", ], "description": "单行注释:start&end" }, "多行注释:start": { "scope": "javascript,typescript,javascriptreact,typescriptreact", "prefix": "ms", "body": [ "/**\r\n * PROJECT 一段自定义描述 start", " * $0", " * */", "" ], "description": "多行注释:start" }, "多行注释:end": { "scope": "javascript,typescript,javascriptreact,typescriptreact", "prefix": "me", "body": [ "/* PROJECT 一段自定义描述 end */", "" ], "description": "多行注释:end" } }
上面定制的是js、ts、jsx的注释代码块,不同的人有不同的风格,你简单改改成你自己的了。
使用说明:代码片段是需要通过 关键字 才能呼唤出来的。比如 如上的普通的单行注释就是通过 ts呼唤出来的,然后按一下补全的键,进行常规补全操作即可,就像windows系统的tab键。
代码片段的关键字就是上述配置文件中的 prefix 属性里的字符串,而scope就是它这个代码片段作用的文件类型了。
而body就是代码片段,数组中每一项都会在生成的代码中独占一行,$0
即表示生成代码片段后光标出现的位置。
还会有$1 2 3 4 5 6
等等,表示下一次光标出现的位置,也就是你连续敲击按tab键时光标出现的位置。
还可以使用让字符串作为占位符,比如单行注释::start&end
中的${1:xxx}
,这种就是用字符串当作占位符,可以给你很清楚的提示。
关键字说明:
ts:单行注释:star
te:单行注释:end
tse:单行注释:start&end
ms: 多行注释:start
me:多行注释:end
继续定制jsx和tsx的代码片段
这次先做一个jsx、tsx中能用的吧,jsx、tsx的注释判断和js、ts不太一样,所以要单独做一下。
{ "jsx&tsx注释:start": { "scope": "javascriptreact,typescriptreact", "prefix": "js", "body": [ "{/* PROJECT 一段自定义描述 start */}", "$0" ], "description": "jsx&tsx注释:start" }, "jsx&tsx注释:end": { "scope": "javascriptreact,typescriptreact", "prefix": "je", "body": [ "{/* PROJECT 一段自定义描述 end */}", "$0" ], "description": "jsx&tsx注释:end" }, "jsx&tsx注释:start&end": { "scope": "javascriptreact,typescriptreact", "prefix": "jse", "body": [ "{/* PROJECT 一段自定义描述 start */}", "", "${1:你即将填充的内容}", "", "{/* PROJECT 一段自定义描述 end */}", ], "description": "jsx&tsx注释:start&end" } }
效果如图:
关键字说明:
js:jsx&tsx注释:start
je:jsx&tsx注释:end
jse:jsx&tsx注释:start&end
再来一个css的
其实到了这里,你已经对vscode定制代码片段非常熟悉了,如果我上面写的片段不符合你的要求,你可以自己改改哟,没有什么复杂的操作。
{ "css注释:start": { "scope": "css", "prefix": "cs", "body": [ "/* PROJECT 一段自定义描述 start */", "$0" ], "description": "css注释:start" }, "css注释:end": { "scope": "css", "prefix": "ce", "body": [ "/* PROJECT 一段自定义描述 end */", "$0" ], "description": "css注释:end" }, "css注释:start&end": { "scope": "css", "prefix": "cse", "body": [ "/* PROJECT 一段自定义描述 start */", "", "${1:你即将填充的内容}", "", "/* PROJECT 一段自定义描述 end */", ], "description": "css注释:start&end" } }
关键字说明:
cs:css注释:start
ce:css注释:end
cse:css注释:start&end
最后做个todo吧
{ "TODO注释:common": { "scope": "javascript,typescript,javascriptreact,typescriptreact,scss,less", "prefix": "tt", "body": [ "// PRJECT-TODO:$0" ], "description": "TODO注释:common" }, "TODO注释:jsx": { "scope": "javascriptreact,typescriptreact", "prefix": "jt", "body": [ "{/* PRJECT-TODO:$0 */}" ], "description": "TODO注释:jsx" }, "TODO注释:css": { "scope": "css", "prefix": "ct", "body": [ "/* PRJECT-TODO:$0 */" ], "description": "TODO注释:css" }, }
关键字说明:
tt:TODO注释:common
jt:TODO注释:jsx
ct:TODO注释:css
Summary
After having these code snippets, you can actually make a vscode plug-in, which is relatively simple. This Nuggets article has written in great detail, as shown There's basically no problem doing it.
Summary description of keywords
#ts that can be used in js, ts, less, and scss files: Single-line comments: star
te: single-line comment: end
tse: single-line comment: start&end
ms: multi-line comment: start
me: multi-line comment: end
jsx, tsx files can be used
js: jsx&tsx comment: start
je: jsx&tsx comment: end
jse: jsx&tsx comment: start&end
#cs that can be used in the css file: css comment: start
ce: css comment: end
cse: css Comment: start&end
Todo
#tt that can be used in the above three files: TODO comment: common
jt: TODO comment: jsx
ct: TODO comment: css
For more related knowledge about VSCode, please visit: vscode tutorial! !
The above is the detailed content of Let's talk about how to customize code snippets in vscode to make coding so fast!. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

How to define header files using Visual Studio Code? Create a header file and declare symbols in the header file using the .h or .hpp suffix name (such as classes, functions, variables) Compile the program using the #include directive to include the header file in the source file. The header file will be included and the declared symbols are available.

VS Code To switch Chinese mode: Open the settings interface (Windows/Linux: Ctrl, macOS: Cmd,) Search for "Editor: Language" settings Select "Chinese" in the drop-down menu Save settings and restart VS Code

Common commands for VS Code terminals include: Clear the terminal screen (clear), list the current directory file (ls), change the current working directory (cd), print the current working directory path (pwd), create a new directory (mkdir), delete empty directory (rmdir), create a new file (touch) delete a file or directory (rm), copy a file or directory (cp), move or rename a file or directory (mv) display file content (cat) view file content and scroll (less) view file content only scroll down (more) display the first few lines of the file (head)

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

The main differences between VS Code and PyCharm are: 1. Extensibility: VS Code is highly scalable and has a rich plug-in market, while PyCharm has wider functions by default; 2. Price: VS Code is free and open source, and PyCharm is paid for professional version; 3. User interface: VS Code is modern and friendly, and PyCharm is more complex; 4. Code navigation: VS Code is suitable for small projects, and PyCharm is more suitable for large projects; 5. Debugging: VS Code is basic, and PyCharm is more powerful; 6. Code refactoring: VS Code is basic, and PyCharm is richer; 7. Code

There are two ways to set up a Chinese language in Visual Studio Code: 1. Install the Chinese language package; 2. Modify the "locale" settings in the configuration file. Make sure Visual Studio Code version is 1.17 or higher.

To enable and set VSCode, follow these steps: Install and start VSCode. Custom preferences including themes, fonts, spaces, and code formatting. Install extensions to enhance features such as plugins, themes, and tools. Create a project or open an existing project. Use IntelliSense to get code prompts and completions. Debug the code to step through the code, set breakpoints, and check variables. Connect the version control system to manage changes and commit code.
