14

Vim Tips – Edit Remote Files With Vim On Linux

 4 years ago
source link: https://www.ostechnix.com/vim-tips-edit-remote-files-with-vim-on-linux/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Vimis 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://<a href="/cdn-cgi/l/email-protection" data-cfemail="a9dcdaccdbe9dbccc4c6ddccdad0daddccc4">[email protected]</a>//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://<a href="/cdn-cgi/l/email-protection" data-cfemail="c1b2aa81f0f8f3eff0f7f9eff3f3f4eff3f3">[email protected]</a>/info.txt

Here is the visual demo of the above task:

Edit-Remote-Files-With-Vim.gif

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

1. [email protected] (E.g.[email protected]) – 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://[email protected]//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:

<strong>$ ssh <a href="/cdn-cgi/l/email-protection" data-cfemail="76051d36474f445847404e58444443584444">[email protected]</a> cat info.txt</strong>
<a href="/cdn-cgi/l/email-protection" data-cfemail="43302803727a716d72757b6d7171766d7171">[email protected]</a>'s password: 
<strong>Welcome to OSTechNix blog</strong>

How-To-Edit-Remote-Files-With-Vim.png

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 protected]: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://<a href="/cdn-cgi/l/email-protection" data-cfemail="23565046516351464e4c5746505a5057464e">[email protected]</a>/path/to/file

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://<a href="/cdn-cgi/l/email-protection" data-cfemail="71021a314048435f4047495f4343445f4343">[email protected]</a>/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.gif

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://<a href="/cdn-cgi/l/email-protection" data-cfemail="0b78604b3a3239253a3d332539393e253939">[email protected]</a>/info.txt

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

Suggested read:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK