当前位置:网站首页>Small exercise of batch replacement with VIM regular expression
Small exercise of batch replacement with VIM regular expression
2022-07-21 19:01:00 【A little confused】
Last time we used macro operation in VIM A series of similar codes are generated in batches from a line of code , The text is here
This time we use regular expressions to realize batch replacement
Post the last exercise first
stay GVIM Next , The contents of the following figure

Change to the following

And it is pointed out that , Use batch operation , You cannot type line by line
Before giving the implementation method , Given first VIM The meaning of various symbols in
Ordinary character | meaning |
---|---|
. | Matches any character |
[abc] | Match any one of the characters in the formula brackets , have access to - Indicates the range, such as [a-z0-9] Match lowercase letters and numbers |
[^abc] | Match characters other than those in square brackets |
\d | Match Arabic numerals , Equate to [0~9] |
\D | Match any character other than Arabic numerals , Equate to [^0~9] |
\x | Match hexadecimal numbers , Equate to [0-9A-Fa-f] |
\X | Matches any character other than a hexadecimal digit , Equate to [ ^0-9A-Fa-f] |
\w | Match word letters |
\W | Match any character other than the letter of the word |
\t | matching Tab character |
\s | Match blanks , Equate to [\t] |
\S | Match non whitespace , Equate to [ ^\t] |
Special characters | meaning |
---|---|
\ * | matching * character |
\ . | matching . character |
\ / | matching / character |
\ \ | matching \ character |
\ [ | matching [ character |
A metacharacter representing a quantity | meaning |
---|---|
* | matching 0~ Any one |
\ + | matching 1~ Any one |
\ ? | matching 0~1 individual |
\ {n,m} | matching n ~ m individual |
\ {n} | matching n individual |
\ {n,} | matching n ~ Any one |
\ {,m} | matching 0 ~ m individual |
Represents the position metacharacter | meaning |
---|---|
$ | Match the end of the line |
^ | Match the beginning of the line |
\ < | Match the beginning of the word |
\ > | Match word endings |
Now back to that little exercise , Because we mainly use regular to replace , So let's copy and paste the first line
In normal mode
gg // Back to the first line
yy // Copy the first line
19p // Paste 19 Time

Then it became like this
Before replacing , introduce VIM The way to replace
The replacement format is
:s/a/b/g // take a Replace with b
//s yes substitute,g yes global
We first gg Back to the first line , Then look and add g And no addition g The difference between
The command line executes the following
:s/1/aa/

You can see , Only the first of the first line 1
Turned into aa
Then execute the following
u //undo, Undo the last action
:s/1/aa/g

You can see all in the first line at this time 1
It's all replaced by aa
Although add g
Put the first line 1
All replaced with aa
, But only the first line has been replaced , If multiple lines need to be replaced , We can't do this on every line , So it introduces %
Let's execute the following content to see what the result is
u // Undo the last action
:%s/1/aa/

You can see the first in each line at this time 1
It's all replaced by aa
If we just want to replace a few lines , Can be %
Change to range
:5,8s/1/aa/

At this time only 5-8 Yes 1
Has been replaced by
Because the content of our little exercise is not fixed , So we need to introduce another function
\=line(".")
This function means to get the line number , And you can add, subtract, multiply , If you want to add n, It only needs
\=ling(".")+n
Try it first , Put the first line 2
Change to line number ×10
:s/2/\=line(".") * 10/

First line 2
Success was replaced by 10
The preparations are over , The regular expression of batch change is given directly below
:%s/\d/\=line(".")/
:%s/\d$/\=line(".") + 1/
The result of executing the first instruction

Then the result of executing the second instruction

Be accomplished
Finally, let's talk about the meaning of these two instructions
First is the first instruction
:%s/\d/\=line(".")/
%s: Replace all lines
\d: The first number
\=line("."): Get line number
Second instruction
:%s/\d$/\=line(".") + 1/
%s: Replace all lines
\d$: The last number ,$ Is a positional metacharacter , Indicates row position
\=line(".") + 1: Get the line number and + 1
边栏推荐
猜你喜欢
Description du développement de l'ESB en combinaison avec la plateforme UMC Cloud
Okaleido tiger NFT is about to log in to binance NFT platform, and the era of NFT rights and interests is about to start
Flyter icons built-in icon library materialicons Encyclopedia
第一部分—C语言基础篇_9. 复合类型(自定义类型)
用VIM正则表达式进行批量替换的小练习
Hongmeng harmonios deveco studio reported an error unistall_ FAILED_ INTERNAL_ ERROR
STM32 OLED显示屏移植工程方法
苹果公司发布watchOS 8.7 包含错误修复和安全更新
Obtain the screenshot of the front panel through programming in LabVIEW
bryntum gantt 5.0.6
随机推荐
Ardunio开发——土壤传感器的使用
Interface idempotency
教程篇(7.0) 03. FortiClient EMS配置和管理 * FortiClient EMS * Fortinet 网络安全专家 NSE 5
【数学建模】青少年犯罪问题 | 逐步回归分析法stepwise函数 | 残差分析rcoplot
Zero basic learning canoe panel (1) -- new panel
[translation] improve stability and reliability with kubernetes + helm + flux!
kubernetes的这几种存储卷,别再傻傻分不清了
第一部分—C语言基础篇_9. 复合类型(自定义类型)
ACM training July 5
有趣的 Kotlin 0x0D:IntArray vs Array<Int>
Demystifying Closures, Futures and async-await in Rust–Part 3: Async & Await
Application cases under the digital twin of industry 4.0
oracle 启动命令
嵌入式学习:Cortex-M系列芯片介绍
Machine learning univariate linear regression
TCP协议
Stm32f40x minimum system
银行不是唐僧肉 银行是金融安全的坚实屏障。
Qt development skills and three problems
电子招标采购商城系统:优化传统采购业务,提速企业数字化升级