27

I use GVIM on Ubuntu 9.10. I'm looking for the right way to configure GVIM to be able to edit remote files (HTML, PHP, CSS) by for exemple ftp.

  • When i use :e scp://username@remotehost/./path/to/file i get: error detected while processing BufEnter Auto commands for "*":E472: Command failed.
  • When i open a file on remote via Dolphin or Nautilus, i cannot use other files with NERDTree.
  • Finally when i edit on remote a file via Dolphin the rights are changing to access interdit.

So how to use GVIM to edit remote files like on my localhost?

pkaeding
  • 36,513
  • 30
  • 103
  • 141
LaTulipe
  • 271
  • 1
  • 3
  • 3
  • Have you tried mapping a key to a function which takes the location as an arg an returns a call to scp with the location and a temp directory and then attempts to open it? You could augment this by binding a second key to write and them scp the file back. – tzenes Jan 25 '10 at 23:38

10 Answers10

52

I've found running the filesystem over ssh (by means of sshfs) a better option than having the editor handle that stuff or running the editor itself over an ssh tunnel.

So you need to

apt-get install sshfs

and then

sshfs remoteuser@remotehost:/remote/path /local/mountpoint

And that will let you edit your remote files as if they were on your local file system.

To make it even smoother you can add a line to /etc/fstab

sshfs#remoteusername@remotehost:/remote/path /local/mountpoint fuse user,noauto

For some reason I find that I have to use fusermount -u /local/mountpoint rather then just umount /local/mountpoint when experimenting with this. Maybe that's just my distro.

Recently I've also noted that the mounting user must be in the fuse group. So:

sudo addgroup <username> fuse

An other popular option of course, would be to run vim (rather then gvim) inside a GNU Screen session on one machine and connect to that session via ssh from wherever you happen to be. Code along all day at work and in the evening you ssh into your office computer, reattach to your gnu screen session and pick up exactly where you left off. I used find the richer color palette to be the only thing I really missed from gvim when using vim, but that can actually be fixed thanks to a fork of urxvt that will let you customize the entire 256 position color palette, not just the 16 first positions of the palette that most terminal emulators will let you customize.

Community
  • 1
  • 1
oivvio
  • 3,026
  • 1
  • 34
  • 41
  • as far as vim colors go, try looking here: http://vim.wikia.com/wiki/256_colors_in_vim – Christian Oudard Feb 25 '11 at 14:43
  • You can get 256 colors in a lot of terminal emulators, but the palette (beyond the first 16 colors) is fixed, so most gvim color themes will still not look that great in vim. However there's a brand new [rxvt-unicode fork](https://github.com/trapd00r/rxvt-unicode) that will let you create a custom palette. I still haven't really tried it out, but it looks promising. – oivvio Mar 02 '11 at 14:24
  • 1
    I've been using it for over 20 years, and I still find things that make me say 'Wow, I didn't know you could do that—I Linux!' – Michael Scheper Aug 19 '16 at 00:15
9

There is one way and that is using the remote host's copy, using SSH to forward the X11 client to you, like so:

user@local:~/$ ssh -X user@host
...
user@host:~/$ gvim file

The latter command should open gvim on your desktop. Of course, this relies on the remote host having X11 / gnome / gvim installed in the first place, which might not be the solution you're looking for / an option in your case.

Note: X11 forwarding can be a security risk.

  • Is there a way to some sort of caching? If you are on a slow network, it can be a pain to remote develop. – Begui Feb 08 '12 at 22:57
7

Try this

:e scp://username@remotehost//path/to/file

Note that the use of // is intentional after remotehost it gives the absolute path of your file :)

http://www.celsius1414.com/2009/08/19/how-to-edit-remote-files-with-local-vim/

stack karma
  • 96
  • 1
  • 2
6

In order for netrw to work seamlessly, I believe you need to not be in compatibility mode.

Try

:set nocompatible

then

:edit scp://host/path/to/file

Bill B
  • 1,280
  • 13
  • 16
2

The vim tips wiki has an article on this, Editing remote files via scp in vim.

1

EDIT: Key authentication is not necessary for opening files over ssh. Vim will prompt for password.

It would be useful to note if netrw.vim was loaded by vim when it started.

:echo exists("g:loaded_netrwPlugin")

For opening files over ssh, you need your local machine's public key in the server's authorized keys. Following help section in vim documentation explains it pretty well.

:help netrw-ssh-hack

Quick way to export public key would be by using ssh-copy-id (if available).

ssh-copy-id user@host

And have a look at netrw documentation for network file editing over other protocols.

:help netrw

HTH.

shoban
  • 650
  • 1
  • 9
  • 14
1

About your command, :e scp://username@remotehost/./path/to/file : note that with netrw, scp is taken relative to your home directory on that remote host. To avoid home-relative pathing, drop that "."; ie. :e scp://username@remotehost//path/to/file .

Chip Campbell
  • 106
  • 1
  • 1
0

According to the docs BufEnter is processed after the file has been read and the buffer created, so my guess is that netrw successfully read the file but you have a plugin that assumes the file is on the local filesystem and is trying to access it, e.g. to run ctags.

Try disabling all your plugin scripts except the default Vim ones, and then editing the file.

Also, try editing a directory to see if netrw can read that - you need to put the / on the end so that netrw knows it is a dir.

Dave Kirby
  • 25,806
  • 5
  • 67
  • 84
  • I disabled the plugin scripts except the default Vim ones, "scp://username@remotehost/path/to/dir/" and i get:" No lines in buffer" – LaTulipe Jan 26 '10 at 08:49
-1

to accomplish this on windows download/install the Dokan library and Dokan SSHFS, which are the first and last links on this page.

necromancer
  • 23,916
  • 22
  • 68
  • 115
-2

I didn't think you were going to be able to directly edit a remote file using GVIM running locally. However, as others have pointed out, this is defintiely possible. This looks very interesting; I will check this out. I will leave the rest of my post up here, in case it is useful to anyone else, as an alternative method. This method will work even if you don't have SSH access to the file (ie, you only have FTP, or S3, or whatever).

You may get that effect, though, by tying GVIM into a graphical file transfer application. For example, on OS X, I use CyberDuck to transfer files (FTP, SFTP, etc). Then, I have it configured to use GVIM as my editor, so I can just double-click on a file in the remote listing, and CyberDuck will download a copy of that remote file, and open it in GVIM. When I save it in GVIM, CyberDuck uploads the file back to the remote host.

I'm sure that this functionality is not unique to CyberDuck, and is probably present in most nicer file transfer utilities.

pkaeding
  • 36,513
  • 30
  • 103
  • 141
  • I tried it with FileZilla and this workaround is better than what i did before. I'll check out the others tomorrow. – LaTulipe Jan 25 '10 at 23:54
  • I'm glad that was useful to you. As it looks like you are new to this site, please consider up-voting any answers that you think are helpful, and marking the best answer as accepted (if it solved your problem). Check out the FAQ link at the top of the page if you are unsure about how to do this. Thanks! – pkaeding Jan 26 '10 at 00:38
  • 1
    This is incorrect. You can definitely edit a remote file using gvim running locally. As others have mentioned :help netrw will tell you more – Michael Anderson Jan 26 '10 at 04:10
  • Well, I stand corrected :). I will look into this netrw method, this looks very interesting. – pkaeding Jan 26 '10 at 18:22