A while ago, we discussed how to edit remote files with Vim editor on Linux. Using that method, we edited files stored on a remote system over SSH without actually having to log-in to the remote system. Today we will be discussing a similar Vim tip - read and write remote files with Vim. Starting from Vim 7.x version, the netrw.vim plugin is installed as a standard plugin by default. This plugin allows the users to read, edit, write and browse remote files via ftp, rcp, scp or http.

What is Netrw plugin?

For those wondering, the Netrw (Network oriented reading, writing, and browsing) plugin supports local and remote editing, reading and writing files across networks. It also supports browsing both local and remote directories. For more details, type :help netrw inside your vim session. Let us go ahead and see how to read or wrote files stored in a remote Linux system from our local system using Vim.

Read And Write Remote Files With Vim On Linux

Reading and writing remote files are almost same as editing remote files.

To read a remote file from our local system, we simply open it using command:

Type q to exit the file.

To write to the remote file, just open it as shown above and press "i" to enter into insert mode and finally start writing in it. Once you are done, press ESC and type :wq to save and quit.

What actually happens in the background is the remote file is copied in "/tmp" directory of your local system using scp command. And then the file is opened for editing. After you're done, the scp command copies the file back to the remote system.

View the remote file's contents from your local system to verify if the file has been really modified using command:

Please note that if you want to mention the absolute path for the directory on the remote host, use double slashes (//) as shown in the following command:

Just in case you have already changed the default SSH port for security purposes, you should explicitly mention the SSH port no like below.

Replace 2200 with your SSH port number.

If you don't have ssh/scp access, you can use other protocols, for example ftp, like below.

Read and Write remote files within Vim session

If you are already inside a Vim session, you can then read and write remote files using Nread (NetRead) and Nwrite (NetWrite) commands.

Open Vim editor in your local system using the following command:

$ vim

You're now inside the Vim session. To read remote file from within the local Vim session in a new buffer, simply run:

Alternatively, you can use the "Nread" command like below.

Or,

For more details, type the following command inside the Vim session:

:Nread ?

After reading the file, type :q to exit.

Similarly, to write the remote file, run:

Press "i" to enter into insert mode and start writing /modifying it.

You can also use :w command to create and write files. But this command will create new empty file only.

After writing, press ESC and type :wq to save and exit.

Alternatively, use "Nwrite" command to create to write files like below.

To know more details about Nwrite, type the following in the Vim session:

:Nwrite ?

Hope this helps.

Suggested read: