Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Vim to read and write remote files in Linux system

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Vim to read and write remote files in Linux system". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use Vim to read and write remote files in the Linux system.

Today we discuss a technique for using Vim-reading and writing remote files with Vim. To do this, we need to use a plug-in called netrw.vim. Since Vim 7.x, netrw.vim has been set as the standard plug-in installed by default. This plug-in allows users to read, write, edit and browse remote files through ftp, rcp, scp, http and other protocols.

What is the plug-in netrw?

The netrw (Network-oriented Network oriented reading and browsing 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. To learn more about the plug-in, type help netrw in the Vim session to view it.

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

Use Vim on Linux system to read and write remote files

The method of reading and writing a remote file is almost the same as editing a remote file, you need to read a remote file from the local system, we can simply use the following command to open it:

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

Operation example

Enter Q to exit the file.

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

Operation example

The running process behind the command is actually using the scp command to copy the remote file to the / tmp directory on the local system, and then open the file for editing. After you have finished editing, the scp command copies the locally edited files back to the remote system again.

To check locally whether the contents of the remote file have actually been changed, use this command:

$ssh cirdan@192.168.225.22 cat info.txt

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

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

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

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

Port 2200 is our custom port number. You can use your own ssh port number to replace 2200 in the above command according to the specific situation.

If you do not have a ssh/scp channel, you can also use other protocols instead, as shown below:

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

Reading and writing remote files in a Vim session

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

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

$vim

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

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

In addition, you can use the Nread command as follows:

: Nread scp://cirdan@192.168.225.22/info.txt

Or, enter:

: Nread "scp://cirdan@192.168.225.22/info.txt"

Operation example

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

: Nread?

After reading the instruction document, type: Q to exit the file.

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

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

Press I to enter insert mode and you can write and modify the file.

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

: w scp://cirdan@192.168.225.22/info.txt

When you are finished, press ESC to exit editing, and then type: wq to save and exit the file.

In addition, you can create and write files with the Nwrite command, with examples of usage as follows:

: Nwrite scp://cirdan@192.168.225.22/info.txt

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

: Nwrite? At this point, I believe you have a deeper understanding of "how to use Vim to read and write remote files in the Linux system". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report