惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

TaoSecurity Blog
TaoSecurity Blog
Jina AI
Jina AI
雷峰网
雷峰网
月光博客
月光博客
The GitHub Blog
The GitHub Blog
WordPress大学
WordPress大学
B
Blog RSS Feed
美团技术团队
C
CXSECURITY Database RSS Feed - CXSecurity.com
小众软件
小众软件
Security Latest
Security Latest
Microsoft Azure Blog
Microsoft Azure Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cybersecurity and Infrastructure Security Agency CISA
Last Week in AI
Last Week in AI
A
Arctic Wolf
Latest news
Latest news
Attack and Defense Labs
Attack and Defense Labs
I
Intezer
F
Fortinet All Blogs
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
Webroot Blog
Webroot Blog
S
Secure Thoughts
Help Net Security
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
V
Visual Studio Blog
P
Proofpoint News Feed
博客园 - 【当耐特】
P
Privacy International News Feed
V
Vulnerabilities – Threatpost
Stack Overflow Blog
Stack Overflow Blog
Know Your Adversary
Know Your Adversary
云风的 BLOG
云风的 BLOG
Hacker News: Ask HN
Hacker News: Ask HN
L
LINUX DO - 最新话题
H
Help Net Security
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tenable Blog
Cloudbric
Cloudbric
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog

博客园 - JackieHan

ubuntu i3 xterm中文输入显示问题解决 ubuntu vim markdown 实时预览 ubuntu docker方式部署docker registry v2 docker 安装 gogs(go git server) 及问题解决 ubuntu14.04 server 安装docker Ubuntu Tomcat Ubuntu ssh 代理 全网访问速度优化 git 操作 vim使用指北 ---- Multiple Windows in Vim vim使用指北 ---- Advanced Editing vi/vim使用指北 ---- Introducting the ex Editor ubuntu 点点滴滴 vi/vim使用指北 ---- Learning the vi and Vim Editors 读书 笔记 vi/vim使用指北 ---- Beyond the Basic vi/vim使用指北 ---- Moving Around in a Hurry vi/vim使用指北 ---- Sample Editing linux权威指南 简记 idea 使用
vim使用指北 ---- Global Replacement
JackieHan · 2014-09-18 · via 博客园 - JackieHan

2014-09-18 01:27  JackieHan  阅读(329)  评论()    收藏  举报

一般替换

s/old/new   --- 替换当前行的第一个匹配项

s/old/new/g ---- 替换当前行所有的匹配项

number1,number2-s/old/new/g  ---- 替换从number1行到number2行中所有的匹配项

1,$s/old/new/g = %s/old/new/g ----- 替换整个文件的所有匹配项

确认再替换

在替换命令后面加个c(confirm),例如1,30s/his/the/gc。每次找到匹配项时会提示是否需要替换,输入y,Enter确认替换,不需要替换直接Enter跳过

context-sensitive 替换 g/pattern/s/old/new/g

第一个g表示需要从所有行查找pattern,后面的g表示当前行所有的匹配项都替换。

如果pattern和old是一样的,old可以省略,这样的命令等同于%s/old/new/g

正则表达式匹配规则

. ---- 匹配除换行符以外的所有字符

* ---- 匹配0个或多个字符

^ ---- 匹配一行的开始

$ ---- 匹配一行的结束

\ ---- 反意字符,表示后面的字符为一个常规字符。

\( \) --- 占位符,一行最多可以有9个。例如\(That\) or \(this\)    \(That\)为1号,后面需要用到That就可以使用\1来代替,\(this\)为2号,是用\2来调用。例如把That or this 替换为this or That则可以用命令%s/\(That\) or \(this\)/\2 or \1

\< /> ---- 匹配以字符开头或结尾的单词,例如\<ac 匹配以ac开头的单词,ac/>则匹配以ac结尾的单词

~ ---- 在查找中匹配上一个使用的正则表达式。例如开始查找/The,现在想查找/Then,则可以使用/~n来代替/Then

POSIX character classes

[:alnum:] ---- 匹配字母数字字符

[:alpha:] ---- 匹配字母字符

[:blank:] ---- 匹配空格和Tab字符

[:cntrl:] ---- 匹配Control字符

[:digit:] ---- 匹配数字字符

[:graph:] ---- 匹配可打印的和可见的非空白字符

[:lower:] ---- 匹配小写字符

[:print:] ---- 匹配可打印的字符包括空白

[:punct:] ---- 匹配标点符号字符

[:space:] ---- 匹配空白字符

[:upper:] ----- 匹配大写字符

[:xdigit:] ---- 匹配16进制的数字字符

在替换字符中使用元字符

\n ---- n表示0,9,前面说过\( \)保存的表达式

\ ---- 表示后面的特殊字符为普通字符

& ---- 表示查到的字符串

~ ---- 表示上一次替换的字符串

\u \l ---- 表示改变其后的字符为大写的或小写的,例如s/\(That\) or \(this\)/\u\2 or \l\1  把That or this 替换成This or that

\U \L 和\e \E ---- 表示改变其后的字符为大写的或小写的直到\e或\E的出现