1. 介绍
vscode可以做很多事, 是真正的生产力工具, 但是如何将其配置得好用并符合自己的需求, 却是很复杂的一件事. 它的默认模式就像一辆自动挡的汽车, 不做任何改动也能开着上路, 如果想压榨一下它的生产力, 还是要懂得一些配置相关的知识或技巧.
VSCode安装
下载VSCode
VSCode有两种版本
-
User Installer版:会安装在当前计算机帐户目录,意味着如果使用另一个账号登陆计算机将无法使用别人安装的vscode。
-
System Installer版:安装在非用户目录,例如C盘根目录,任何帐户都可以使用。(建议使用此版本)
-
vscode默认提供的User Installer版,大多数人都是用的这个版本。
安装步骤:
双击Installer按指引安装
2. 如何打开配置
vscode的配置入口并不在一个很显眼或很容易找到的地方, 它位于一个与配置不太关联的主菜单下的二级菜单, 这也是为什么此处要单独劈开一节介绍如何打开配置得原因.
- 打开方式
- 方法一:通过菜单打开 设置菜单位于 文件->首选项->设置. 打开用户设置。VScode支持选择配置,也支持编辑setting.json文件修改默认配置。个人更倾向于编写json的方式进行配置.
- 方法二: 使用快捷键打开 打开设置的快捷键通常为: Ctrl+Alt+s, 打开配置面板后, 右上角有个Open Settings(json)可以打开settings.json.
- vscode 有两种配置方式, 一种是图形界面, 这种非常时候普通用户, 使用鼠标点击就能完成设置. vscode也可以通过配置文件来进行配置, 配置文件为json格式的settings.json
3. VScode用户设置
3.1. 设置字体大小
(1)editor.fontsize用来设置字体大小,可以设置editor.fontSize : 14;
"editor.fontSize": 14,
3.2. 设置自动保存方式
(2)files.autoSave这个属性是表示文件是否进行自动保存,推荐设置为onFocusChange——文件焦点变化时自动保存。
"files.autoSave":"onFocusChange",
3.3. 设置自动补全
(3)editor.tabCompletion用来在出现推荐值时,按下Tab键是否自动填入最佳推荐值,推荐设置为on;
3.4. 查询中过滤掉非必要的文件
vscode的搜索页面位于左侧, 本来就很窄很拥挤, 如果再出现一些明显非必要的结果,更显得拥挤不堪, 所以非常有必要做一些过滤, 这样每次的搜索可以直达目标, 省去一些手动折叠文件, 鼠标向下滚动等等操作, 非常有帮助. 当然你说连代码的产出物也是你搜索的范围, 目前来说我没有想出什么更好的办法, 只有使用快捷键打开配置, 将这些过滤条件关闭掉, 也不用重启, 每隔数秒配置会自动生效.
"search.exclude": {
"**/node_modules": true,
"**/bower_components": true,
"build/": true,
"temp/": true,
"library/": true
},
3.5. 过滤文件
有些文件是系统,或者某些工具使用的配置文件, 我们不常使用, 将其列在项目中会分散注意力, 其实我们完全可以将这些文件从项目文件中将其忽略掉, 比如.git配置文件对象树, .idea配置文件, 这些文件我们几乎平时很少修改, 也不必去关心, 可以将其设置为忽略.
"files.exclude": {
"**/.git": true,
"**/.idea": true
}
3.6. 关闭自动检测更新
"extensions.autoCheckUpdates": false,
3.7. 配置大小写转换快捷键
编辑keybindings.json
{
"key": "ctrl+shift+l",
"command": "editor.action.transformToLowercase"
},
{
"key": "ctrl+shift+u",
"command": "editor.action.transformToUppercase"
}
3.8. 配置快捷键
打开快捷键配置 file -> preference -> keyboard shotcuts 也可以通过配置文件进行配置keybindings.json
3.9. 安装与配置主体
- 以安装panda主体为例:
- 首先安装主题
- 点开左侧边栏的extentions
- 搜索panda theme找到对应的插件然后安装
- 然后配置主题
-
打开颜色主题, 选择Panda作为主题
-
或者使用快捷键Ctl+Shift+p 打开命令面板,输入color theme 然后选择PandaSyntax
-
或者直接修改settings.json
"workbench.colorTheme": "Panda Syntax", -
说明在最终设置中我还是使用的 Solarized Dark 这个主题, 比较习惯它的风格.
-
3.10. 关闭Open editor
open editor对我来说其实没有什么用处, 而占有了太多的侧边栏位置将 workspace视图挤得只有一点点, 而且在顶部.
而我使用workspace的频率远远大于open editor, 即使我需要看那些打开的文件, 我也可以使用快捷键打开
"explorer.openEditors.visible": 0
3.11. 在侧边栏快速定位文件
虽然右键点击标签页,选择reveal in sidebar可以定位到侧边栏, 我希望给它定义一个快捷键, 这样可以减少鼠标操作. 取这个功能reveal的首字母构成一个快捷键组合Ctrl + Alt + r 修改keybindings.json
{
"key": "ctrl+alt+r",
"command": "workbench.files.action.showActiveFileInExplorer"
}
参考文档: How to reveal current file in Explorer?
3.12. vscode整合gitbash
打开用户设置: File -> Preference -> settings, 切换到json模式
添加如下配置到Settings并保存, 其中bash.exe的路径根据自己实际安装位置做相应替换.
"terminal.integrated.profiles.windows": {
"Git-Bash": {
"path": "C:\\Program Files\\Git\\usr\\bin\\bash.exe"
}
},
"terminal.integrated.defaultProfile.windows": "Git-Bash"
3.13. vscode高亮括号对
// 開啟 bracket pair colorization;1.67 以上可省略此開啟設定
"editor.bracketPairColorization.enabled": true,
// 設定顏色
"workbench.colorCustomizations": {
// 層級括號顏色,從 1 至 6 層,此處只設定了 5 層
"editorBracketHighlight.foreground1": "#ffd700",
"editorBracketHighlight.foreground2": "#DC143C",
"editorBracketHighlight.foreground3": "#87cefa",
"editorBracketHighlight.foreground4": "#ffd700",
"editorBracketHighlight.foreground5": "#da70d6",
"editorBracketHighlight.foreground6": "#87cefa",
// 異常括號的顏色,比如多出來的結尾括號
"editorBracketHighlight.unexpectedBracket.foreground": "#ff0000",
}
参考文章:
VS Code 開啟效能提升 1 萬倍的「內建」bracket pair colorization
vscode1.60原生高性能括号着色无缝迁移方案
4. 安装plugins
Angular Snippets (Version 13) Go Intellij IDEA Keybindings Markdown Preview Mermaid Support
4.1. 插件列表
| 插件 | 版本 | 安装命令 | 描述 |
|---|---|---|---|
| Angular Snippets (Version 13) | v13.0.0 | ext install johnpapa.Angular2 | The extension for Visual Studio Code adds snippets for Angular for TypeScript and HTML |
| gitlens | v11.7.0 | ext install eamodio.gitlens | GitLens simply helps you better understand code. Quickly glimpse into whom, why, and when a line or code block was changed. Jump back through history to gain further insights as to how and why the code evolved. Effortlessly explore the history and evolution of a codebase. |
| intellij-idea-keybindings | v1.5.0 | ext install k--kato.intellij-idea-keybindings | Port of IntelliJ IDEA key bindings for VS Code. Includes keymaps for popular JetBrains products like IntelliJ Ultimate, WebStorm, PyCharm, PHP Storm, etc. |
| lebab | 2.6.0 | ext install mrmlnc.vscode-lebab | Lebab transpiles your ES5 code into readable ES2015 (sugar-syntax). It does exactly the opposite of what Babel does. |
| markdown-mermaid | v1.13.0 | ext install bierner.markdown-mermaid | Adds Mermaid diagram and flowchart support to VS Code's builtin markdown preview |
| bookmarks | v13.2.2 | ext install alefragnani.bookmarks | Improve your Bookmarks experience with Tabnine code completions |
| ~~ Formate is an CSS/LESS/SCSS format extension to format properties and align property values to improve readability. ~~ | |||
| Markdown All in One | v3.4.0 | ext install yzhang.markdown-all-in-one | All you need for Markdown(keyboard shortcuts, table of contents, auto preview and more) |
| mardownlint | v0.47.0 | ext install DavidAnson.vscode-markdownlint | Markdown/CommonMark linting and style checking for Visual Studio Code |
| Go | v0.33.1 | ext install golang.go | Rich Go language support for Visual Studio Code |
| Bracket Pair Colorizer | v1.0.62 | ext install CoenraadS.bracket-pair-colorizer | Rich Go language support for Visual Studio Code |
| rust-analyzer | v0.3.1301 | ext install rust-lang.rust-analyzer | this extention provides support for the Rust programming language. |
| Gradle Language Support | v0.2.3 | ext install naco-siren.gradle-language | Gradle Language Support |
| Import Cost | v3.3.0 | ext install wix.vscode-import-cost | This extension will display inline in the editor the size of the imported package. The extension utilizes webpack in order to detect the imported size. |
| Todo Highlighter | v1.0.1 | ext install zerefdev.todo-highlighter | zerefdev.todo-highlighter |
| Todo Tree | v0.0.223 | ext install Gruntfuggly.todo-tree | This extension quickly searches (using ripgrep) your workspace for comment tags like TODO and FIXME, and displays them in a tree view in the activity bar. The view can be dragged out of the activity bar into the explorer pane (or anywhere else you would prefer it to be). |
| Language Support for Java(TM) by Red Hat | v0.2.3 | ext install redhat.java | Provides Java ™ language support via Eclipse ™ JDT Language Server, which utilizes Eclipse ™ JDT, M2Eclipse and Buildship. |
| shell-format | v7.2.5 | ext install foxundermoon.shell-format | A formatter for shell scripts, Dockerfile, gitignore, dotenv, /etc/hosts, jvmoptions, and other file types. |
| CodeCursor(Cursor for VS Code) | v0.4.0 | ext install ktiays.aicursor | Cursor integration for Visual Studio Code. |
| vscode-icons | v12.3.0 | ext install vscode-icons-team.vscode-icons | Bring icons to your Visual Studio Code and Icons Customization. |
| Prettier - Code formatter | v9.10.4 | ext install esbenp.prettier-vscode | Prettier is an opinionated code formatter.. |
| Colorize | v0.11.1 | ext install kamikillerto.vscode-colorize | Instantly visualize css colors in your css/sass/less/postcss/stylus/XML... files. |
| indent-rainbow | v8.3.1 | ext install oderwat.indent-rainbow | A simple extension to make indentation more readable. |
| live server | v5.7.9 | ext install ritwickdey.LiveServer | Launch a local development server with live reload feature for static & dynamic pages. |
Ctrl+shift+p 或者按F1 打開命令行提示框, 輸入安裝命令進行插件安裝
5. 完整配置
{
"editor.minimap.enabled": false,
"editor.fontSize": 14,
"breadcrumbs.enabled": true,
"editor.renderControlCharacters": true,
"editor.renderWhitespace": "none",
"workbench.colorTheme": "Solarized Dark",
"update.mode": "none",
"explorer.confirmDelete": false,
"explorer.confirmDragAndDrop": false,
"extensions.autoCheckUpdates": false,
"files.autoSave": "onFocusChange",
"search.exclude": {
"**/node_modules": true,
"**/bower_components": true,
//hexo 生成的文件
"**/db.json": true,
//hexo 生成的文件
"public/**/*": true,
"build/": true,
"temp/": true,
"library/": true,
"**/*.anim": true
},
"files.exclude": {
"**/.git": true,
"**/.idea": true,
"**/.classpath": true,
"**/.project": true,
"**/.settings": true
},
"gitlens.currentLine.scrollable": false,
"gitlens.currentLine.pullRequests.enabled": false,
"gitlens.currentLine.enabled": false,
"gitlens.hovers.currentLine.over": "line",
"gitlens.codeLens.enabled": false,
// 開啟 bracket pair colorization;1.67 以上可省略此開啟設定
"editor.bracketPairColorization.enabled": true,
// 設定顏色
"workbench.colorCustomizations": {
// 層級括號顏色,從 1 至 6 層,此處只設定了 5 層
"editorBracketHighlight.foreground1": "#ffd700",
"editorBracketHighlight.foreground2": "#DC143C",
"editorBracketHighlight.foreground3": "#87cefa",
"editorBracketHighlight.foreground4": "#ffd700",
"editorBracketHighlight.foreground5": "#da70d6",
"editorBracketHighlight.foreground6": "#87cefa",
// 異常括號的顏色,比如多出來的結尾括號
"editorBracketHighlight.unexpectedBracket.foreground": "#ff0000",
},
"terminal.integrated.profiles.windows": {
"Git-Bash": {
"path": "C:\\Program Files\\Git\\usr\\bin\\bash.exe"
}
},
"terminal.integrated.defaultProfile.windows": "Git-Bash",
"prettier.tabWidth": 4
}
6. 说明
本文原文位于鹏叔的技术博客空间 - vscode基本配置指南, 若要获取最近更新请访问原文.