终于,又换编辑器了,之前就听人说 VS Code 特别好用,但由于懒,一直没有尝试。最近公司在推广 VSCode-huawei,大概就是在开源 vscode 的基础上去掉微软专有的插件,再提供一个内部的插件市场。笔者也借此机会,把家里(之前用的 sublime text 3)和公司(之前用 sublime text 3 和 source insight 4)的编辑工具统一一下。本文仅仅是 VS Code 常用快捷键及扩展插件的列举。

Shortcuts

                                    Mac                     Windows

## 通用

打开 shortcuts 网页文档           cmd K + cmd R            Ctrl+K Ctrl+R
在 vscode 显示 shortcuts         cmd K + cmd S            Ctrl+K Ctrl+S
显示命令面板                      cmd + shift + P          Ctrl+Shift+P, F1
跳转到文件及一些其他操作            cmd P                    Ctrl+P
    - 直接输入文件名,跳转到文件
    - ?  列出当前可执行的动作
    - !  显示 Errors或 Warnings
    - :  跳转到行数
    - @  跳转到当前文件的 symbol(搜索变量或者函数)
    - @: 根据分类跳转到当前文件的 symbol(搜索变量或者函数)
    - #  根据名字查找 symbol
切换 sidebar 显示                cmd B                    Ctrl+B
切换 Zen Mode 模式               cmd + K + Z              Control+K+Z
显示/隐藏 Panel                  cmd + J                  Control+J
Split Editor                    cmd + \                  Control+\
Focus on 1st Editor Group       cmd + 1                  Contrpl+1

## 跳转命令

跳转到文件                        cmd P                    Ctrl+P
跳转到行号                        control+G                Ctrl+G
跳转到当前文件的的符号              cmd + shift + O          Ctrl+shift+O
根据分类跳转到当前文件的符号         cmd + shift + O 并输入:   Ctrl+shift+O 并输入:
Go Back                         cmd + left               Ctrl+left
Go Forward                      cmd + right              Ctrl+right
Go to Definition                F12                      F12
Go to Implementations           cmd F12                  Ctrl+F12
Go to References                shift + F12              Shift+F12
Go to Bracket                   cmd + shift + \

## Bookmarks
Mark/Unmark                     cmd + option + K
Jump to Next                    cmd + option + L
Jump to Previous                cmd + option + J

## Build && RUN
Build                           cmd + shift + B
Run Test Task                   cmd + control + T

## 其它

预览 markdown 文档                 cmd + shift + V
打开当前文件所在的目录               cmd K + R                Ctrl K + R

Extensions

VS Code 提供了一个插件市场,这里有很多强大的各式各样的插件,每个人可以根据开发语言及个人喜好选择自己的工具。

General

C/C++

Front-End

Settings

settings.json

{
    "workbench.iconTheme": "vscode-icons",
    "vsicons.dontShowNewVersionMessage": true,
    "terminal.integrated.shell.osx": "/bin/zsh",
    "editor.suggestSelection": "first",
    "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
    "C_Cpp.updateChannel": "Insiders",
    "editor.fontSize": 14,
    "editor.rulers": [80, 100, 120],
    "cmake.configureOnOpen": false,
    "terminal.integrated.fontSize": 14,
    "debug.console.fontSize": 14,
    "git.ignoreLegacyWarning": true,
    "editor.renderWhitespace": "all",
    "window.zoomLevel": 0,
    "scrollkey.line1": 1,
    "scrollkey.line2": 10,
    "scrollkey.line3": 20,
    "files.autoGuessEncoding": true,
    "files.trimTrailingWhitespace": true,
    "files.autoSave": "afterDelay",
    "C_Cpp.default.cppStandard": "c++11",
    "editor.minimap.enabled": false,
    "zenMode.hideLineNumbers": false,
    "explorer.openEditors.visible": 0
}

也可在每个 workspace 设置 settings.json

.vscode/settings.json

{
    // 暗淡不活跃的代码区,如 #if 0/#endif 之间的代码
    "C_Cpp.dimInactiveRegions": false
}

keybinds.json

// Place your key bindings in this file to override the defaults
[
    {
        "key": "ctrl+cmd+e",
        "command": "workbench.action.editor.changeEncoding",
        "when": "editorTextFocus"
    },
    {
        "key": "cmd+up",
        "command": "scrollkey.up2",
        "when": "editorTextFocus"
    },
    {
        "key": "cmd+down",
        "command": "scrollkey.down2",
        "when": "editorTextFocus"
    },
    {
        "key": "ctrl+cmd+up",
        "command": "cursorTop",
        "when": "textInputFocus"
    },
    {
        "key": "ctrl+cmd+down",
        "command": "cursorBottom",
        "when": "textInputFocus"
    },
    {
        "key": "cmd+left",
        "command": "workbench.action.navigateBack"
    },
    {
        "key": "cmd+right",
        "command": "workbench.action.navigateForward"
    },
    {
        "key": "ctrl+cmd+t",
        "command": "workbench.action.tasks.test"
    },
]

Code formatting

C/C++ 插件集成了 clang-format,默认情况下,clang-format 会在工作目录寻找 .clang-format 文件,如果找不到该文件,会使用 Visual Studio 的代码风格。clang-format 书写方式见 clang-format style options

Sample tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "build",
            "options": {
                "env": {
                    "CPLUS_INCLUDE_PATH": "/usr/local/include",
                    "LIBRARY_PATH": "/usr/local/lib"
                }
            },
            "command": "rm -rf build && mkdir build && cd build; cmake ..; make",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": []
        },
        {
            "type": "shell",
            "label": "run",
            "command": "build/unit_tests",
            "dependsOn":["build"],
            "group": {
                "kind": "test",
                "isDefault": true
            },
            "problemMatcher": []
        }
    ]
}

Sample launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(lldb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceFolder}/build/unit_tests",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb"
        }
    ]
}

Tips

  • 如果无法直接在vscode安装插件,可以获取相应的 vsix 文件,并使用 Install from VSIX 的方式安装插接件。

  • 打开一个新文件会被另一个新打开的文件替换,是因为前一个文件是一个不固定的 Editor(可以通过 Editor 上的字体来判断,斜体表示没有固定,正体表示固定),可以使用双击 Editor 或者双击 sidebar 中文件的方式使文件固定在 Editor 区域,编辑一个文件也会将其 Editor 固定。

Videos