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

Use of Linux read command

1. Command Introduction The read command is a bui...

Vue implements image dragging and sorting

This article example shares the specific code of ...

WeChat applet implements a simple dice game

This article shares the specific code of the WeCh...

Several common methods of CSS equal height layout

Equal height layout Refers to the layout of child...

Implementation of multiple instances of tomcat on a single machine

1. Introduction First of all, we need to answer a...

Solution to the problem of mysql master-slave switch canal

After configuring VIP, the error message that app...

Solution to find all child rows for a given parent row in MySQL

Preface Note: The test database version is MySQL ...

Experience of redesigning the homepage of TOM.COM

<br />Without any warning, I saw news on cnB...

Summary of the use of Vue computed properties and listeners

1. Computed properties and listeners 1.1 Computed...

Detailed explanation of mysql deadlock checking and deadlock removal examples

1. Query process show processlist 2. Query the co...

A brief analysis of the configuration items of the Angular CLI release path

Preface Project release always requires packaging...

How to use cookies to remember passwords for 7 days on the vue login page

Problem Description In the login page of the proj...

React error boundary component processing

This is the content of React 16. It is not the la...