VsCode的部分基本操作

vscode的部分基本操作

多行同一位置选择

  1. 同时选中多行 shift + alt + 鼠标左键 向下向上拖动就可以同时选中多行

文章位置前进后退

  1. 文章的前进和后退分别是 alt + alt +

vscode自动保存

  1. 打开Visual Studio Code,左上角依次点开 文件→首选项→设置;
  2. 搜索设置框输入”autosave”,点击的选择项有如下:
    Auto Save
    1
    2
    3
    4
    off: 关闭自动保存
    afterDelay: XX毫秒后自动保存,这个就是我所讲的解决方法,下面会详细介绍
    onFocusChange: 当焦点移出编辑框
    onWindowChange: 当焦点移出VSCode窗口

    这里说的是焦点而不是鼠标,移到外面去后还要点一下才能保存。如果设置延时自动保存,建议频率不要太高(也就是设置延时时间不要太短),频率很高就会导致CPU资源消耗增加。

正则表达式

文件编写属性

一、通过「工作区设置」(推荐,按项目 / 文件夹区分)

VS Code 的「工作区设置」会保存在项目根目录的 .vscode/settings.json 文件中,仅对当前项目生效,适合团队协作或不同项目的差异化配置。操作步骤:

  1. 打开目标项目文件夹(确保已作为 “工作区” 打开)。
  2. 按下 Ctrl+Shift+P(或 Cmd+Shift+P)打开命令面板,输入并选择 **Open Workspace Settings (JSON)**。
  3. 在生成的 .vscode/settings.json 中,通过「文件匹配规则」为特定文件 / 类型配置属性。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
{	
"files.associations": {
"cstdint": "cpp",
"cstdio": "cpp",
"cctype": "cpp"
}
// 新增:编码格式配置(按文件路径/类型区分)
"[txt]": {
// 所有 .txt 文件默认用 utf-8 编码
"files.encoding": "utf8"
},
"[md]": {
// 所有 .md(Markdown)文件默认用 utf-8 编码
"files.encoding": "utf8"
},
// 部分 .cpp 文件用 utf-8(例如:src 目录下的 .cpp)
"**/src/*.cpp": {
"files.encoding": "utf8"
},
// 部分 .cpp 文件用 gb2312(例如:legacy 目录下的 .cpp)
"**/legacy/*.cpp": {
"files.encoding": "gb2312"
},
// 特定单个 .cpp 文件用 gb2312(例如:old_file.cpp)
"old_file.cpp": {
"files.encoding": "gb2312"
},

// 对所有文件生效
"editor.tabSize": 4, // 缩进空格数
"editor.insertSpaces": true, // 使用空格代替制表符
"editor.wordWrap": "on", // 自动换行("off" 关闭,"wordWrapColumn" 按列数换行)
"editor.lineNumbers": "on", // 显示行号("off" 关闭,"relative" 相对行号)
"editor.rulers": [80, 120], // 在 80/120 列显示参考线
"editor.formatOnSave": true, // 保存时自动格式化

// 仅对 .cpp 文件生效
"[cpp]": {
"editor.tabSize": 2, // C++ 文件缩进 2 空格
"editor.formatOnSave": false, // C++ 保存时不自动格式化
"editor.defaultFormatter": "ms-vscode.cpptools", // 指定 C++ 格式化工具
}
}
  1. .txt.md 文件:通过 [txt][md] 匹配所有后缀为 .txt.md 的文件,直接指定编码为 utf8(VS Code 中 utf8 等同于 utf-8)。

  2. 部分 .cpp 文件区分编码

    • 用通配符 **/src/*.cpp 匹配src 目录下所有 .cpp 文件,设置为 utf8
    • **/legacy/*.cpp 匹配legacy 目录下所有 .cpp 文件,设置为 gb2312
    • 用精确文件名 old_file.cpp 匹配单个 .cpp 文件,设置为 gb2312(适合单独的特殊文件)。

    其中 ** 表示 “任意层级的文件夹”,可根据你的实际目录结构修改(例如 src/utils/*.cpp 仅匹配 src/utils 下的 .cpp)。

    生效逻辑

    VS Code 会按 “** 精确匹配优先于模糊匹配 **” 的规则应用配置:

    • 若一个文件同时满足多个规则(例如 legacy/src/test.cpp 同时匹配 **/src/*.cpp**/legacy/*.cpp),则更具体的路径规则会生效(此处 **/legacy/*.cpp 更具体)。
    • 配置后,打开对应文件时会自动应用指定编码,无需手动调整。

    如果需要调整具体文件 / 目录的匹配范围,只需修改通配符路径即可(例如 docs/*.md 仅匹配 docs 目录下的

文件

files.associations(文件关联)和 files.encoding(文件编码)

编辑器基础行为(editor.*

针对编辑操作的全局或文件类型特定配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 对所有文件生效
"editor.tabSize": 4, // 缩进空格数
"editor.insertSpaces": true, // 使用空格代替制表符
"editor.wordWrap": "on", // 自动换行("off" 关闭,"wordWrapColumn" 按列数换行)
"editor.lineNumbers": "on", // 显示行号("off" 关闭,"relative" 相对行号)
"editor.rulers": [80, 120], // 在 80/120 列显示参考线
"editor.formatOnSave": true, // 保存时自动格式化

// 仅对 .cpp 文件生效
"[cpp]": {
"editor.tabSize": 2, // C++ 文件缩进 2 空格
"editor.formatOnSave": false, // C++ 保存时不自动格式化
"editor.defaultFormatter": "ms-vscode.cpptools", // 指定 C++ 格式化工具
}

文件操作相关(files.*

除了 encodingassociations,其他文件相关配置:

1
2
3
4
5
6
7
8
9
10
"files.autoSave": "onFocusChange",  // 失去焦点时自动保存("off" 关闭,"afterDelay" 延迟保存)
"files.exclude": { // 在资源管理器中隐藏指定文件/目录
"**/.git": true,
"**/.DS_Store": true,
"**/node_modules": true
},
"files.watcherExclude": { // 排除文件监听(提升大项目性能)
"**/node_modules/**": true
},
"files.eol": "\n", // 换行符(\n 或 \r\n),默认自动检测

语言特定配置(按文件类型)

通过 [语言ID] 或文件路径匹配,针对特定语言配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// JavaScript 文件
"[javascript]": {
"editor.minimap.enabled": false, // 关闭迷你地图
"editor.snippetSuggestions": "top", // 代码片段提示优先
"javascript.validate.enable": true, // 启用 JS 语法校验
},

// Python 文件
"[python]": {
"python.defaultInterpreterPath": "/usr/bin/python3", // 默认解释器路径
"python.linting.pylintEnabled": true, // 启用 pylint 检查
"python.formatting.provider": "black", // 使用 black 格式化
},

// Markdown 文件
"[markdown]": {
"editor.quickSuggestions": { // 启用 Markdown 快速提示
"other": true,
"comments": true,
"strings": true
},
"markdown.preview.fontSize": 16, // 预览字体大小
}

格式化与代码风格(format.* 或语言相关)

控制代码格式化工具和风格:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 全局格式化配置
"editor.defaultFormatter": "esbenp.prettier-vscode", // 默认格式化工具(如 Prettier)
"editor.formatOnPaste": false, // 粘贴时不自动格式化

// 针对 JSON 的格式化
"[json]": {
"editor.defaultFormatter": "vscode.json-language-features",
"json.format.enable": true, // 启用 JSON 格式化
"json.format.spaceAfterComma": true // 逗号后加空格
},

// C# 格式化风格
"[csharp]": {
"csharp.format.enable": true,
"csharp.format.newLineAfterOpenBrace": true // 左大括号后换行
}

代码提示与补全(editor.quickSuggestions 等)

控制智能提示行为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 全局启用快速提示
"editor.quickSuggestions": {
"other": true,
"comments": false, // 注释中不显示提示
"strings": true // 字符串中显示提示(如路径补全)
},

// 对 HTML 增强提示
"[html]": {
"editor.quickSuggestions": {
"tags": true // 标签自动补全
},
"html.suggest.html5": true // 启用 HTML5 标签提示
}

终端与集成(terminal.*

如果需要针对文件类型关联终端行为:

1
2
3
4
5
6
7
8
9
10
11
"terminal.integrated.defaultProfile.windows": "PowerShell",  // Windows 默认终端
"terminal.integrated.profiles.linux": { // Linux 终端配置
"bash": {
"path": "/bin/bash"
}
},
"[python]": {
"terminal.integrated.env.python": { // Python 终端环境变量
"PYTHONPATH": "${workspaceFolder}"
}
}

其他实用配置

  • 拼写检查(需 cSpell 插件):
1
2
3
4
"[markdown]": {
"cSpell.enabled": true, // 对 Markdown 启用拼写检查
"cSpell.words": ["CodeViz", "GB2312"] // 自定义词典(忽略这些词的拼写错误)
}
  • 文件图标(需 vscode-icons 插件):
1
2
3
"vsicons.associations.files": {  // 自定义文件图标
"*.conf": "config" // .conf 文件显示为配置图标
}
  • 折叠设置
1
2
3
4
"[cpp]": {
"editor.foldingStrategy": "indentation", // 按缩进折叠(而非语法)
"editor.showFoldingControls": "always" // 始终显示折叠按钮
}

配置技巧

  1. 通过 UI 界面生成配置:打开设置界面(Ctrl+,),搜索具体属性(如 tabSize),修改后会自动同步到 settings.json
  2. 按优先级生效:工作区设置(.vscode/settings.json)> 用户设置 > 全局默认,具体文件路径匹配(如 **/src/*.cpp)优先于语言类型匹配(如 [cpp])。
  3. 查看所有可用属性:参考 VS Code 官方配置文档

根据你的开发场景(如 C/C++、前端、Python 等),可以针对性配置上述属性,优化编辑体验。

files.associations:文件关联配置

用于将特定文件(如无后缀文件、自定义后缀文件)关联到 VS Code 支持的语言类型(如 cpppythonjson 等),从而启用该语言的语法高亮、智能提示、格式化等功能。

1. 配置格式

键为 “文件匹配规则”,值为 “语言 ID”(VS Code 内置的语言标识):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
"files.associations": {
// 1. 自定义后缀文件关联到指定语言
"*.abc": "javascript", // .abc 文件按 JavaScript 语法处理
"*.xyz": "python", // .xyz 文件按 Python 语法处理

// 2. 无后缀文件关联到指定语言
"config": "ini", // 无后缀的 "config" 文件按 INI 语法处理
"Makefile": "makefile", // Makefile(无后缀)按 makefile 语法处理

// 3. 特定路径的文件关联到指定语言
"src/**/*.log": "text", // src 目录下所有 .log 文件按纯文本处理
"test/*.temp": "json" // test 目录下 .temp 文件按 JSON 语法处理

// 4. 系统头文件关联(如 C/C++ 中常见)
"cstdint": "cpp", // cstdint 头文件按 C++ 语法处理
"*.h": "c" // .h 头文件默认按 C 语法处理(而非 C++)
}

2. 常用语言 ID 参考

VS Code 内置的常见语言 ID(可在设置中通过自动补全查看全部):

  • cpp:C++
  • c:C
  • python:Python
  • javascript:JavaScript
  • typescript:TypeScript
  • json:JSON
  • markdown:Markdown
  • html:HTML
  • css:CSS
  • ini:INI 配置文件
  • makefile:Makefile
  • text:纯文本
1
2
3
4
5
6
7
8
9
10
{
"files.associations": {
"config.dat": "ini", // config.dat 按 INI 语法处理
"old_api.c": "c", // 显式指定为 C 语言(可选,默认已关联)
"new_api.cpp": "cpp" // 显式指定为 C++ 语言(可选)
},
"old_api.c": { "files.encoding": "gbk" }, // 单个文件用 GBK
"[cpp]": { "files.encoding": "utf8" }, // 所有 C++ 文件用 UTF-8
"[ini]": { "files.encoding": "utf8" } // 所有 INI 文件用 UTF-8
}