Information about generated files when creating python files in VS code

Article directory


foreword

For the convenience of later maintenance projects, we usually add some relevant instructions about this code file at the top of all codes, which may include file name, author, email address, time, etc. VS Code is a very powerful code editor, we can use custom code snippets to realize the function of adding title description. The following uses the python file as an example to show the specific method.


Solution

1. Open VS Code, click 文件首选项配置用户代码片段. A dialog box will pop up, and you can see the configuration of various languages, as shown below, we pull down and click python.
insert image description here
2. You can see the description in the default python.json, the Chinese meaning is as follows,

Put your Python code snippets here. Each snippet has a name, a prefix, a body, and a description.
The prefix is ​​used to trigger code snippets where the code body will be expanded and inserted. Possible variables are:
$1, $2 for tab stop, 0 for final cursor position, 0 for final cursor position,0 means final cursor position, {1:label}, ${2:another} means placeholder. Placeholders with the same ID are concatenated together.
Example:
“Print to console”: { “prefix”: “log”,“body”: [“console.log('$1');”,“$2”],“description”: “Log output to console”}






insert image description here
3. We delete all these contents and replace them with the following code. titleAccording to the above translation, combined with the following code, we can type in to activate the default title description we set when creating a file .
Note: When adding the following code, remember to modify the part behind @Author and @Email to your own corresponding content

{
    
    
    "Add python title Header": {
    
    
        "prefix": "title",
        "body": [
            "#!/usr/bin/env python",
            "# -*- coding: utf-8 -*-",
            "\"\"\"",
            "# @FileName      : ${TM_FILENAME_BASE}",
            "# @Time          : ${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE} ${CURRENT_HOUR}:${CURRENT_MINUTE}:${CURRENT_SECOND}",
            "# @Author        : pellykoo",
            "# @Email         : [email protected]",
            "# @description   :",
            "\"\"\""
        ],
        "description": "Add title to Python File"
    }
    
}

4. Close the file after setting, click 文件新建文件to select Python File. Press the key Ctrl+ Sto save the newly created file, and give it a name, here we name it new.py. Enter to confirm.
insert image description here
5. Enter at the beginning titleand press Tabto complete.
insert image description here

Summarize

Students who are good at drawing inferences may see that code snippets are a very practical function. This article is just to realize the function of adding titles. In fact, we can also use some commonly used code blocks and some functions that have been improved. Add directly to this python.json file, other languages ​​are similar, just replace the python in step 1 with java, c, cpp. This can help us improve the efficiency of code writing and avoid duplication of work. VS Code provides a convenient interface that allows us to easily define and use custom code snippets.

Guess you like

Origin blog.csdn.net/PellyKoo/article/details/129831510