vim -- 查找和替换
%s/foo/bar/g
在所有行中寻找‘foo’,并且用‘bar’替换
:s/foo/bar/g
在当前行寻找‘foo’,并且用‘foo’替换
:%s/foo/bar/gc
将每一个‘foo',并用’bar‘替换,但是替换时询问
%s/\<foo\>/bar/gc
查找单词完全匹配’foo‘替换成bar,但是替换时询问
:%s/foo/bar/gci
查找’foo‘并且替换成’bar‘但是大小写不敏感,替换时询问,
:%s/foo\c/bar/gc是与上一个相同,因为\c是大小写不敏感的
使用:set noingorecase查找时就是大小写敏感的
:%s/foo/bar/gcI
将每一个’foo‘替换成’bar‘,大小写敏感,替换时询问
:$s/foo\C/bar/gcI与上一条相同
使用:set ignorecase查找时不是大小写敏感的
g表示global(全局的) - 每一行出现的都被改变,不仅是每一行的第一个
这样做的假设是默认’gdefault‘ 和 ’edcompatible‘ 是关的,所以需要:%s///g使用全局替换
使用:set gdefault时,:%s///是使用全局替换,而:%s///g不是
使用g选项主要是相反的意思
使用c选项时,对每个替换操作需要确认。Vim可能会输出replace with foobar (y/n/a/q/l/^E/^Y)?, 这里foobar是:%s/.../...,你可以使用y进行替换,n跳过这个替换,a对当前及以后所有进行替换操作(a是 all remaining matches),q是退出这个操作,l是对当前进行替换操作,然后退出操作,(l是last),^E是指按下Ctrl + E滚到上一屏,^Y是指Ctrl + Y滚到下一屏,然而最后两个选项是可选的。
还在使用c标志,Vim会跳到它从顶部查找到的第一个匹配项,提示你确认对匹配执行替换。Vim IncSearch突出匹配的文本给你一个视觉提示在对哪个匹配进行操作。
查找范围
:s/foo/bar 将当前行’foo‘全部替换为‘bar’
:%s/foo/bar/g 将所有行的’foo‘全部替换为’bar‘
:5,12s/foo/bar/g 将5-12行(包括5行和12行)中的’foo'全部替换为‘bar’
:'a,'bs/foo/bar/g 从标记a到标记b所有行中的‘foo’替换为‘bar’
:'<,'>s/foo/bar/g 当使用 +visual时,在visual选中的区域。Vim会自动加入 ( '<, '>)当你选中一片区域,并且按下:时。
:.,$s/foo/bar/g 从当前行(.)到最后一行($)将’foo‘替换成’bar‘
:.,+2s/foo/bar/g 从当前行(.)到下两行将’foo‘替换成’bar‘
:g/^baz/s/foo/bar/g 以’baz‘开头的所有行中的’foo‘替换成’bar'
注意:As of Vim 7.3, substitutions applied to a range defined by marks or a visual selection (which uses a special type of marks '< and '>) are not bounded by the column position of the marks by default. Instead, Vim applies the substitution to the entire line on which each mark appears unless the \%V
atom is used in the pattern like: :'<,'>s/\%Vfoo/bar/g
.
使用查找时:
.
, *
, \
, [
, ^
, 和 $ 是元字符
+
, ?
, |
, &
, {
, (
, and )
must be escaped to use their special function.
\/
是 /(用反斜杠 + 斜杠查找斜杠)
\t 是tab,\s是空格
\n 是新的一行,\r是CR(回车符 = Ctrl + M = ^M)
[]在中括号之间的内容表示要查找内容的一个集合,字符的范围可以用一个 - , 比如,要查找一个字符a,b,c, 或者是数字1,可以使用[1a-c]表示,^表示取反,例如[^1a-c]表示匹配除1,a,b,c以外的所有字符
\{#\}用来匹配剩下的字符,/foo.\{2\} 会匹配foo和紧跟着的两个字符,\没有必要,/foo.{2}会有相同的结果
\(foo\) 对‘foo’制造了一个回溯引用,括号没有\表示匹配,所以\在这里是必须的
使用替换时:
\r表示新的一行,\n表示没有字符
\&表示&
\0
inserts the text matched by the entire pattern
\1插入第一个回溯引用,\2表示插入第二个回溯引用
也可以使用其他的分割符来进行查找,例如
:s#http://www.example.com/index.html#http://example.com/#
使用\zs和\ze来指定替换的开始和结束,比如
:s/Copyright 2007 All Rights Reserved/Copyright 2008 All Rights Reserved/
可以用
:s/Copyright \zs2007\ze All Rights Reserved/2008/
使用当前的word或是寄存器
:%s//bar/g
用‘bar’替换上一个匹配的模式
:%s/foo/<c-r><c-w>/g
- Replace each occurrence of 'foo' with the word under the cursor.
<c-r><c-w>
means that you press Ctrl-R then Ctrl-W.- The word under the cursor will be inserted as though you typed it.
:%s/foo/<c-r><c-a>/g
- Replace each occurrence of 'foo' with the WORD under the cursor (delimited by whitespace).
<c-r><c-a>
means that you press Ctrl-R then Ctrl-A.- The WORD under the cursor will be inserted as though you typed it.
:%s/foo/<c-r>a/g
- Replace each occurrence of 'foo' with the contents of register 'a'.
<c-r>a
means that you press Ctrl-R thena
.- The contents of register 'a' will be inserted as though you typed it.
:%s/foo/<c-r>0/g
- Same as above, using register 0 which contains the text from the most recent yank command. Examples of yank (copy) commands are
yi(
which copies the text inside parentheses around the cursor, andy$
which copies the text from the cursor to the end of the line. After a yank command which did not specify a destination register, the copied text can be entered by pressing Ctrl-R then0
.
:%s/foo/\=@a/g
- Replace each occurrence of 'foo' with the contents of register 'a'.
\=@a
is a reference to register 'a'.- The contents of register 'a' is not shown in the command. This is useful if the register contains many lines of text.
:%s//<c-r>//g
- Replace each match of the last search pattern with the
/
register (the last search pattern). - After pressing Ctrl-R then
/
to insert the last search pattern (and before pressing Enter to perform the command), you could edit the text to make any required change.
:%s/<c-r>*/bar/g
- Replace all occurrences of the text in the system clipboard (in the
*
register) with 'bar' (see next example if multiline). - On some systems, selecting text (in Vim or another application) is all that is required to place that text in the
*
register.
:%s/<c-r>a/bar/g
- Replace all occurrences of the text in register 'a' with 'bar'.
<c-r>a
means that you press Ctrl-R thena
. The contents of register 'a' will be inserted as though you typed it.- Any newlines in register 'a' are inserted as
^M
and are not found. - The search works if each
^M
is manually replaced with '\n' (two characters: backslash, 'n'). - This replacement can be performed while you type the command:
:%s/<c-r>=substitute(@a,"\n",'\\n','g')<CR>/bar/g
- The
"\n"
(double quotes) represents the single character newline; the'\\n'
(single quotes) represents two backslashes followed by 'n
'. - The
substitute()
function is evaluated by the<c-r>=
(Ctrl-R=
) expression register; it replaces each newline with a single backslash followed by 'n
'. - The
<CR>
indicates that you press Enter to finish the=
expression.
:%s/<c-r>0/bar/g
- Same as above, using register 0 which contains the text from the most recent yank command
使用例子:
:%s/foo/bar/
将每一行第一次出现的‘foo’用‘bar’替换
:%s/.*\zsfoo/bar/
将每一行最后一次出现的'foo'用‘bar’替换
:%s/\<foo\>//g
对每一行,删除所有出现的‘foo’
:%s/\<foo\>.*//
将每一行出现的‘foo’以及这一行之后的所有文本删除
:%s/\<foo\>.\{5}//
将每一行出现的‘foo’以及之后的5个字符删除
:%s/\<foo\>\zs.*//
将每一行出现在‘foo’之后的文本删除
:%s/.*\<foo\>//
将每一行中的‘foo’和它之前的文本删除
:%s/.*\ze\<foo\>//
将每一行出现在‘foo’之前的文本删除
:%s/.*\(\<foo\>\).*/\1/
将每一行‘foo'之前的文本和之后的文本删除
:s/^\(\w\)/\u\1/
将当前行的第一个字符改成大写
:%s/\(.*\n\)\{5\}/&\r/
每5行插入一个空白行
将\(.*\n\)(任意一行)重复5次(\{5\})
替换成&(匹配到的文本,就是5行内容),后面加上新的一行(\r)
:%s/\<foo\(\a*\)\>/\=len(add(list, submatch(1)))?submatch(0):submatch(0)/g
得到一个匹配结果的列表,列表必须存在
设置modified标志,因为替换,但是内容没有改变
:%s/\<foo\(\a*\)\>/\=add(list, submatch(1))/gn
This has the advantage, that the buffer won't be marked modified and no extra undo state is created. The expression in the replacement part is executed in the sandbox and not allowed to modify the buffer.
vim -- 查找和替换的更多相关文章
- vim 查找与替换
一.vim 查找 1. 正向查找 / 与 反向查找 ? 2. 退出查找 <Esc> 3. 跳转到下一处匹配 n ,跳转到上一处匹配 N 4. /<CR> 正向跳转到相同模式的下 ...
- Vim查找与替换命令大全,功能完爆IDE!
Vi/Vim 可以说是文本编辑中的一代传奇人物,直至现在,它仍然在高级程序员的武器库中占有一席之地.每个 Linux 发行版默认都包含Vim ,而且即使你不是 Linux 系统用户,你也可以安装 Vi ...
- vim 查找和替换命令 替换/n和\n
一. 字符串的查找 1. vim 中用 / 和 ? 来查找字符串,两者的区别是: /string 会高亮显示光标后匹配的第一个字符串,回车后光标移到该字符串的第一个字母: ?string 会高亮显示光 ...
- vim 查找并替换多个匹配字符
通常我们在使用vim的使用需要查找文档中是否含有需要的字符 1.vim 1.txt进入文档编辑 2.输入/键,再输入需要查找的字符,或者输入?键再输入需要查找的字符 3.查找到后可以enter进去,再 ...
- vim 查找及替换
#全文(%)查找(s)行首2个空格开头(/^ ), 替换(g)为无即删掉(//) :%s/^ //g #全文查找每行尾的2个空格,删除 :%s/ $//g
- Vim查找与替换
\c 忽略大小写 \C 强制区分大小写 \v 除了_.字母.数字以为的所有字符都当做具有特殊含义的字符 \V 只有反斜杠有特殊含义 %s///gn 统计某个词出现的次数 替换的flag g 全局范围执 ...
- vim查找和替换
https://www.cnblogs.com/huxinga/p/7942194.html %s/husband/丈夫/g
- 在VIM中进行快速的查找和替换
VIM是被誉为非常高效的文本编辑软件.但是掌握并高效的使用是件有难度的事情.在VIM中进行快速的查找和替换是提高VIM使用效率的重要方法.下面是我在阅读VIM用户手册时整理的一些资料: 行内搜索. f ...
- VIM 技巧 (二)查找与替换
今天和大家分享下 对于 vim 中 查找与替换方法 例如有一篇文章.中英文不限制. 用户如果想查找其中的文字或单词 在 win系统的Notepad中一般都时 ctrl + F 去查询 而在 vim ...
随机推荐
- ThreadLocal的简单应用
概括起来说,对于多线程资源共享的问题,同步机制采用了“以时间换空间”的方式,而ThreadLocal采用了“以空间换时间”的方式.前者仅提供一份变量,让不同的线程排队访问,而后者为每一个线程都提供了一 ...
- Android studio 模拟器中输入中文
Android studio 模拟器中输入中文 学习了:https://blog.csdn.net/feidie436/article/details/78318752?locationNum=10& ...
- TestNG 四 测试方法之工厂
工厂允许你动态的创建测试.例如,假设你需要创建一个测试方法,并用它来多次访问一个web页面,而且每次都带有不同的参数: public class TestWebServer { @Test(param ...
- Redis源代码分析(五)--- sparkline微线图
sparkline这个单词,我第一次看的时候.也不知道这什么意思啊,曾经根本没听过啊,可是这真真实实的出如今了redis的代码中了,刚刚開始以为这也是属于普通的队列嘛.就把他分在了struct包里了. ...
- vue - 选项
1.计算属性(computed):主要是对原数据进行改造输出.改造输出:包括格式化数据(价格,日期),大小写转换,排序,添加符号 2.methods(methods):用于绑定html中的事件对应的方 ...
- Java基础——线程总结
Java基础--线程总结 一.线程是什么? 线程:一个程序里不同的运行路径. 二.怎样创建线程? 两种方法创建线程: 第一种 (1)定义详细功能类实现Runnable接口,能够多次调用而实现数据共享 ...
- 站点搭建从零開始(五) WordPress的安装
前面说了非常多废话.如今最终转到正题.WordPress的安装. 1.WordPress安装非常easy 假设你的server能通过应用中心一键安装WordPress,这一节就非常轻松了,基本上不须要 ...
- 默认权限umask
什么是umask? 当我们登录系统之后创建一个文件总是有一个默认权限的,那么这个权限是怎么来的呢?这就是umask干的事情.umask设置了用户创建文件的默认 权限,它与chmod的效果刚好相反,um ...
- android studio教学视频资源(点开即看)
android studio教学视频资源(点开即看) 自从Google推出android studio之后.包含github在内的非常多第三方代码库项目很多其它的採用的android studio编译 ...
- Python 的__name__属性
Python 的__name__属性: 一个模块被另一个程序第一次引入时,其主程序将运行. 如果我们想在模块被引入时,模块中的某一程序块不执行,我们可以用__name__属性来使该程序块仅在该模块自身 ...