In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to access shell from Vim or run external commands. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Execute external commands in Vim
Sometimes, you may need to execute external commands in the Vim editing window. For example, imagine this scenario: you have opened a file in Vim and made some changes, and then when you try to save those changes, Vim throws an error saying that you do not have sufficient permissions.
Now, quitting the current vim session and re-opening the file with sufficient permissions will mean that you will lose all your changes, so you may agree that in most cases there is not only one option. In a situation like this, the ability to run external commands inside the editor will come in handy.
We'll come back to the above use case later, but for now, let's look at how to run the basic commands in vim.
Suppose you are editing a file and want to know the number of lines, words, and characters that the file contains. To do this, in vim command-line mode, just type the colon:, followed by an exclamation point!, followed by the command to be executed (wc is used in this example), followed by the file name (using% for the current file).
:! Wc%
This is an example:
The above mentioned command line is ready to execute:
Here is the output on the terminal:
After you see the output, enter the enter key and you will return to your vim session.
This feature is really convenient when you are writing code or script and want to know as soon as possible whether the code or script contains compile-time or syntax errors.
Continue, if the requirement is to add output to a file, use: read! Orders. Here is an example:
: read! Wc%
The read command inserts the output of the external command as a new line below the current line in the edited file. If you prefer, you can also specify that a specific line number output will be added after a specific line.
For example, the following command will add the output of wc after the second line of the file.
: 2read! Wc%
Note: use $to insert on the last line and 0 to insert before the first line.
Now, going back to one of the use cases we discussed at the beginning, the following command will help you save the file without having to close the file first (which means that the unsaved content will not be lost) and then use the sudo command to reopen it.
: w! Sudo tee%
Access shell in Vim
In addition to executing separate commands, you can also put your newly created shell in vim. To do this, what you have to do in the editor is to run the following command:
: shell
Or:
: sh
When you have finished performing your shell task, type exit which will take you back to the Vim session where you left.
Loopholes to keep in mind
Although in the real world, the shell that can be accessed is absolutely consistent with their user rights, it can also be used to elevate rights. As we explained in an earlier article (on sudoedit), even if you give a user the right to sudo only to edit a file through Vim, they can still use this technology to run a new shell from the editor, and they can do everything root users or administrator users can do.
Thank you for reading! This is the end of the article on "how to access shell from Vim or run external commands". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.