Home Vim Vim Tips – Edit Remote Files With Vim On Linux

Vim Tips – Edit Remote Files With Vim On Linux

By sk
Published: Updated: 7K views

Vim is one of the best, highly configurable text editor that comes with a lot of unique features that you can't find in other text editors. Today, we will discuss one of the useful feature - editing remote system's files from the local system. Meaning - we can edit a file stored in a remote Linux system without actually having to log-in to it via SSH or any other methods. Before I knew this trick, I usually SSH into the remote system and then edit the files. However, there is also a way to edit remote files with Vim from your local system. Starting from Vim 7.x version, the netrw.vim plugin is installed as a standard plugin. This plugin allows you to edit 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 edit files stored in a remote Linux system from our local system using Vim.

Edit remote files with Vim on Linux

Editing a remote file with Vim is very simple:

vim scp://user@remotesystem//path_to_file

Example:

I have a text file named info.txt in my remote system with the following line.

Welcome to OSTechNix

Now I am going to edit that file, and do some changes in it and then save and close the file. All from my local system!

To do so, I simply run the following command:

$ vim scp://sk@192.168.225.22/info.txt

Here is the visual demo of the above task:

Edit Remote Files With Vim On Linux

Edit Remote Files With Vim On Linux

Here, you should pay attention to the following three things.

1. user@remotesystem (E.g. sk@192.168.225.22) - Here sk is the username of the remote system. 192.168.225.22 is the IP address of the remote system.

2. Single slash (/) - If you want to edit a file that is stored in the $HOME directory of a remote system, you must use a trailing slash to separate remote system's IP address or hostname from the file path. In the above case, I have stored the info.txt file in $HOME directory, so I used single trailing slash.

3. // (Double slashes) - To specify full path of a file, you must use double slashes. One slash (/) is used to separate remote system's IP address or hostname from the actual file path. And the another slash is used to mention the absolute (full) path of the remote file. For example, let us say you are editing a file named info.txt that is located in /home/sk/Documents/ directory of your remote system. In this case, the command would be:

$ vim scp://sk@192.168.225.22//home/sk/Documents/info.txt

Note the double slashes between remote system's IP address and the file path. Double slashes are required only when mentioning absolute path of a remote file.

Verify the remote file's contents from your local system:

$ ssh sk@192.168.225.22 cat info.txt
sk@192.168.225.22's password: 
Welcome to OSTechNix blog
How To Edit Remote Files With Vim

Edit Remote Files With Vim

See? I have added an extra word "blog" in the info.txt file.

Some times, you might have changed the default SSH port for security purposes. In that case, mention the SSH port no like below.

$ vim scp://sk@192.168.225.22:2200/info.txt

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.

$ vim ftp://user@remotesystem/path/to/file

So what is happening in the background?

When you edit the remote file from your local system using the following command;

$ vim scp://sk@192.168.225.22/info.txt

You might have noticed the following output at the bottom of your Vim editor:

:!scp -q 'sk@192.168.225.22:info.txt' '/tmp/vsoDT6K/0.txt'
"/tmp/vsOLzGf/0.txt" 1L, 21C

Meaning - The scp command copies the requested file from the remote system and saves in /tmp directory of your local system and then opens it for editing. After you completed the editing, the file is uploaded back to the remote system using scp command.

Edit remote files within Vim session

If you are already inside a Vim session, you can then edit remote files from your local system like below.

Open the file from within Vim in a new buffer by running the following command:

:e scp://sk@192.168.225.22/info.txt

Do the changes in the file and hit ESC key and type :wq to save and close the file.

Have a look at the following visual demo.

Edit Remote Files Within Vim session in a new buffer

Edit Remote Files Within Vim session in a new buffer

As you can see in the above output;

  • I opened Vim editor from my local system,
  • Then I opened the remote file named info.txt inside the Vim session in a new buffer,
  • And then made some changes in the file,
  • Finally, saved the changes and closed the file (ESC and :wq).

You can also open the file in a new tab by running:

:tabe scp://sk@192.168.225.22/info.txt

Hope this helps. I will post more Vim tips in the days to come. Keep visiting!


Suggested read:


You May Also Like

5 comments

Jalal March 13, 2020 - 11:40 am

Hi,
good, good good…
Thanks a lot

Reply
Etc Etera March 13, 2020 - 3:16 pm

Nice, didn’t know that yet! Any chance to open a file as root on the server with this?

Reply
sk March 13, 2020 - 4:19 pm

As mentioned in this link, this is not possible because vim is not executing remote commands. It is simply using scp to copy the file over, edit it locally and scp it back when done.

Reply
Luiz March 14, 2020 - 7:41 pm

No more .vimrc in the remote! Thank you

Reply
healer August 20, 2021 - 12:45 pm

really,really nice!
though i use linux several years, i still learn a lot from your topic, thanks a lot!

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More