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:
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://firstname.lastname@example.org/info.txt
Here is the visual demo of the above task:
Here, you should pay attention to the following three things.
1. user@remotesystem (E.g. email@example.com) - 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://firstname.lastname@example.org//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 email@example.com cat info.txt firstname.lastname@example.org's password: Welcome to OSTechNix blog
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://email@example.com: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://firstname.lastname@example.org/info.txt
You might have noticed the following output at the bottom of your Vim editor:
:!scp -q 'email@example.com: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:
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.
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:
Hope this helps. I will post more Vim tips in the days to come. Keep visiting!
- Vim Tips – Read And Write Remote Files With Vim On Linux
- Execute Commands On Remote Linux Systems Via SSH