Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The Linux system uses Vim to read and write remote files


Jun 01, 2021 Article blog


Table of contents


In this article, we'll talk about the use of The Linux System Vim -- reading and writing remote files. Before we can do this, we'll install a plug-in called netrw.vim

What is a plug-in netrw?

netrw (web-oriented read and write and browse Network oriented reading, writing and browsing) plug-in not only supports local and remote terminals to edit, read and write files across the network, but also supports browsing local and remote terminal directories. For more details on this plug-in, you can view it by help netrw in the Vim session.

Let's take a look at how to use Vim to read and write files stored on a remote Linux system from your local system.

Use Vim on the Linux system to read and write remote files

Reading and writing a remote file is almost the same as editing a remote file, requiring a remote file to be read from the local system, and we can simply open it with this command:

$ vim scp://[email protected]/info.txt

 The Linux system uses Vim to read and write remote files1

Enter q to exit the file.

To write a remote file from the local system, you also open the file with the command above, then press i to enter insert mode and then you can write to the file. After you have finished writing what you need to write to the file, press ESC to exit insert mode, and then enter wq to save and exit.

 The Linux system uses Vim to read and write remote files2

The running process behind the command is actually to copy the remote file to the /tmp directory of the local system with the scp command, and then open the file edit. After you've edited it, scp command copies the locally edited file back to the remote system again.

To see locally whether the contents of the remote file have really been altered with this command:

$ ssh [email protected] cat info.txt

Note that if you want to use the absolute path of the remote terminal directory, you should use double slashes like the following command:

$ vim scp://[email protected]//home/cirdan/Documents/info.txt

If you have changed SSH port for security reasons, you should explicitly describe the SSH port number as follows:

$ vim scp://[email protected]:2200/info.txt

Here port 2200 is our custom port number, so you can replace the 2200 in the above command with your own ssh port number on a case-by-case basis.

If you don't have a channel for ssh/scp you can also replace it with another protocol, as follows:

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

Read and write remote files in a Vim session

If you have entered a Vim session, you Nread use the NetRead and Nwrite commands to read and write remote files.

Suppose we now open the Vim editor of the local system with the following command:

$ vim

Then you enter the Vim session and read a remote file in the Vim session in the new local cache, all you need to do is run the following command:

:e scp://[email protected]/info.txt

In addition, you can use Nread command as follows:

:Nread scp://[email protected]/info.txt

Alternatively, enter:

:Nread "scp://[email protected]/info.txt"

 The Linux system uses Vim to read and write remote files3

To learn more about this command, enter the following command in the Vim session:

:Nread ?

After reading the documentation, enter :q to exit the file.

Similarly, to write to a remote file, you should first use the following command:

:e scp://[email protected]/info.txt

Press i to enter insert mode to write and modify files.

You can also create and write files with :w but this command can only create a new empty file:

:w scp://[email protected]/info.txt

Once you're done, press ESC to exit editing, and then :wq to save and exit the file.

In addition, you can create and write files with Nwrite command, as follows:

:Nwrite scp://[email protected]/info.txt

For more information about the Nwrite command, enter the following information in the Vim session:

:Nwrite ?

These are the tips for using Vim in Linux systems, and I hope it will help you, and students interested in Linux can take a look at the tutorial

Linux tutorial: https://www.w3cschool.cn/linux/

Linux Microsyscope: https://www.w3cschool.cn/minicourse/play/linuxcourse

Linux should learn this: https://www.w3cschool.cn/linuxprobe/