内容简介: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:
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>
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.
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:
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Python语言程序设计
[美]梁勇(Lang Y. D.) / 李娜 / 机械工业出版社 / 2015-4 / 79.00元
本书采用“问题驱动”、“基础先行”和“实例和实践相结合”的方式,讲述如何使用Python语言进行程序设计。本书首先介绍Python程序设计的基本概念,接着介绍面向对象程序设计方法,最后介绍算法与数据结构方面的内容。为了帮助学生更好地掌握相关知识,本书每章都包括以下模块:学习目标,引言,关键点,检查点,问题,本章总结,测试题,编程题,注意、提示和警告。 本书可以作为高等院校计算机及相关专业Py......一起来看看 《Python语言程序设计》 这本书的介绍吧!