Detailed explanation of commands to read and write remote files using Vim in Linux system

Detailed explanation of commands to read and write remote files using Vim in Linux system

Set vim's working mode (temporary)

:set (mode information)
:set nu — Display line numbers:set nonu — Cancel line numbers:set mouse=a — Set mouse available:set cursorline — Set display line:help — View help

Today we will discuss a Vim usage skill - reading and writing remote files with Vim. To achieve this goal, we need to use a plug-in called netrw.vim . Starting from Vim 7.x, netrw.vim is set as a standard plugin installed by default. This plugin allows users to read, write, edit and browse remote files via protocols such as ftp, rcp, scp, http, etc.

What is the netrw plugin?

netrw (Network oriented reading, writing and browsing) plug-in not only supports editing, reading and writing files across the network between local and remote terminals, but also supports browsing local and remote terminal directories. For more detailed information about this plugin, enter help netrw in your Vim session.

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

Read and write remote files using Vim on Linux

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

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

Type q to exit the file.

To write a remote file from the local system, first open the file using the above command, then press i to enter insert mode and then write to the file. After writing the content to be written to the file, press ESC key to exit the insert mode, and then enter wq to save and exit.

The running process behind the command is actually to use the scp command to copy the remote file to the /tmp directory of the local system, and then open the file for editing. After you finish editing, the scp command copies the local edited file back to the remote system again.

To check whether the remote file content has been modified locally, use this command:

$ ssh [email protected] cat info.txt

Please note that if you want to use the absolute path to the remote terminal directory, you should use double slashes as in the following command:

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

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

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

Here, port 2200 is our custom port number. You can use your own ssh port number instead of 2200 in the above command according to the specific situation.

If you don't have an ssh/scp channel, you can use other protocols instead, as follows:

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

Reading and writing remote files in a Vim session

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

Suppose we now use the following command to open the Vim editor on the local system:

$ vim

Then you are in the Vim session. To read a remote file in the newly cached Vim session locally, just run the following command:

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

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

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

Or, enter:

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

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

:Nread ?

After reading the description file, 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 the i key to enter insert mode to write and modify files.

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

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

After writing, press the ESC key to exit editing, then enter :wq to save and exit the file.

In addition, you can also use the Nwrite command to create and write files. The usage examples are as follows:

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

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

:Nwrite ?

Summarize

This is the end of this article about using Vim to read and write remote files in Linux system. For more relevant content about using vim to read and write remote files in Linux, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed usage of Linux ipcs command and ipcrm command
  • Linux sar command usage and code example analysis
  • Use of Linux ls command
  • Use of Linux sed command
  • Use of Linux xargs command
  • Use of Linux read command
  • Use of Linux chkconfig command
  • Detailed explanation of the use of Linux time command
  • Use of Linux ln command
  • Linux cut command explained
  • Use of Linux ipcs command

<<:  Detailed explanation of MySQL slow queries

>>:  Using js to implement a number guessing game

Recommend

This article teaches you how to import CSS like JS modules

Table of contents Preface What are constructible ...

MySQL slow query pt-query-digest analysis of slow query log

1. Introduction pt-query-digest is a tool for ana...

How to install PHP7.4 and Nginx on Centos

Prepare 1. Download the required installation pac...

How to deal with time zone issues in Docker

background When I was using Docker these two days...

Query process and optimization method of (JOIN/ORDER BY) statement in MySQL

The EXPLAIN statement is introduced in MySQL quer...

Use of Linux cal command

1. Command Introduction The cal (calendar) comman...

Detailed process of building mongodb and mysql with docker-compose

Let's take a look at the detailed method of b...

Discussion on the numerical limit of the ol element in the html document

Generally speaking, it is unlikely that you will ...

Briefly explain the use of group by in sql statements

1. Overview Group by means to group data accordin...

Analysis of the usage of Xmeter API interface testing tool

XMeter API provides a one-stop online interface t...

SystemC environment configuration method under Linux system

The following is the configuration method under c...

CSS position fixed left and right double positioning implementation code

CSS Position The position attribute specifies the...

JavaScript function syntax explained

Table of contents 1. Ordinary functions 2. Arrow ...