Vim Tips – Edit Remote Files With Vim On Linux

栏目: IT技术 · 发布时间: 5年前

内容简介:Vimis one of the best, highly configurable text editor that comes with a lot of unique features that you can’t find in other text editors. Today, we will discuss one of the useful feature –For those wondering, theEditing a remote file with Vim is very simp

Vimis one of the best, highly configurable text editor that comes with a lot of unique features that you can’t find in other text editors. Today, we will discuss one of the useful feature – editing remote system’s files from the local system . Meaning – we can edit a file stored in a remote Linux system without actually having to log-in to it via SSH or any other methods. Before I knew this trick, I usually SSH into the remote system and then edit the files. However, there is also a way to edit remote files with Vim from your local system. Starting from Vim 7.x version, the netrw.vim plugin is installed as a standard plugin. This plugin allows you to edit files via ftp, rcp, scp or http.

What is Netrw plugin?

For those wondering, the Netrw (Network oriented reading, writing, and browsing) plugin supports local and remote editing, reading and writing files across networks. It also supports browsing both local and remote directories. For more details, type :help netrw inside your vim session. Let us go ahead and see how to edit files stored in a remote Linux system from our local system using Vim.

Edit Remote Files With Vim On Linux

Editing a remote file with Vim is very simple:

vim scp://<a href="/cdn-cgi/l/email-protection" data-cfemail="a9dcdaccdbe9dbccc4c6ddccdad0daddccc4">[email protected]</a>//path_to_file

Example:

I have a text file named info.txt in my remote system with the following line.

Welcome to OSTechNix

Now I am going to edit that file, and do some changes in it and then save and close the file. All from my local system!

To do so, I simply run the following command:

$ vim scp://<a href="/cdn-cgi/l/email-protection" data-cfemail="c1b2aa81f0f8f3eff0f7f9eff3f3f4eff3f3">[email protected]</a>/info.txt

Here is the visual demo of the above task:

Vim Tips – Edit Remote Files With Vim On Linux

Here, you should pay attention to the following three things.

1. [email protected] (E.g.[email protected]) – Here sk is the username of the remote system. 192.168.225.22 is the IP address of the remote system.

2. Single slash (/) – If you want to edit a file that is stored in the $HOME directory of a remote system, you must use a trailing slash to separate remote system’s IP address or hostname from the file path. In the above case, I have stored the info.txt file in $HOME directory, so I used single trailing slash.

3. // (Double slashes) – To specify full path of a file, you must use double slashes. One slash (/) is used to separate remote system’s IP address or hostname from the actual file path. And the another slash is used to mention the absolute (full) path of the remote file. For example, let us say you are editing a file named info.txt that is located in /home/sk/Documents/ directory of your remote system. In this case, the command would be:

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

Note the double slashes between remote system’s IP address and the file path. Double slashes are required only when mentioning absolute path of a remote file.

Verify the remote file’s contents from your local system:

<strong>$ ssh <a href="/cdn-cgi/l/email-protection" data-cfemail="76051d36474f445847404e58444443584444">[email protected]</a> cat info.txt</strong>
<a href="/cdn-cgi/l/email-protection" data-cfemail="43302803727a716d72757b6d7171766d7171">[email protected]</a>'s password: 
<strong>Welcome to OSTechNix blog</strong>

Vim Tips – Edit Remote Files With Vim On Linux

See? I have added an extra word “blog” in the info.txt file.

Some times, you might have changed the default SSH port for security purposes. In that case, mention the SSH port no like below.

$ vim scp://[email protected]:2200/info.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://<a href="/cdn-cgi/l/email-protection" data-cfemail="23565046516351464e4c5746505a5057464e">[email protected]</a>/path/to/file

Edit remote files within Vim session

If you are already inside a Vim session, you can then edit remote files from your local system like below.

Open the file from within Vim in a new buffer by running the following command:

:e scp://<a href="/cdn-cgi/l/email-protection" data-cfemail="71021a314048435f4047495f4343445f4343">[email protected]</a>/info.txt

Do the changes in the file and hit ESC key and type :wq to save and close the file.

Have a look at the following visual demo.

Vim Tips – Edit Remote Files With Vim On Linux

As you can see in the above output,

  • I opened Vim editor from my local system,
  • Then I opened the remote file named info.txt inside the Vim session in a new buffer,
  • And then made some changes in the file,
  • Finally, saved the changes and closed the file (ESC and :wq).

You can also open the file in a new tab by running:

:tabe scp://<a href="/cdn-cgi/l/email-protection" data-cfemail="0b78604b3a3239253a3d332539393e253939">[email protected]</a>/info.txt

Hope this helps. I will post more Vim tips in the days to come. Keep visiting!

Suggested read:


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

增长的本质

增长的本质

凯萨·伊达尔戈 / 中信出版集团股份有限公司 / 2015-11-1 / CNY 49.00

这是一本物理学家撰写得跨经济学、社会、物理学等多学科的专著。在伊达尔戈之前,从来没有人以这样的方式研究经济增长。 什么是经济增长?为什么它只发生在历史长河的一些节点上?传统意义上的解释都强调了体制、地理、金融和心理因素。而塞萨尔?伊达尔戈告诉我们,想了解经济增长的本质,还需要走出社会科学的研究,在信息、关系网和复杂性这样的自然科学中寻求答案。为了认识经济发展,塞萨尔?伊达尔戈认为我们首先需要......一起来看看 《增长的本质》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具