remote-sshfs.nvim

April 4, 2023 • 7 minute read • 1372 words

Introducing remote-sshfs.nvim: a Neovim plugin for remote development

Remote-sshfs.nvim is a Neovim plugin that allows users to explore, edit, and develop on a remote machine via SSHFS with Neovim. This plugin is loosely based on the VSCode extension Remote-SSH, but it does not install packages on the remote server. Instead, it conducts finds, greps, etc. over SSH and accesses files via SSHFS.

Installation

This plugin is currently in alpha/unstable stage and may break or change frequently. However, for those who are interested in trying it out, this plugin requires Neovim (v0.7.0) or the latest neovim nightly commit to work due to its dependencies.

Installation is simple and can be done through a variety of package managers:

Using vim-plug

Plug 'nosduco/remote-sshfs.nvim'

Using dein

call dein#add('nosduco/remote-sshfs.nvim')

Using packer.nvim

use {
'nosduco/remote-sshfs.nvim',
requires = { {'nvim-telescope/telescope.nvim'} } -- optional if you declare plugin somewhere else
}

Using lazy.nvim

-- init.lua:
{
'nosduco/remote-sshfs.nvim',
}

-- plugins/telescope.lua:
return {
'nosduco/remote-sshfs.nvim',
dependencies = { 'nvim-telescope/telescope.nvim' } -- optional if you declare plugin somewhere else
}

Once installed and setup, the plugin can be loaded with Telescope using the command require('telescope').load_extension 'remote-sshfs'.

Getting Started

One of the key features of this plugin is its ability to connect and mount a remote host via SSHFS using the :RemoteSSHFSConnect command. This command triggers a picker that allows users to select hosts parsed from their SSH config files. Once a host is selected, remote-sshfs mounts the host and changes the current working directory to that folder. When the user closes vim, the mount is automatically unmounted and cleaned.

In addition to the :RemoteSSHFSConnect command, the plugin also allows users to disconnect from a remote host using the :RemoteSSHFSDisconnect command. Users can also select an SSH config to edit via a picker using the :RemoteSSHFSEdit command.

The plugin also includes the ability to use Telescope Find Files and Live Grep functionality completely remote via SSH using the :RemoteSSHFSFindFiles and :RemoteSSHFSLiveGrep commands. However, it is important to note that the remote server must have either ripgrep, fd/fdfind, or the where command installed for this feature to work.

For convenience, it is recommended to set up key mappings for these commands using Lua. Users can also customize remote-sshfs.nvim to their liking using the setup structure provided in the README.

Customization

The setup structure provided in the README allows users to customize remote-sshfs.nvim to their liking. Here is an example setup with the default config:

require('remote-sshfs').setup{
connections = {
ssh_configs = { -- which ssh configs to parse for hosts list
vim.fn.expand "$HOME" .. "/.ssh/config",
"/etc/ssh/ssh_config",
-- "/path/to/custom/ssh_config"
},
sshfs_args = { -- arguments to pass to the sshfs command
"-o reconnect",
"-o ConnectTimeout=5",
},
},
mounts = {
base_dir = vim.fn.expand "$HOME" .. "/.sshfs/", -- base directory for mount points
unmount_on_exit = true, -- run sshfs as foreground, will unmount on vim exit
},
handlers = {
on_connect = {
change_dir = true, -- when connected change vim working directory to mount point
},
on_disconnect = {
clean_mount_folders = false, -- remove mount point folder on disconnect/unmount
},
on_edit = {}, -- not yet implemented
},
ui = {
select_prompts = false, -- not yet implemented
confirm = {
connect = true, -- prompt y/n when host is selected to connect to
change_dir = false, -- prompt y/n to change working directory on connection (only applicable if handlers.on_connect.change_dir is enabled)
},
},
log = {
enable = false, -- enable logging
truncate = false, -- truncate logs
types = { -- enabled log types
all = false,
util = false,
handler = false,
sshfs = false,
},
},
}

This structure provides users with the ability to specify which SSH configs to parse for hosts, which arguments to pass to the sshfs command, and more. By default, remote-sshfs.nvim will change the Vim working directory to the mount point when connected and will not clean mount point folders on disconnect/unmount. Users can also enable logging, specify which types of logs to enable, and more.

Commands

Here's a list of the available commands:

  • :RemoteSSHFSConnect: Use this command to open the host picker and connect to a remote host (parsed from ssh configs)
  • :RemoteSSHFSDisconnect: Use this command to disconnect from a connected host
  • :RemoteSSHFSEdit: Use this command to open the ssh config picker to open and edit ssh configs
  • :RemoteSSHFSFindFiles: Use this command to initiate a telescope find files window which operates completely remotely via SSH and will open buffers referencing to your local mount
  • :RemoteSSHFSLiveGrep: Use this command to initiate a telescope live grep window which operates completely remotely via SSH and will open buffers referencing to your local mount

Conclusion

Remote-sshfs.nvim is a Neovim plugin that provides a seamless way for users to work on remote machines via SSHFS with Neovim. With its easy installation process, customizable setup structure, and available commands, users can easily connect and disconnect from remote hosts, edit files remotely, and utilize Telescope Find Files and Live Grep functionality completely remote via SSH.

While still in alpha/unstable stage, remote-sshfs.nvim shows a lot of promise and has the potential to become a valuable tool for remote development with Neovim. With integrations being developed and contributions from the community, we look forward to seeing how this plugin continues to evolve and improve.

If you're interested in trying out remote-sshfs.nvim for yourself, head over to the GitHub repository and give it a go!