Several basic configurations of vscode

Introduction to vscode

What is vscode

Simply put, vscode is a text editor, not an ide . This means that vscode only provides an editing environment and not a compilation environment. If you want to use vscode to integrate the development environment, you must go through the following steps:

  • Install the necessary compiler (such as mingw, python, texlive, etc.)
  • Configure the corresponding compilation environment (add environment variables, etc.)
  • Install matching vscode extension plugin
  • Link the external compiler to vscode by extending the "attributes" provided by the plugin

At the same time, a text editor will definitely not provide the function of running programs, and the same is true for vscode. To run the program, there are several ways:

  • Some extension plug-ins of vscode will provide configured terminals and commands to run programs, which we can use directly;
  • There is an extension plug-in code-runner specializing in running programs in vscode, which can be used after installation;
  • Vscode provides interfaces to several types of terminals. We can use the terminal in Vscode like a small black box with cmd, so we can run the program in the Vscode terminal;

In this way, we have a general understanding of how to use vscode to compile and run programs.

vscode configuration

First of all, we must make it clear that the smallest unit of vscode configuration is the folder, that is, we can use the open folder function. We can link different folders to different external compilers, implement different shortcut tasks, and quickly debug and detailed settings. Expand the function of the plug-in and so on.

Therefore, let's take a quick look at the configuration structure of vscode, as shown in the figure below: As
Insert picture description here
you can see,

  • There are global settings settings.json under vscode; after that is the workspace, and the workspace settings settings.json, a workspace contains multiple folders;
  • Each folder has the same .vscode folder. The three files in this folder are settings.json, tasks.json, and launch.json , which are the core of the configuration.
  • Each folder can be configured to compile and run files in different languages: c++, python and html, and can be directly accessed through the same workspace. This is the integration and convenience of vscode.

Now, let's say one by one:

File structure

vscode provides a three-level file structure, from workspace to folder to single file:

  • The workspace is a "collection" of folders provided by vscode. A workspace can contain multiple folders, just as a folder can contain multiple files.
  • The folder is the smallest unit of work for vscode configuration, and vscode cannot provide configuration for a single file.
  • In different workspaces, we can choose to enable/disable different extension plug-ins to save memory

settings.json

In this json file, we can set various properties of the built-in vscode or extended plug-ins through key-value pairs , including the address of the external compiler, various compilation preferences, and so on. At the same time, vscode provides nested settings,
Insert picture description here

  • From high to low, they are global settings, workspace settings, and folder settings; the global settings are opened by ctrl+shift+P and then input settings.json by default.
  • The properties we set in the folder settings will override the workspace settings and global settings, and vice versa .
  • Therefore, for global settings, such as editor fonts, etc., we set them in user settings.json. The workspace will not be described for the time being , and there are different settings for each folder, such as want to be in a different folder To use different python environments, we choose to set the corresponding content in the folder settings (that is, the settings.json inside the folder).

tasks.json

  • Many times, like when using a linux system, we will customize some small scripts to facilitate some functions. vscode uses tasks to support quick realization of some convenient functions.
  • Some extension plug-ins will have encapsulated tasks that can be executed directly, and we can also customize various tasks, such as "compile current files", "delete redundant files" and so on.
  • Tasks are more convenient than directly defining .bat files in that vscode provides many ways to quickly access specific paths, such as the path of the current folder, the path of the folder opened by vscode, the path of the current file, and the current file without extension Name and so on.
  • The tasks defined in tasks.json can only be used in the current folder (the workspace containing the folder).

launch.json

This content is mainly used to provide support for debugging. **Different templates will be provided for extension plug-ins in different languages. **Press f5 to select a template and generate a file.

Advantages of vscode

  • good looking
  • Strong integration
  • It is very convenient after configuration, you can add functions at any time
  • Expansion is extremely rich

Configuration example

After understanding the configuration principle of vscode, I will give some of my own configuration below for reference.

settings.json

Global configuration

{
    
    
	//vscode的默认终端,此处设置为cmd
    	"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe",
    	//拖拽移动文件时不要确认提示
    	"explorer.confirmDragAndDrop": false,
    	//手动升级vscode
    	"update.mode": "manual",
    	//自动保存,此处设置为永远自动保存
    	"files.autoSave": "afterDelay",
    	//task的下拉列表中显示历史常用的个数 
    	"task.quickOpen.history": 0,
}

To python

{
    
    	
	//控制是否激活pylint
    	//"python.linting.pylintEnabled": true,
    	"python.linting.enabled": true,
    	//如果使用pylint可能会导致奇怪的报错,加上这几句可以取消
    	//"python.linting.pylintArgs": [
    	//    "--generate-members"
    	//],
    	//是否在自动补全的函数后自动添加括号
    	"python.autoComplete.addBrackets": false,
    	//是否使用python插件内部的自动补全功能
    	"python.jediEnabled": true,
    	//python环境的路径
    	"python.pythonPath": "C:\\apps\\Anaconda\\python.exe",
    	//python自动补全的搜索路径
    	"python.autoComplete.extraPaths": [
    	    "C:/apps/Anaconda/Lib/site-packages/",
    	    "C:/apps/Anaconda/Scripts/"
         ],
}

For c, c++

{
    
    
    "files.associations": {
    
    
        "*.tcc": "cpp",
        "array": "cpp",
        "atomic": "cpp",
        "cctype": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "deque": "cpp",
        "unordered_map": "cpp",
        "vector": "cpp",
        "exception": "cpp",
        "algorithm": "cpp",
        "functional": "cpp",
        "iterator": "cpp",
        "memory": "cpp",
        "memory_resource": "cpp",
        "numeric": "cpp",
        "optional": "cpp",
        "random": "cpp",
        "string": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "utility": "cpp",
        "fstream": "cpp",
        "initializer_list": "cpp",
        "iosfwd": "cpp",
        "iostream": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "new": "cpp",
        "ostream": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "typeinfo": "cpp"
    }
}

Pair latex

{
    
    
	//编译方式
	"latex-workshop.latex.tools": [
          {
    
    
            "name": "latexmk",
            "command": "latexmk",
            "args": [
            "-synctex=1",
            "-interaction=nonstopmode",
            "-file-line-error",
            "-pdf",
            "%DOC%"
            ]
          },
          {
    
    
            "name": "xelatex",
            "command": "xelatex",
            "args": [
            "-synctex=1",
            "-interaction=nonstopmode",
            "-file-line-error",
            "%DOC%"
              ]
          },          
          {
    
    
            "name": "pdflatex",
            "command": "pdflatex",
            "args": [
            "-synctex=1",
            "-interaction=nonstopmode",
            "-file-line-error",
            "%DOC%"
            ]
          },
          {
    
    
            "name": "bibtex",
            "command": "bibtex",
            "args": [
            "%DOCFILE%"
            ]
          }
        ],
	"latex-workshop.latex.recipes": [
        {
    
    
            "name": "xelatex",
            "tools": [
            "xelatex"
            ]
                  },
            {
    
    
            "name": "latexmk",
            "tools": [
            "latexmk"
            ]
          },
          {
    
    
            "name": "pdflatex -> bibtex -> pdflatex*2",
            "tools": [
            "pdflatex",
            "bibtex",
            "pdflatex",
            "pdflatex"
            ]
          }
        ],
        //需要清除文件的后缀
        "latex-workshop.latex.clean.fileTypes": [
        "*.aux",
        "*.bbl",
        "*.blg",
        "*.idx",
        "*.ind",
        "*.lof",
        "*.lot",
        "*.out",
        "*.toc",
        "*.acn",
        "*.acr",
        "*.alg",
        "*.glg",
        "*.glo",
        "*.gls",
        "*.ist",
        "*.fls",
        "*.log",
        "*.fdb_latexmk",
        "*.gz"
      ],
      //不要显示错误弹窗
      "latex-workshop.message.error.show": false,
      //不要显示信息弹窗
      "latex-workshop.message.information.show": false,
      //不要显示警报弹窗
      "latex-workshop.message.warning.show": false,
      //保存时不要自动编译
      "latex-workshop.latex.autoBuild.run": "never",
      //默认在右边tab预览
      "latex-workshop.view.pdf.viewer": "tab",
}        

tasks.json

For C/C++

{
    
    
    "version": "2.0.0",
    "command": "g++",
    "args": ["-g","${file}","-o","${fileBasenameNoExtension}.exe"],    // 编译命令参数
    "problemMatcher": {
    
    
        "owner": "cpp",
        "fileLocation": ["relative", "\\"],
        "pattern": {
    
    
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    }
}

Compile the .asy file after drawing with asymptote, delete the extra files, and move the result to a specific folder

{
    
    
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        //编译
        {
    
    
            //在task列表中显示的文字
            "label": "Generate EPS",
            //运行环境
            "type": "shell",
            //命令
            "command": "asy",
            //参数
            "args": [
                "-f",
                "eps",
                //$开头的都是vscode提供的封装好的变量,具体内容
                //可以自己尝试
                "${file}"
            ],
            //所述属组
            "group": "build",
            //报错处理
            "problemMatcher": []
        },
        //删除文件
        {
    
    
            "label": "Delete FILE",
            "type": "shell",
            "command": "del",
            "args": [
                "*.aux",
                "*.log",
                "*.dvi",
                "*.pdf"
            ],
            //设置为none的task不会在build task的下拉列表中展示
            "group": "none",
            "problemMatcher": []
        },
        //将生成的eps移动至相应文件夹
        {
    
    
            "label": "Move EPS",
            "type": "shell",
            "command": "move",
            "args": [
                "/Y",
                //$开头的都是vscode提供的封装好的变量,具体内容
                //可以自己尝试
                "${workspaceFolder}\\${fileBasenameNoExtension}.eps",
                "C:\\Pt_Latex\\Reources\\"
            ],
            "group": "build",
            //在运行这个任务之前,需要提前运行的任务
            "dependsOn": [
                "Delete FILE"
            ],
            "problemMatcher": []
        }
    ]
}

launch.json

For C/C++

{
    
      
    "version": "0.2.0",  
    "configurations": [  
        
        {
    
      
            "name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示  
            "type": "cppdbg",       // 配置类型,这里只能为cppdbg  
            "request": "launch",    // 请求配置类型,可以为launch(启动)或attach(附加)  
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",// 将要进行调试的程序的路径  
            "args": [],             // 程序调试时传递给程序的命令行参数,一般设为空即可  
            "stopAtEntry": false,   // 设为true时程序将暂停在程序入口处,一般设置为false  
            "cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录  
            "environment": [],  
            "externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台  
            "MIMode": "gdb",  
            "miDebuggerPath": "C:\\apps\\MinGW\\bin\\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应  
            "preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc  
            "setupCommands": [  
            	{
    
       
      		"description": "Enable pretty-printing for gdb",  
                    "text": "-enable-pretty-printing",  
                    "ignoreFailures": true  
            	}  
            ]  
        }  
    ]  
}

For python (the default is just fine)

{
    
    
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        
        {
    
    
            "name": "Python: 当前文件",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            //是否在程序开始处停止
            "stopOnEntry": false
        }
    ]
}

Guess you like

Origin blog.csdn.net/namespace_Pt/article/details/105932818
Recommended