共191行
2026-04-21 11:55:13
我有两台电脑分别是:A: Ubuntu,B: MacOSX。在A和B上都装上最新版的neovim,介绍一下,如何在B上通过neovim的LSP连接A上的环境呢?
要在MacOSX(B)上通过Neovim的LSP连接Ubuntu(A)上的环境,需要先在两台电脑上安装最新版Neovim,然后配置SSH连接,最后选择合适的远程LSP方案。以下是详细步骤:
通过APT安装(稳定版但可能不是最新):
sudo apt update
sudo apt install neovim通过Snap安装(获取较新版本):
sudo snap install nvim --classic下载AppImage(获取最新版本):
curl -LO https://github.com/neovim/neovim/releases/latest/download/nvim.appimage
chmod u+x nvim.appimage
sudo mv nvim.appimage /usr/local/bin/nvim通过Homebrew安装(推荐):
brew install neovim下载预编译二进制:
# 根据芯片选择x86_64或arm64版本
curl -LO https://github.com/neovim/neovim/releases/latest/download/nvim-macos-x86_64.tar.gz
tar xzf nvim-macos-x86_64.tar.gz
sudo mv nvim-macos-x86_64/bin/nvim /usr/local/bin/验证安装:在两台电脑上运行 nvim --version 确认安装成功。
确保从MacOSX(B)可以SSH连接到Ubuntu(A):
# 在B上测试连接
ssh user@ubuntu-ip-address如果使用密钥认证,确保公钥已添加到A的 ~/.ssh/authorized_keys 中。
有几种主流方案可选:
在B上安装coc.nvim插件:
" 使用vim-plug安装
Plug 'neoclide/coc.nvim', {'branch': 'release'}配置coc.nvim连接远程LSP:
在 ~/.config/nvim/coc-settings.json 中添加:
{
"remote.SSH.enable": true,
"remote.SSH.server": "user@ubuntu-ip-address",
"remote.SSH.configPath": "~/.ssh/config",
"workspace.folder": "/path/to/remote/project"
}在A上安装语言服务器:
# 例如安装Python语言服务器
npm install -g pyright
# 或安装TypeScript语言服务器
npm install -g typescript-language-server在A的coc-settings.json中配置LSP路径:
{
"languageserver": {
"python": {
"command": "pyright-langserver",
"args": ["--stdio"],
"filetypes": ["python"]
}
}
}在B上安装插件:
-- 使用lazy.nvim
{
"amitds1997/remote-nvim.nvim",
dependencies = {
"nvim-lua/plenary.nvim",
"MunifTanjim/nui.nvim",
"nvim-telescope/telescope.nvim"
},
config = true
}配置远程连接:
require('remote').setup({
connections = {
{
name = "ubuntu-dev",
host = "ubuntu-ip-address",
user = "your-username",
port = 22,
path = "/path/to/project"
}
}
})通过命令连接:
:RemoteOpen ubuntu-dev安装插件:
Plug 'chipsenkbeil/distant.nvim'配置远程连接:
require('distant').setup({
mode = "ssh",
host = "ubuntu-ip-address",
user = "your-username"
})在A上启动LSP服务器并监听端口:
# 例如启动PHP语言服务器
php-language-server --tcp=0.0.0.0:2080 --memory-limit=4G在B的Neovim配置中连接远程LSP:
-- 使用nvim-lspconfig
require('lspconfig').php.setup({
cmd = { 'php-language-server', '--tcp=ubuntu-ip-address:2080' }
})检查LSP状态:
:LspInfo测试功能:
针对远程连接的高延迟环境:
"suggest.timeout": 5000"codeLens.enable": false"suggest.autoTrigger": "none"推荐使用coc.nvim方案,因为它提供了最完整的远程开发体验,包括文件浏览、LSP集成和配置同步。remote-nvim.nvim也是一个很好的选择,特别适合需要完整远程编辑环境的用户。
关键点:
ServerAliveInterval 防止断开:checkhealth 检查各项功能状态这样就能在MacOSX上享受Ubuntu开发环境的完整LSP功能,包括代码补全、定义跳转、错误检查等。