vscode的使用

vscode的使用

怎么自动格式化

  1. 首先在vscode中安装扩展C/C++,扩展程序将自动安装clang-format。
  2. 打开首选项设置(ctrl + ,),搜索format ,勾选format on save 自动保存。
  3. 打开首选项设置(ctrl + ,),搜索format ,勾选format on Type 。

怎么自动保存

  1. 打开首选项设置(ctrl + ,),搜索auto save ,选afterDelay

配置

常用的配置

自动格式化

vscode中我们可以通过设置来实现保存文件时自动格式化代码,具体步骤如下:

首先按Ctrl +,或者点击setting(右下解齿轮图标),输入`format)

设置

  • default Formatter :c/c++
  • Format on Paste
  • Format on Save
  • Format on Type

这些项目

  • 彩色括号 设置-> bracket pair colorization

常用的插件

  • c/c++ 一定要安装
  • chinese language 中文界面(最好不要安装,因为比赛的时候是英文界面)

运行

这里我们只通用ctrl+alt+t打开terminal,输入使用g++ -g -o 1 1.cpp来执行命令

第一次打开vscode,会自动下载一个c/c++ language components的东西

file -> new file -> select language ->选c++

写入下面的代码

#include <iostream>
int main(){
    std::cout << "hello";
    return 0;
}
  • [[ctrl]] + [[s]] 保存代码
  • 保存时,创建一个code文件夹,并打开
  • 命名为1.cpp,点保存

注意,如果你以后想要就在code目录下写代码,点击左侧边栏的open folder,选择刚才创建的 code目录,那么左侧样就会变成code目录的内容,这样以后写代码比较方便

3. 编译运行

  1. [[ctrl]] + [[alt]] + [[arrow down]] 打开一个新的桌面
  2. [[ctrl]] + [[alt]] + [[t]] 打开终端,输入编译命令

输入如下命令

# 进入code目录
cd code

# 手动编译

g++ -g -o 1 1.cpp

#运行
./1

快捷键

注释

  • 单行注释 [[ctrl]]+[[/]],如果再按一次,取消注释
  • 多行注释 [[ctrl]]+[[/]]

行操作

  • 向下/上移动当前行:[[alt]] + [[⬇] / [[⬆]
  • 选中当前行: [[ctrl]]+[[l]]
  • 删除当前行:[[shift ]]+ [[ctrl ]]+ [[k]]
  • 行增加缩进: [[ctrl ]]+ [[[]]
  • 行减少缩进: [[ctrl ]]+ [[]]]
  • 复制当前行到上一行:[[shift]]+ [[alt]] +[[up]]
  • 复制当前行到下一行:[[shift]]+ [[alt]] +[[down]]
  • 向下新起一行: [[ctrl]] + [[enter]]
  • 向上新起一行: [[ctrl]] + [[shift]] + [[enter]]

常用

  • 撤销 : [[ctrl]] + [[z]]
  • 反撤销 : [[ctrl]] + [[shift]] + [[z]]
  • 向下/下卷屏: [[ctrl]] + [[⬆]] / [[⬇]]
  • 向左/右跨越单词: [[ctrl]] + [[⬅]] / [[➡]]
  • 向下/上同时编辑多行(多行光标) : [[ctrl]] + [[shift]] + [[⬆]] / [[⬇]]

其它

  • 显示/隐藏左侧目录栏 [[ctrl]] + [[b]]
  • 控制台终端显示与隐藏:[[ctrl]]+ [[`]]
  • 切换全屏: F11
  • 格式化整个文件:[[ctrl]] + [[shift]] + [[i]]

手动修改快捷键

调试单个文件

参考:VS Code之C/C++程序的调试(Debug)功能简介

点击左侧的调试按钮

选LLVM/GDB

怎么保证的每次调试的时候,自动读取in数据文件呢

修改launch.jsonargs参数为

"args": [ "<", "in" ],

不想每次手动在main函数下断点,希望可以自动在main停止?

在 launch.json 文件中添加 stopAtEntry 字段,并将其设置为 true。这将在程序开始时立即停止在 main 函数。

{
    "version": "0.2.0",
    "configurations": [
        {
            ...
            "stopAtEntry": true,   // 将 stopAtEntry 设置为 true
            ...
        }
    ]
}

代码片段 snippets

代码片段(Code Snippets),指的是一些使用率很高的代码模板,可以是固定的内容(比如文件头的版权声明),或者是可以修改的预定义模板,比如for、while循环的模板。 通过Snippet,输入特定的关键词,就可以在代码段引擎的帮助下,生成预定义的模板代码,接着我们还可以通过在预定义的光标位置之间跳转,来修改补全模板,得到我们最终想要的代码。

下面是一个例子

{
	"for": {
		"prefix": "for",
		"body": [
			"for (${size_t} ${i} = ${1:0}; ${i} < ${2:length}; ${i}++)",
			"{",
			"	$3",
			"}"
		],
		"description": "Code snippet for 'for' loop"
	},
	"forr": {
		"prefix": "forr",
		"body": [
			"for (int ${i} = ${1:length} - 1; ${i} >= ${2:0}; ${i}--)",
			"{",
			"	$3",
			"}"
		],
		"description": "Code snippet for reverse 'for' loop"
	},
	"do": {
		"prefix": "do",
		"body": [
			"do",
			"{",
			"	$1",
			"} while($2);"
		],
		"description": "Code snippet for do...while loop"
	},
	"while": {
		"prefix": "while",
		"body": [
			"while ($1)",
			"{",
			"	$2",
			"}"
		],
		"description": "Code snippet for while loop"
	},
	"foreach": {
		"prefix": "foreach",
		"body": [
			"for(auto ${var} : ${collection_to_loop})",
			"{",
			"	$1",
			"}"
		],
		"description": "Code snippet for range-based for loop (c++11) statement"
	},
	"if": {
		"prefix": "if",
		"body": [
			"if ($1)",
			"{",
			"	$2",
			"}"
		],
		"description": "Code snippet for if statement"
	},
	"else": {
		"prefix": "else",
		"body": [
			"else",
			"{",
			"	$1",
			"}"
		],
		"description": "Code snippet for else statement"
	},
	"else if": {
		"prefix": "else if",
		"body": [
			"else if ($1)",
			"{",
			"	$2",
			"}"
		],
		"description": "Code snippet for else-if statement"
	},
	"enum": {
		"prefix": "enum",
		"body": [
			"enum ${MyEnum}",
			"{",
			"	$1",
			"};"
		],
		"description": "Code snippet for enum"
	},
	"enum class": {
		"prefix": "enum class",
		"body": [
			"enum class ${MyClass} { };"
		],
		"description": "Code snippet for enum class (c++11)"
	},
	"class": {
		"prefix": "class",
		"body": [
			"class ${MyClass}",
			"{",
			"public:",
			"	${MyClass}();",
			"	${MyClass}(${MyClass} &&) = default;",
			"	${MyClass}(const ${MyClass} &) = default;",
			"	${MyClass} &operator=(${MyClass} &&) = default;",
			"	${MyClass} &operator=(const ${MyClass} &) = default;",
			"	~${MyClass}();",
			"",
			"private:",
			"	$1",
			"};",
			"",
			"${MyClass}::${MyClass}()",
			"{",
			"}",
			"",
			"${MyClass}::~${MyClass}()",
			"{",
			"}"
		],
		"description": "Code snippet for class"
	},
	"classi": {
		"prefix": "classi",
		"body": [
			"class ${MyClass}",
			"{",
			"public:",
			"	${MyClass}() = default;",
			"	${MyClass}(${MyClass} &&) = default;",
			"	${MyClass}(const ${MyClass} &) = default;",
			"	${MyClass} &operator=(${MyClass} &&) = default;",
			"	${MyClass} &operator=(const ${MyClass} &) = default;",
			"	~${MyClass}() = default;",
			"",
			"private:",
			"	$1",
			"};"
		],
		"description": "Code snippet for class with inline constructor/destructor"
	},
	"interface": {
		"prefix": "interface",
		"body": [
			"__interface I${Interface}",
			"{",
			"	$1",
			"};"
		],
		"description": "Code snippet for interface (Visual C++)"
	},
	"namespace": {
		"prefix": "namespace",
		"body": [
			"namespace ${MyNamespace}",
			"{",
			"	$1",
			"}"
		],
		"description": "Code snippet for namespace"
	},
	"#ifdef": {
		"prefix": "#ifdef",
		"body": [
			"#ifdef ${DEBUG}",
			"$1",
			"#endif // ${DEBUG}"
		],
		"description": "Code snippet for #ifdef"
	},
	"#ifndef": {
		"prefix": "#ifndef",
		"body": [
			"#ifndef ${1:1}",
			"$2",
			"#endif // !$1"
		],
		"description": "Code snippet for #ifndef"
	},
	"#if": {
		"prefix": "#if",
		"body": [
			"#ifdef ${1:0}",
			"$2",
			"#endif // $1"
		],
		"description": "Code snippet for #if"
	},
	"struct": {
		"prefix": "struct",
		"body": [
			"struct ${MyStruct}",
			"{",
			"	$1",
			"};"
		],
		"description": "Code snippet for struct"
	},
	"switch": {
		"prefix": "switch",
		"body": [
			"switch (${switch_on})",
			"{",
			"default:",
			"	break;$1",
			"}"
		],
		"description": "Code snippet for switch statement"
	},
	"try": {
		"prefix": "try",
		"body": [
			"try",
			"{",
			"	",
			"}",
			"catch (const std::exception&)",
			"{",
			"	$1",
			"}"
		],
		"description": "Code snippet for try catch"
	},
	"union": {
		"prefix": "union",
		"body": [
			"union ${MyUnion}",
			"{",
			"	$1",
			"};"
		],
		"description": "Code snippet for union"
	},
	"cout": {
		"prefix": "cout",
		"body": [
			"std::cout << \"${1:/* message */}\" << std::endl;"
		],
		"description": "Code snippet for printing to std::cout, provided the header is set"
	},
	"#inc": {
		"prefix": "#inc",
		"body": [
			"#include \"$1\""
		],
		"description": "Code snippet for #include \" \""
	},
	"#inc<": {
		"prefix": "#inc<",
		"body": [
			"#include <$1>"
		],
		"description": "Code snippet for #include \" \""
	},
	"#def": {
		"prefix": "#def",
		"body": [
			"#define \"$1\" \"$2\" "
			],
		"description": "Code snippet for #define \" \""
	},
	"main": {
		"prefix": "main",
		"body": [
			"int main() {",
      "   $0"
			"    return 0;",
			"}"
		],
		"description": "Code snippet for main function"
	}
}

常用的插件

  • vscode snippet 这个不是插件,是vscode自带的功能,一定要用好!!可以快速的写代码.插入自己写的各种算法的模板.

  • remote development,在win10下连接wsl写代码,节省使用的虚拟机的内存占用

  • vscode-luogu,wow 直接上传代码进行评测

  • share code,当你需要分享代码时 地址 : https://marketplace.visualstudio.com/items?itemName=RolandGreim.sharecode
    作用: 上传代码到网站上与其他人分享
    使用: 按Ctrl+Shift+P输入分享代码。然后可以选择Pastebin和GitHub Gist。(匿名)意味着您无需登录即可分享文件,但选择此选项后将无法以后编辑或删除文件
    具体看插件页面的动图

  • vscode-pdf,可以看pdf,这样就可以直接用vscode,看题目了

  • Dictionary Completion,补全英文输入的单词,需要配置一下

  • excel viewer,有时间会输出一些csv格式的文件,用来查找这些csv格式的数据,进行调试或找规律

  • excel-to-markdown-table 写markdown的时间有用