0%

Visual Studio Code的使用总结

Visual Studio Code介绍

Visual Studio Code是很轻量但功能强大的代码编辑器,可以在Windows、macOS和 Linux上运行。

User and Workspace Settings

通过settings,可以很容易地配置自己的Visual Studio Code。它提供了
two different scopes for settings:

  • User Settings - Settings that apply globally to any instance of VS Code you open.
  • Workspace Settings - Settings stored inside your workspace and only apply when the workspace is opened.

Workspace settings override user settings.

打开方式

页面打开:Code –> Preferences –> Settings,这种方式可以很直观地配置

也可以在json文件里直接修改:

  • User Settings:
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
  User cat settings.json
{
window.title: ${activeEditorLong}${separator}${rootName},
workbench.statusBar.visible: true,
workbench.colorTheme: Visual Studio Dark,
workbench.iconTheme: vs-minimal, // panel
window.zoomLevel: 0,
editor.tabSize: 4,
editor.detectIndentation: false,
editor.renderControlCharacters: true,
editor.minimap.enabled: false,
leetcode.endpoint: leetcode,
leetcode.defaultLanguage: golang,
breadcrumbs.enabled: false,
editor.renderWhitespace: none,
terminal.integrated.shell.osx: zsh,
terminal.external.osxExec: iTerm.app, // 默认打开iterm2
// golang
go.autocompleteUnimportedPackages: true,
go.formatTool: goimports, //使用 goimports 工具进行代码格式化,或者使用 goreturns gofmt
[go]: {
editor.formatOnSave: true,
},
// vim
vim.useSystemClipboard: true,
diffEditor.ignoreTrimWhitespace: false,
diffEditor.renderSideBySide: true,
C_Cpp.updateChannel: Insiders,
editor.suggestSelection: first,
vsintellicode.modify.editor.suggestSelection: automaticallyOverrodeDefaultValue,
editor.wordWrap: wordWrapColumn,
editor.wordWrapColumn: 120,
// 每行输入字符长度提示线
editor.rulers: [
80,
120
],
editor.renderLineHighlight: all,
go.useLanguageServer: true,
"window.openFilesInNewWindow": "on", //启用后,将在新窗口中打开文件,而不是重复使用现有实例。
"workbench.editor.enablePreview": false, // 打开的文件显示在多个tab"
}
  • Wrokspace: …

快捷键

默认常用快捷键

  • (Command Palette | Show All Commands) shift + command + p 或者 F1
  • 搜索文件 command + p
  • 搜索内容(Find in Files) shift + command + f
  • 回到上一个地方(Go Back) ctrl + -
  • 跳到侧边栏(Focus into Side Bar) command + 0
  • 切换侧边栏(Toggle Side Bar Visibility) command + b
  • 打开原生的console(OpenNativeConsole) shift + command +c
  • 切换Panel(Toggle Panel) command + j
  • 调整Panel大小(Resize Panel) ctrl + command + <– | –>

Code navigation

![image](Visual-Studio-Code的使用总结/Code navigation.jpg)

  • 跳转到定义(Go to Definition) F12
  • (showAllSymbols) command + t

自定义快捷键

更改方式

打开键盘快捷键

Keyboard Shotcuts里显示当前支持哪些快捷键,包括默认的和自定义的;也可以在里面直接更改

Keyboard Shotcuts(json)里了自定义的快捷键;前面在Keyboard Shotcuts修改的会展示在这里,也可以在这里直接修改

常用自定义快捷键

根绝个人喜好,常用的如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 {
"key": "cmd+enter",
"command": "workbench.action.toggleFullScreen" //切换全屏
},
{
"key": "cmd+right",
"command": "workbench.action.nextEditor" //下个编辑器
},
{
"key": "cmd+left",
"command": "workbench.action.previousEditor" //上个编辑器
},
{
"key": "alt+cmd+n",
"command": "openInTerminal" //打开Terminal,并跳到当前文件的目录
},
{
"key": "cmd+s cmd+w",
"command": "workbench.action.quickSwitchWindow" // 切换窗口
},
{
"key": "cmd+2",
"command": "workbench.action.focusPanel" // 跳到Panel
}

常用插件

在左边Extensions里选择插件install。下面列出几种常用的插件:

Vim

遇到的问题

  • Vim方向键无法长按

解决方案:终端下执行命令:

defaults write com.microsoft.VSCode ApplePressAndHoldEnabled -bool false

复原:

defaults write com.microsoft.VSCode ApplePressAndHoldEnabled -bool true

  • 让 VIM yank 至剪切板的内容通过 ⌘ V 粘贴出来

配置如下 setting:
“vim.useSystemClipboard”: true,

Go

https://code.visualstudio.com/docs/languages/go

Auto completions

1
2
"go.autocompleteUnimportedPackages": true, // Auto completions
"go.formatTool": "gofmt", //代码格式化, 可以用goimports,或者goreturns和gofmt; 必须把vscode的"files.autoSave": off才能生效

Code navigation

Code navigation features are available in the context menu in the editor.

  • Go To Definition F12 - Go to the source code of the type definition.
  • Peek Definition ⌥F12 - Bring up a Peek window with the type definition.
  • Peek References ⇧F12 - Show all references for the type. (显示该类型的所有引用,个人觉得这个功能很强大)

You can navigate via symbol search using the Go to Symbol commands from the Command Palette (⇧⌘P).

Go to Symbol in File - ⇧⌘O
显示该文件的symbol,symbol: 指定义的函数,变量,类型等

Go to Symbol in Workspace - ⌘T

You can also navigate back and forth between a Go file and its test implementation using the Go: Toggle Test File command.

Debugging Go code using VS Code

设置一个或者多个GOPATH

C/C++

Edit Configurations

F1打开Command Palette –> C/C++: Edit configurations (JSON)

主要设置”includePath”, 便于代码跳转,参考 https://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference

在终端里直接打开vscode

在vscode 中快捷键 shift + command + p 输入 code ,选择安装code 命令。
本质是做了个软链/usr/local/bin/code

用vscode打开当前目录:

1
code .