Skip to content

vscode

搭建完美vscode环境

操作篇

文本操作

opt 说明
ctrl + 左右 关闭快速移动到词头尾
ctrl + shift + 左右 关闭快速选择一个词
ctrl + shift + L 快速选择鼠标所在的词, 包含文档中所有相同的词
  • 段 | opt | 说明 | |---|---| | ctrl + F2 | 选中了一段之后执行此快捷键,会选出所有相同的元素 |

  • opt 说明
    ctrl + L 快速选择当前行, 一般会和其他快捷键冲突,建议修改为 ctrl + alt + L
    alt + shift + 上下 复制一行到上一行或下一行

配置篇

  • 常用配置
{
    // "terminal.integrated.profiles.windows": {
    //     // ...
    //     "Git Bash": {
    //         "source": "Git Bash",
    //         "path": "E:\\apps\\Git\\bin\\bash.exe"
    //     }
    // },
    "terminal.integrated.defaultProfile.windows": "Git Bash",
    // 配置此项,保证windows 环境和 linux 环境 换行符一致
    "files.eol": "\n",
    // 保证编辑文本时,左侧explore 不要跟着动, 这样配合 快捷键绑定(2) 中操作,左侧就不会跳来跳去了,又非常好找代码
    "explorer.autoReveal": false
}
  • 快捷键绑定
id option 说明 binding key
1 Switch Window 切换窗口 ctrl + shift + i
2 Reveal Active Key 显示当前文件所在目录 ctrl + alt + i

其他

代理配置

// 不要写成 http://127.0.0.1:7890, 否则有些走代理会出现错误
"terminal.integrated.env.linux": {
    "http_proxy": "127.0.0.1:7890",
    "https_proxy": "127.0.0.1:7890"
},

markdown 图片复制

vscode 1.79 版支持在markdown 中直接粘贴图片,不用再担心本机、wsl或remote环境的markdown图片配置问题了^[https://juejin.cn/post/7244809769794289721]

我的配置参考:

"markdown.copyFiles.destination": {
    // 将复制的图片放入根目录下的imgs文件夹中
    "**/*": "${documentWorkspaceFolder}/imgs/"
}

插件篇

根据路径跳转到文件

https://marketplace.visualstudio.com/items?itemName=rexebin.f12-open-file

  • 主要用于在注释中跳转到对应文件,相当好用

vscode wsl2

环境变量问题

windows 和 wsl2 环境冲突

同时使用 nvs

屏蔽 windows nvs环境路径, 在 ~/.bashrc 中设置如下, {user} 替换为windows系统的用户名

export PATH=$(echo $PATH | sed -e 's;:/mnt/c/Users/{user}/AppData/Local/nvs/default;;' -e 's;:/mnt/c/Users/{user}/AppData/Local/nvs/;;')
同时使用 npm公共库路径名

最好配置时与windows 配置名区分, 如下如果同时配置了 .npm-global 名称则需要屏蔽掉windows 的名称

export PATH=$(echo $PATH | sed -e 's;:/mnt/c/Users/{user}/.npm-global/bin:/mnt/c/Users/{user}/.npm-global;;')

在wsl2中 禁止继承windows 环境变量

在 /etc/wsl.conf 中设置

[interop]
enabled=false
appendWindowsPath=false

但这样有些 windows 命令不能使用,可以将其加入到 path环境变量中, 常用的有以下

# 包含 explore.exe 命令
/mnt/c/Windows  

各文件启动顺序

~/.bashrc  vscode 终端会默认加载一次
/etc/profile    vscode 调试模块会加载一次此相关的环境变量,但这个必须重启wsl

vscode c++配置

vscode debug配置

调试c++项目配置

launch.json配置

"version": "0.2.0",
"configurations": [
    {
        "name": "C++ Debug",
        "type": "cppdbg",
        "request": "launch",
        "program": "${workspaceFolder}/dist/${relativeFileDirname}/${fileBasenameNoExtension}",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "preLaunchTask": "build",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ]
    }
]

tasks.json

{
    "tasks": [
        {
            "label": "mkdir",
            "type": "shell",
            "command": "mkdir",
            "args": [
                "-p",
                "${workspaceFolder}/dist/${relativeFileDirname}"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "build",
            "type": "shell",
            "command": "g++", // 或者你用的其他 C++ 编译器的命令
            "args": [
                "-g",
                "${relativeFileDirname}/*.cpp",
                "-o",
                "${workspaceFolder}/dist/${relativeFileDirname}/${fileBasenameNoExtension}"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "dependsOn": "mkdir"
        }
    ],
    "version": "2.0.0"
}

配置括号位置

{
    "C_Cpp.clang_format_style": "{ BasedOnStyle: Chromium, IndentWidth: 4}",
}

code-server 使用

共用 vscode 目录插件

code-server 安装插件自带的源很费事

可以使用远程 vscode-remote 安装好插件后再进行软连接,如下

ln -s ~/.vscode/extensions ~/.local/share/code-server/extensions

这样就很方便安装源了