vscode

字体配置——使用JetBrains Mono

  1. 下载Jetbrians Mono字体:how-to-install
  2. 解压缩
  3. 安装
    1. Mac下将ttf文件夹下的文件全选,右击选择打开,安装所有字体
    2. Centos9下, 将ttf文件夹下的文件全部移动到 /usr/share/fonts/${newdir}下 , yum install -y fontconfig 并执行 fc-cache 。然后执行 fc-list 即可看到新的字体
  4. 搜索font family,改成 'JetBrains Mono', Menlo, Monaco, 'Courier New', monospace
  5. 搜索font size,改成13。如果这个字体还不够大,可以 command + +来放大UI。
  6. 搜索line height,改成1.6。

TIPS:

  1. 建议打开vscode的配置同步功能。
  2. 本地的字体设置对远程开发同样生效。

快捷键配置

  1. cmd+k ,再按 cmd+s,进入快捷键设置
  2. 点击右上角按钮进入原始文件 keybindings.json

Alt text

  1. 修改文件内容如下:
快捷键 作用
cmd+m 用于切换多个终端,特别是在 cmd+shift+b 触发构建任务之后
option+o 用于打开remote explorer窗口,在多个远程开发环境中切换
option+t 用于打开outline窗口,用于查看大纲或者函数列表
// 将键绑定放在此文件中以覆盖默认值auto[]
[
    {
        "key": "cmd+m",
        "command": "workbench.action.terminal.focusNext"
    },
    {
        "key": "alt+o",
        "command": "workbench.view.remote"
    },
    {
        "key": "alt+t",
        "command": "outline.focus"
    }
]

其他快捷键

作用 MacOS快捷键 Windows快捷键
Expand / shrink selection ⌃⇧⌘ → / ← Shift+Alt+→ / ←
Go to Line… ⌃G Ctrl+G
Change language mode ⌘K M Ctrl+K M
Go back / forward ⌃- / ⌃⇧- Alt+ ← / →
Toggle word wrap ⌥Z alt + z
Delete line ⇧⌘K Ctrl+Shift+K

内置快捷键cheatsheet

  1. MacOS快捷键
  2. Windows快捷键

远程开发

  1. 安装 Remote-ssh 插件。
  2. 使用ssh连接到远程服务器。推荐配置是2C2G以上。为了让远程服务器流畅连接网络,使用了clash做分流,并将clash作为系统代理。
  3. 历史记录保存在 ~/.ssh/config 中。

卸载远程服务器vscode server

这个比想象的更加常用,因为在大型的项目中,符号跳转经常导致CPU占用很高并且无响应

  1. 执行命令 Remote-SSH: Uninstall VS Code Server from Host 不推荐,实测没有卸载干净
  2. 或手动执行
cat > /usr/local/bin/killcode <<\EOF
# Kill server processes
kill -9 $(ps aux | grep vscode-server | grep $USER | grep -v grep | awk '{print $2}')
# Delete related files and folder
rm -rf $HOME/.vscode-server # Or ~/.vscode-server-insiders
EOF
chmod +x /usr/local/bin/killcode
killcode

Rust开发

安装四个插件:

插件名 说明
rust-analyzer rust插件
codelldb 调试插件
tamasfe.even-better-toml toml格式化
serayuzgur.crates crate.io插件 自动检查依赖有没有更新

配置默认build任务:.vscode/tasks.json

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cargo",
			"command": "build",
			"problemMatcher": [
				"$rustc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"label": "rust: cargo build"
		}
	]
}

配置调试任务 .vscode/launch.json

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug",
            "program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}",
            "args": [],
            "cwd": "${workspaceFolder}",
            "console": "internalConsole",
            "preLaunchTask": "${defaultBuildTask}",
            "env": {"port":"80"}
        }
    ]
}

按F5开启调试,F5 resume调试。

Golang开发

  1. 安装golang
curl https://go.dev/dl/go1.21.11.linux-amd64.tar.gz -Lf -o /tmp/golang.tar.gz
rm -rf /usr/local/go && tar -C /usr/local -xzf /tmp/golang.tar.gz
if ! grep "go/bin" ~/.zshrc;then
  export PATH=$PATH:/usr/local/go/bin
fi
  1. 安装插件 golang.go
  2. 安装相关的依赖

参考 codespaces devcontainers go feature install.sh

The extension depends on go, gopls, dlv and other optional tools. If any of the dependencies are missing, the ⚠️ Analysis Tools Missing warning is displayed. Click on the warning to download dependencies.See the tools documentation for a complete list of tools the extension depends on.

# Install Go tools that are isImportant && !replacedByGopls based on
# https://github.com/golang/vscode-go/blob/v0.38.0/src/goToolsInformation.ts
GO_TOOLS="\
    golang.org/x/tools/gopls@latest \
    honnef.co/go/tools/cmd/staticcheck@latest \
    golang.org/x/lint/golint@latest \
    github.com/mgechev/revive@latest \
    github.com/go-delve/delve/cmd/dlv@latest \
    github.com/fatih/gomodifytags@latest \
    github.com/haya14busa/goplay/cmd/goplay@latest \
    github.com/cweill/gotests/gotests@latest \ 
    github.com/josharian/impl@latest"
(echo "${GO_TOOLS}" | xargs -n 1 go install -v )2>&1 | tee  ./init_go.log

echo "Installing golangci-lint latest..."
curl -fsSL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | \
    sh -s -- -b "$HOME/go/bin" | tee  -a ./init_go.log
  1. launch.json

**注意:**vscode好像不能正确处理软链接,所以最好不好把项目放在软链接的项目中,或者配置 substitutePath,这是我从有些高级功能可以见 debugging看到的,有些高级功能也可以在这里面找到

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Package",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "cwd": "${workspaceFolder}",
            "program": "${workspaceFolder}/cmd/${workspaceFolderBasename}",
            "output": "__debug_bin_main",
            "args": [
                "--addr=localhost:7788",
                "--refer=arloor"
                ,"--tls=true"
            ]
        },        
        {// 参考 https://blog.csdn.net/passenger12234/article/details/122930124
            "name": "Test Debug", // debug 单测
            "type": "go",
            "mode": "test",
            "request": "launch",
            "buildFlags": [],
            // "program": "${relativeFileDirname}", // 当前打开的目录
            "program": "./internal/app",
            "output": "__debug_bin_test",
            "args": [
                // "-test.run",
                // "^TestGetLlamAccessPoint$"
            ],
        },
    ]
}
  1. settings.json中golang相关配置
{
    "go.lintTool": "golangci-lint",
    "go.toolsManagement.autoUpdate": true,
    "go.formatTool": "gofmt",
    "[go]": {
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports": "always"
        }
    },
    "go.testFlags": [
        "-v",
        "-gcflags=all=-l"
    ],
    "go.formatFlags": [
        "-w"
    ],
}

Python开发

python插件

插件名 说明
ms-python.python python插件
ms-python.vscode-pylance python语言服务
ms-python.debugpy python调试插件
ms-python.pylint python lint插件 我没装这个插件,因为pylance已经有lint了

settings

    "python.analysis.inlayHints.callArgumentNames": "all",
    "python.analysis.inlayHints.functionReturnTypes": true,
    "python.analysis.inlayHints.variableTypes": true,
    "python.analysis.autoFormatStrings": true,
    "python.languageServer": "Pylance"

venv

pip3 install virtualenv&&virtualenv venv #或 python3 -m venv virEnv
source venv/bin/activate
pip3 install -r requirements.txt

cmd + shift + p,然后输入select interpreter,最后选择venv中的python解释器地址即可。

配置python调试任务

alt text

integrated terminal

我在远程开发时经常遇到重新连接远程机器时,集成terminal没有正常初始化,表现为shell标记旁边出现黄色感叹号。参考Terminal Shell Integration,发现需要关闭“自动脚本注入”,选择手动开启。具体操作是:

  1. cmd + , 打开设置,不勾选下面的选项。对应 settings.json 中的配置是"terminal.integrated.shellIntegration.enabled": false

alt text

  1. 在 zshrc 中手动开启集成terminal

Linux上

if ! grep TERM_PROGRAM ~/.zshrc;then
  echo '[[ "$TERM_PROGRAM" == "vscode" ]] && . "$(code --locate-shell-integration-path zsh)"' >> ~/.zshrc
fi

MacOS上:

if ! grep TERM_PROGRAM ~/.zshrc;then
  echo '[[ "$TERM_PROGRAM" == "vscode" ]] && . "$(/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code --locate-shell-integration-path zsh)"' >> ~/.zshrc
fi

文件自动保存+自动格式化

files.autoSave设置成延迟时,自动format无效

{
    "editor.formatOnSave": true,
    "files.autoSave": "onFocusChange",
}

其他插件

Git插件

我没有用GitLens,而是用下面两个插件,因为GitLens功能太多了,还是商业化的。

插件名 说明
GitHistory cmd + shift + p输入git log可以看完整提交历史
GitBlame 显示每行是谁在什么时候修改的

我的vscode配置备份

{
    "editor.unicodeHighlight.nonBasicASCII": false,
    "editor.fontSize": 14,
    "editor.fontFamily": "'JetBrains Mono', Menlo, Monaco, 'Courier New', monospace",
    "editor.lineHeight": 1.6,
    "editor.fontLigatures": false,
    "editor.fontVariations": false,
    "terminal.integrated.defaultProfile.linux": "zsh",
    "git.enableSmartCommit": true,
    "git.confirmSync": false,
    "update.showReleaseNotes": false,
    "editor.minimap.enabled": false,
    "git.autofetch": true,
    "terminal.integrated.fontSize": 13,
    "debug.console.fontSize": 13,
    "workbench.editor.empty.hint": "hidden",
    "files.autoSaveDelay": 200,
    "git.pullTags": false, // 不自动拉取tag,避免github action更新的tag被拉取,导致git pull失败
    "github.copilot.enable": {
        "*": true,
        "plaintext": false,
        "markdown": true,
        "scminput": false
    },
    "python.analysis.inlayHints.callArgumentNames": "all",
    "python.analysis.inlayHints.functionReturnTypes": true,
    "python.analysis.inlayHints.variableTypes": true,
    "python.analysis.autoFormatStrings": true,
    "python.languageServer": "Pylance", // python languageServer插件
    "github-actions.workflows.pinned.refresh.enabled": true, // 自动刷新被pin住的github action的执行状态,可能触发Github API的限制
    "github-actions.workflows.pinned.refresh.interval": 10,
    "remote.SSH.defaultExtensions": [
        "waderyan.gitblame",
        "donjayamanne.githistory",
        "github.copilot",
        "github.vscode-github-actions", // github actions
        "rust-analyzer", // rust-analyzer
        "codelldb",
        "tamasfe.even-better-toml",
        "serayuzgur.crates",
        "golang.go",
        "ms-python.vscode-pylance",
        "ms-python.python"
    ],
    "workbench.colorTheme": "Default Dark+",
    "rust-analyzer.check.command": "clippy",
    "window.commandCenter": false,
    "workbench.layoutControl.enabled": false,
    "go.lintTool": "golangci-lint",
    "remote.SSH.remotePlatform": {
        "bi.arloor.com": "linux",
        "pl.arloor.com": "linux"
    },
    "go.toolsManagement.autoUpdate": true,
    "diffEditor.ignoreTrimWhitespace": true,
    "debug.onTaskErrors": "showErrors",
    "diffEditor.renderSideBySide": true,
    "github.copilot.editor.enableAutoCompletions": true,
    "terminal.integrated.shellIntegration.enabled": false,
    "go.formatTool": "gofmt",
    "editor.formatOnSave": true,
    "files.autoSave": "afterDelay",
    "terminal.integrated.defaultProfile.osx": "zsh",
    "[go]": {
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports": "always"
        }
    },
    "go.testFlags": [
        "-v",
        "-gcflags=all=-l"
    ],
    "go.formatFlags": [
        "-w"
    ],
    "security.workspace.trust.untrustedFiles": "open"
}