当前位置:网站首页>VIM from dislike to dependence (20) -- global command
VIM from dislike to dependence (20) -- global command
2022-06-27 08:24:00 【aluluka】
In the previous article , We introduced how to find and replace , Substitution is a simple application based on search , It just changes the matching text to another . that vim What else can you do with the text on the match in ? In this article, we will discuss this problem .
First time to know global command
The key to our ability to perform other operations on rows with matches is global command .global The function of the command is to execute the specified... On the line where there is a match ex command . The format of the command is as follows :
:[range] g[lobal][!]/{pattern}/[cmd]
- With most ex command , It receives a scope . If no scope is given , By default, it will be applied to the entire file , That is, its default range is %
- ! Represents reverse , Is executed on a line where no match exists ex command
- pattern Represents a match pattern
- cmd Indicates which... Will be executed on the corresponding text ex command . If it is not specified, it will execute by default print command
What needs to be emphasized here is , perform ex The command operates on a line with a match . The operation is not highlighted text , It's a line with highlighted text .
Let's use a simple example to demonstrate how to use
#define VERSION "v1.0.1"
char pszVersion[] = VERSION;
#define TITLE "vim"
char* pszTitle = TITLE;
#define AUTHOR "Bram Moolenaar"
char* pszAuthor = AUTHOR;
Suppose there's a piece of code , We use first ".*" To match a string , And then execute :g//d To perform the delete operation .
We found that instead of deleting the following strings, it deleted all lines with strings , Only assignment statements are preserved . I believe that through this example, you should have understood global How commands work .
If we want to delete all assignment statements in the above code , You can use ! To reverse ,:g!//d
stay 《vim Practical skills 》 This book mentions a very interesting thing . Through the above description , It can be concluded that global A short form of a command :g/re/p among g yes global An abbreviation for a command ,re For regular expressions regular expression, and p It is print Abbreviation . We will / If you remove it, you will find that the abbreviation becomes grep The word . This is the same. grep The origin of this order .
The above contents have been preliminarily introduced global Use of commands , Let's look at other usage scenarios
Used in conjunction with the buffer parameter list
We still use neovim Configuration file for . We randomly in part lua Add some to the file TODO Notes . It means that the function that will be realized in the future has not been done for the time being .
Let's look in a file first TODO word , It doesn't matter that some documents show that they are not found , Just to save the schema
And then put all the lua File is added to the parameter list ,:args **/.*lua
Then select clear a register qaq , among qa Means we are going to use a Register to record a macro , Use directly without entering anything q The end of the recording . Because a macro writes the contents of an operation into a register , So a macro that does nothing can clear the contents of a register .
And then execute :argdo g//yank A Use here yank This command copies the contents to the register . In addition, I used A instead of a Because the command is executed for each file in turn , It needs to be added to the register one by one . So we use capital letters here .
Now you can view a The contents of the register , It is found that there is already corresponding content .
One disadvantage of the extracted content is that it cannot display the specific file todo term . Functions that will be implemented in the future but are not implemented now are used TODO Describing it is a common habit in programming , There are many good plug-ins for this function , Related plug-ins will be introduced later .
Appoint ex The scope of execution of the order
Not only global You can specify the range , It's followed by cmd You can also specify a range , The following will demonstrate how to use
Suppose there is a paragraph css Code
html{
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
body{
line-high: 1.5;
color: black;
background: white;
}
We'd like to give each group css The internal attributes are sorted alphabetically .
The first way : You can record macros to automate . First, match by pattern { That is to use /{ Then start recording macros : nvi{:sort , First, through n To jump to the next match , And then use vi{ Select by text object {} The content in , Finally, execute for the selected sort command 

But we can use global The command does the same . First, construct the corresponding schema . We can go through :g/{/ .+1,/}/-1 sort Such a command to complete this operation . Let's analyze the meaning of this command .
- First, follow this command
/It is divided into 3 Parts of , The first part is{Means to match all{What follows . - The second part is
.+1,/}/-1. It represents a range , The scope is divided into two parts , With,Division , The previous section represents the next line of the current line , That is to say{The next line of the line ,/}Indicates the end of the match}Symbol , Followed by a-1Express}On the previous line , This range represents{}All the lines between . - The last part is the command
sort, Execute in the corresponding scopesortcommand .
For the above explanation , We can global Command to make a detailed use of the format :
:[range]/g[lobal][!]/{start pattern}/ .{offsize},/{end pattern}/{offsize} [cmd]
Here we can also follow other orders , For example C Indent the code in the function , Then you can use it :g/{/ .+1,/}/-1 > .
边栏推荐
- The IPO of Yefeng pharmaceutical was terminated: Yu Feng, the actual controller who had planned to raise 540million yuan, made P2P investment
- MySQL环境变量配置的教程
- Zabbix部署说明(Server+Win客户端+交换机(H3C))
- 分析日志.log
- Redis配置文件详解
- 【12. 最大连续不重复子序列】
- Coggle 30 days of ML July competition learning
- 2022.06.26(LC_6101_判断矩阵是否是一个 X 矩阵)
- LVGL GUI GUIDER移植代码到STM32
- 直接修改/etc/crontab 文件内容,定时任务不生效
猜你喜欢

Refer to | the computer cannot access the Internet after the hotspot is turned on in win11

SPARQL basic introductory exercise

lvgl 说明3关于lvgl guider的使用

vim 从嫌弃到依赖(20)——global 命令

Redis的持久化机制

win命令行中导入、导出数据库相关表

Lvgl description 3 about the use of lvgl Guide

准备好迁移上云了?请收下这份迁移步骤清单

Experience record of Luogu's topic brushing

盲測調查顯示女碼農比男碼農更優秀
随机推荐
AQS underlying source code of concurrent programming JUC
MATLAB小技巧(19)矩阵分析--主成分分析
100% understanding of 5 IO models
MySQL lock details
lvgl 说明3关于lvgl guider的使用
洛谷刷题心得记录
Experience record of Luogu's topic brushing
L'introduction en bourse de Wild Wind Pharmaceutical a pris fin: Yu pinzeng, qui avait l'intention de lever 540 millions de RMB, a effectué un investissement P2P.
Redis的事务
期货反向跟单靠谱吗?
参考 | 升级 Win11 移动热点开不了或者开了连不上
[batch dos-cmd command - summary and summary] - environment variables, path variables, search file location related instructions - set, path, where, what if there are spaces in the path parameters of
JS EventListener
c的时间函数算效率
sql注入之order by注入
【云原生】2.3 Kubernetes 核心实战(上)
Analysis log log
Recognize the ordering of O (nlogn)
Win10 how to manage startup items?
vim 从嫌弃到依赖(19)——替换