VScode使用Remote SSH进行远程开发,跳转缓慢文件解决

引言

众所周知,一些特定的编程语言可能需要Linux环境的支持,比如在golang代码中引用C语言,就需要使用到cgo,而这个环境配置需要Linux作为支撑。如果本机使用的是Windows环境,想要在Linux环境中进行代码编译、调试、部署,还有如下几种方式:

1. 搭建Windows + Linux双系统。Windows进行文档处理等办公操作,Linux进行开发、编译、调试等工作。缺点是,系统切换比较慢,有些沟通工具支持不好,比如在开发环境下,如果团队使用微信进行沟通,就可能导致信息延迟、沟通不畅等问题,从而影响开发效率。
2. 在虚拟机上安装Ubuntu或者Centos等Linux桌面系统。缺点是,虚拟机比较笨重,再在此基础上构建体量更加大的Linux系统,一方面对软硬件环境配置要求比较高,另一方面像一些数据库、接口调试等可视化工具使用不方面。
3. 使用VMware + VScode + Remote SSH远程开发工具 + Ubuntu Server无桌面版。虽然这种方式也使用了虚拟机 + Linux,但是相对使用桌面版系统,会节省不少空间。

使用Remote SSH构建远程开发环境

在VMware上安装完Ubuntu Server版后,进入VScode左侧扩展局域栏的Extension扩展市场,搜索Remote - SSH,然后进行Install安装。然后,使用ssh-keygen命令生成密钥对,用public key生成authorized_keys,再进入密钥所在目录cd ~/.ssh,使用scp命令将本地Windows生成的公钥上传到Ubuntu系统,通过如下命令生成authorized keys。

# 本地Windows系统生成公钥、私钥
ssh-keygen
cd C:\Users\xxx\.ssh\
# 使用scp命令将公钥上传远程Ubuntu的~/.ssh目录下
scp .\id_rsa.pub xxx@192.168.xx.xxx:~/.ssh/
# 使用公钥生成authorized keys
cat id_rsa.pub >> authorized_keys

然后,选择Remote SSH配置文件,一般选择C:\Users\xxx.ssh\config,配置如下字段信息

Host name-of-ssh-host-here
    User your-user-name-on-host
    HostName host-fqdn-or-ip-goes-here
    Port port-of-host
	IdentityFile C:\Users\xxx\.ssh\id_rsa  # 私钥所在路径

配置完上面的Remote SSH后,打开VScode使用Remote SSH,选择远程Ubuntu上的项目文件,即可以进行远程代码开发。
使用VScode通过Remote SSH进行远程开发时,本地开启VScode,连接上remote ssh服务器后,会在远程服务器上生产VScode服务器版本(在~/.vscode-server目录下,该目录下有VScode的二进制文件,用户及remote ssh配置文件settings.json,还有相关扩展extensions)。

无法跳转问题

如果vscode无法跳转,可能与ssh版本有关系,首先可以通过如下命令检查git版本

ssh -V
OpenSSH_for_Windows_8.1p1, LibreSSL 3.0.2

windows系统下,系统自带的某些git版本可能与vscode兼容不太好,可以将下面红框中的Remote.SSH: Path换成自己安装的ssh版本。
同时,如果Remote.SSH: Path配置不正确,也会导致vscode无法正常跳转。
在这里插入图片描述

跳转缓慢问题

Ctrl + Shift + P进行golang扩展组件的Update Install后,配置完Remote SSH开发环境,打开Ubuntu系统上的golang项目后,使用VScode查看代码,使用Ctrl + 鼠标左键进入代码时跳转很慢,查阅资料说,把语言服务器和delay延迟参数设置取消掉,发现仍然解决不了问题,最后折腾半天,在VScode插件市场重新卸载Remote SSH和golang插件,重新安装后,还是跳转缓慢,然后重置之前取消的语言服务器不勾选,即在settings.json中配置"go.useLanguageServer": true,参数为true,开启语言服务器后,跳转正常了,其完成配置如下:

{
    "remote.SSH.remotePlatform": {
        "xxx.xxx.xx.xxx": "linux",
        "Ubuntu": "linux",
        "VM-Ubuntu": "linux"
    },
    "remote.SSH.showLoginTerminal": true,
    "editor.fontSize": 19,
    "extensions.ignoreRecommendations": true,
    "go.formatTool": "goimports",
    "editor.hover.delay": 30,
    // The default for files.watcherExclude excludes node_modules and some folders under .git, but you can add other directories that you don't want VS Code to track.
    "files.watcherExclude": {
        "**/.git/objects/**": true,
        "**/.git/subtree-cache/**": true,
        "**/node_modules/*/**": true,
        "**/.hg/store/**": true
    },
    "go.goroot": "/usr/local/go",
    "go.gopath": "/home/xxx/ws/go",
    //第三方库代码提示
    "go.inferGopath": true,
    //自动完成未导入的包
    "go.autocompleteUnimportedPackages": true,
    "go.gocodePackageLookupMode": "go",
    "go.gotoSymbol.includeImports": true,
    "go.useCodeSnippetsOnFunctionSuggest": true,
    "go.useCodeSnippetsOnFunctionSuggestWithoutType": true, 
    "[go]": { 
        "editor.formatOnSave": true, 
        "editor.codeActionsOnSave": { 
            "source.organizeImports": true, 
        },    // Optional: Disable snippets, as they conflict with completion ranking.    "editor.snippetSuggestions": "none", 
    },
    "[go.mod]": { 
        "editor.formatOnSave": true, 
        "editor.codeActionsOnSave": { 
            "source.organizeImports": true, 
        },
    },
    "go.trace.server": "verbose", 
    "gopls": { 
        // Add parameter placeholders when completing a function. 
       "usePlaceholders": false, 
       // If true, enable additional analyses with staticcheck. 
       // Warning: This will significantly increase memory usage.   "staticcheck": false, 
    },
    "go.useLanguageServer": true,
    "go.languageServerFlags": [ 
        "-remote=auto", "-logfile=auto", "-debug=:0", "-rpc.trace", 
    ],
    "go.languageServerExperimentalFeatures": {},
    "tabnine.experimentalAutoImports": true,
}

PS:使用remote ssh进行远程开发,一定要打开语言服务器选项,要不然代码跳转会非常慢。

参考

真香!使用 VSCode 进行远程开发与调试
vscode远程开发配置remote ssh
Win10环境下配置VScode远程开发ssh-remote(免密登录)
配置ssh免密登录_配置vscode 远程开发+ 免密登录
goland远程开发
vscode 的 golang 提示很慢
vscode gopls 跳转太慢解决方法
解决VScode配置远程调试Linux程序的问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

love666666shen

谢谢您的鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值