Some common configuration operations in VS CODE (shortcut key settings, C/C++ debug, code path configuration)

Overview

Let's have a simple operation article today, VSCODE is a software we often use, and I have written about it before VSCODE telecommuting Some of the operations (friends who are interested can click to see it), today I will briefly introduce some operation shortcut settings, C/C++ debug, code path configuration that I use in other places, and I will share it with you. .

Author: Conscience remains

Reprint authorization and onlookers: Welcome to add WeChat public account: Conscience_Remains

 

1 Setting of shortcut keys


First of all, vscode comes with a lot of convenient shortcut keys:

· Comment: [ctrl+k,ctrl+c] or ctrl+/

· Move line: alt+up/down

· Show/hide the left directory bar ctrl + b

· Copy the current line: shift + alt +up/down

· Delete the current line: shift + ctrl + k

· Console terminal display and hide: ctrl + ~

· Find file/install vs code plugin address: ctrl + p

· Code formatting: shift + alt +f

· Create a new window: ctrl + shift + n

· Increase indentation of lines: ctrl + [

· Decrease indentation of lines: ctrl + ]

· Trim trailing spaces (remove unused spaces at the end of a line): ctrl + shift + x

· Font zoom in/out: ctrl + ( + or - )

· Split editor: ctrl + 1/2/3

· Switch window: ctrl + shift + left/right

· Close the editor window: ctrl + w

· Switch to full screen: F11

· Word wrap: alt + z

· Show git: ctrl + shift + g

· Global search file: ctrl + shift + f

· Commands to display related plugins (eg: git log): ctrl + shift + p

· Folding code: ctrl + k + 0-9 (0 is fully folded)

· Expand the code: ctrl + k + j (to fully expand the code)

·Selected text: shift + left / right / up / down

· Format selected code: ctrl + k / ctrl +f

· Add content (cursor) to multiple lines at the same time: ctrl + alt + up/down

· Global replacement: ctrl + shift + h

· Open recently opened file: ctrl + r

 

In addition, we will also customize some shortcut key settings that we are used to:


First select the icon of the gear in the lower right corner of VSCODE, right-click and select Keyboard Shortcuts

 

 

After clicking to enter, you can see a lot of options. At this time, we can search and click to set the shortcut keys of our personal preferences.

Here I first select the Go Back I need to set it up. After entering, we can directly enter the key combination. If it is repeated with other shortcut keys, vscode will prompt below. Just exit after setting.

 

 

2 c/c++ code debug debugging

 

I declare in advance that the following is the debug debugging configuration of vscode's c/c++ in win10 environment:

It's actually two steps:

1. Now the GCC compiler, and then configure it into the environment variable

2. Configure the vscode file to enter the compilation

 

First download and use the MinGW-W64 GCC-8.1.0 compiler, because the direct download will fail, so it is recommended that you use the offline download method to download.

Offline download address: https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/

 

After I entered the website, I pulled it to the bottom and selected an offline package that I am used to to download.

After downloading, you can directly decompress it and use it, no installation is required, as shown below

 

Configure the environment variables under the window, and search for environment variables directly by win+q

 

 

Select Edit Environment Variables

Select Path to edit

 

Add the decompressed mingw64 directory, this needs to be added according to the decompressed directory

 

At this time, use win+r or powershell to enter gcc -v or g++ -v to see if the installation is successful

 

 

Now configure vscode

First of all, we create a new file for testing, among which we need to pay attention to creating three new files for debugging. You can add the following json file directly to your file, c_cpp_properties.json needs special attention, the C library file inside The directory is my own, you can configure it yourself

 

 

c_cpp_properties.json: c and cpp options. It is not necessary to touch it. If the header file in the system environment cannot be found, it is likely that there is a problem here, and you can add it in "includePath".

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}",
                "D:/Program Files (x86)/GNU Tools ARM Embedded/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/",
                "D:/Program Files (x86)/GNU Tools ARM Embedded/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                "D:/Program Files (x86)/GNU Tools ARM Embedded/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32/",
                "D:/Program Files (x86)/GNU Tools ARM Embedded/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/tr1/",
                "D:/Program Files (x86)/GNU Tools ARM Embedded/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                "D:/Program Files (x86)/GNU Tools ARM Embedded/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed/",
                "D:/Program Files (x86)/GNU Tools ARM Embedded/mingw64/x86_64-w64-mingw32/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "intelliSenseMode": "msvc-x64",
            "cStandard": "gnu11",
            "cppStandard": "c++11"
        }
    ],
    "version": 4
}

  

launch.json: Debug related options. Select the debugger and the file to be debugged for debugging.
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C/C++",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "gdb.exe",
            "preLaunchTask": "compile",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
        },
    ]
}

  

tasks.json: compilation instructions. This can only execute the simplest compilation instructions, if necessaryc languagethat isgccput the followingcommanditem byg++change togcc

If it is multi-file compilation (that is, the function declaration is separated from the function definition, don't change it if you don't understand it), you need to change the "${file}" item in the args list to "${workspaceFolder}/*.cpp" , multi-file cmake is recommended

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "compile",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

  

 

Then install a basic C/C++ plug-in, so you can debug

 

 

Now we can directly debug with F5. Remember to break the point before debugging.

 

I used a simple C++ code for debugging. The debugging interface is as follows. The block diagram on the left has watch and call stack, and then you can see the buttons used for debugging.

 

 

 

 

3 Code path settings

 

I once encountered such a problem. When I edited the code in vscode, I could not find the basic header files in the standard library of C, and it always prompted a problem when the code was completed. After looking for the problem, I found that my path setting was wrong.

 

1. First click on the light bulb next to the wavy line and choose to edit the "includePath" setting

 

 

2. After entering, select the c_cpp_properties.json file

3. Then add our path to includePath

 

 

The program path that matches my other correct path settings

After I add my path, it can be displayed normally, everyone should pay attention to the slash direction of the directory

 

 

 

This is some configuration operations of vscode that I shared. If you have any better ideas, please share and exchange.

 

 

For more sharing, scan the code to follow me

Posted by louisA2A on Sun, 15 May 2022 21:35:07 +0300