VIM - Read And Write Remote Files
Reading and writing remote files are almost same as editing remote files.
To read a remote file from our local system, we simply open it using command:
vim scp://peterk@192.168.1.69/test.txt
View the remote file’s contents from your local system to verify if the file has been really modified using command:
ssh peter@192.168.1.69 cat test.txt
Please note that if you want to mention the absolute path for the directory on the remote host, use double slashes // as shown in the following command:
vim scp://peter@192.168.1.69//home/peter/Documents/test.txt
Just in case you have already changed the default SSH port for security purposes, you should explicitly mention the SSH port no like below.
vim scp://peter@192.168.1.69:2200/test.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://user@remotesystem/path/to/file
Read and Write remote files within Vim session
If you are already inside a Vim session, you can then read and write remote files using Nread (NetRead) and Nwrite (NetWrite) commands.
Open Vim editor in your local system using the following command:
vim
You’re now inside the Vim session. To read remote file from within the local Vim session in a new buffer, simply run:
:e scp://peterk@192.168.1.69/test.txt
Alternatively, you can use the “Nread” command like below.
:Nread scp://peter@192.168.1.69/test.txt
Or,
:Nread "scp://peter@192.168.1.69/test.txt"
For more details, type the following command inside the Vim session:
:Nread ?
After reading the file, type :q to exit.
Similarly, to write the remote file, run:
:e scp://peter@192.168.1.69/test.txt
Press “i” to enter into insert mode and start writing /modifying it.
You can also use :w command to create and write files. But this command will create new empty file only.
:w scp://peter@192.168.1.69/test.txt
After writing, press ESC and type :wq to save and exit.
Alternatively, use “Nwrite” command to create to write files like below.
:Nwrite scp://peter@192.168.1.69/test.txt
To know more details about Nwrite, type the following in the Vim session:
:Nwrite ?