I am editing a bunch of files on a remote server. I can connect using SSH (public key or password). I use Vim as follows: vim scp://user@server//path/to/file.cpp I need to use Vim on my computer because it is customized, thus I don't want to use the Vim installed on the server.

I can't install anything on this remote server. To compile, I run make.

I'd like to be able to run make from Vim so that it compiles on the server. I'd like to avoid opening another terminal with a SSH connection to that server.

My question then is: is it possible to maintain a SSH connection through Vim?

Thank you

  • Have you considered scp -r ~/.vim user@example.com:? This would work well as long as your local and remote versions of Vim are reasonably close to one another. Running vim scp://... has many limitations. – Satō Katsura Oct 12 '16 at 9:59
  • Yes but I have many Vim plugins which depend on external libraries. As I said, I cannot install anything. What limitations do you see in using Vim the way I do? – Romain Pellerin Oct 12 '16 at 11:29
  • For example you won't be able to run :make. – Satō Katsura Oct 12 '16 at 11:31
  • The trick is to run :! ssh myserver 'make'. Works amazingly well with @Enzo's solution. – Romain Pellerin Oct 12 '16 at 11:54
  • Sure, but the reason most people run :make as opposed to running make at a shell's prompt is to let Vim parse errors from makeprg and subsequently open a quickfix window. :! ssh myserver 'make' doesn't allow that. shrug – Satō Katsura Oct 12 '16 at 14:06

Sure it is as long as you are on a recent OpenSSH installation on the client. You have the "control pipes" and man ssh_config will show you the magics. Basically on the client you'd create a Host entry in ~/.ssh/config like this:

Host my_remote_server # a symbolic name of your choice
  Hostname server_IP_or_DNS # the real name
  User username # the username you use for login
  IdentityFile ~/.ssh/a_suitable_ssh_key_if_any # for authentication purposes
    ControlMaster auto
    ControlPath ~/.ssh/%C # or ~/.ssh/%h-%p-%r for older versions
    ControlPersist 5m # or yes for neverexpiring persistent connection

By doing so the first connection will open a "channel" that will be kept open for the specified amount of time or forever. From now on you can refer to your ssh connection with just the name you put as Host. So, for example, instead of writing:

vim scp://username@server_IP_or_DNS//full_path/to/file.cpp

you will write:

vim scp://my_remote_server//full_path/to/file.cpp


ssh my_remote_server

To kill an existing persistent connection you need to run:

ssh -O exit my_remote_server

All this will work for plain SSH, SCP and SFTP as well. That'd be it.

  • Awesome! Works like a charm. – Romain Pellerin Oct 12 '16 at 9:30
  • You can also mod my answer up! ;-) – EnzoR Oct 12 '16 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.