本文翻译自:Find and replace strings in vim on multiple lines

I can do :%s/<search_string>/<replace_string>/g for replacing a string across a file, or :s/<search_string>/<replace_string>/ to replace in current line. 我可以执行:%s/<search_string>/<replace_string>/g来替换文件中的字符串,或者:s/<search_string>/<replace_string>/来替换当前行。

How can I select and replace words from selective lines in vim? 如何在vim中的选择行中选择和替换单词?

Example: replace text from lines 6-10 , 14-18 but not from 11-13 . 示例:替换第6-10行,第14-18行的文本,而不是第11-13行的文本。


#1楼

参考:https://stackoom.com/question/1Ltak/在多行中查找和替换vim中的字符串


#2楼

You can do it with two find/replace sequences 您可以使用两个查找/替换序列来完成此操作

  :6,10s/<search_string>/<replace_string>/g:14,18s/<search_string>/<replace_string>/g

The second time all you need to adjust is the range so instead of typing it all out, I would recall the last command and edit just the range 第二次您需要调整的是范围,所以我不用回输所有内容,而是回想上一条命令并仅编辑范围


#3楼

Replace All: 全部替换:

:%s/foo/bar/g

Find each occurrence of 'foo' (in all lines), and replace it with 'bar'. 找到所有出现的“ foo”(在所有行中),然后将其替换为“ bar”。

For specific lines: 对于特定的行:

:6,10s/foo/bar/g

Change each 'foo' to 'bar' for all lines from line 6 to line 10 inclusive. 将第6行到第10行(包括第6行)的所有行的每个“ foo”更改为“ bar”。


#4楼

The :&& command repeats the last substitution with the same flags. :&&命令用相同的标志重复最后的替换。 You can supply the additional range(s) to it (and concatenate as many as you like): 您可以为其提供其他范围(并连接任意多个范围):

:6,10s/<search_string>/<replace_string>/g | 14,18&&

If you have many ranges though, I'd rather use a loop: 如果您有很多范围 ,我宁愿使用循环:

:for range in split('6,10 14,18')| exe range 's/<search_string>/<replace_string>/g' | endfor

#5楼

As a side note, instead of having to type in the line numbers, just highlight the lines where you want to find/replace in one of the visual modes: 作为旁注,无需键入行号,只需在一种可视模式下突出显示要查找/替换的行即可:

  • VISUAL mode ( V ) VISUAL模式( V )
  • VISUAL BLOCK mode ( Ctrl + V ) VISUAL BLOCK模式( Ctrl + V )
  • VISUAL LINE mode ( Shift + V , works best in your case) VISUAL LINE模式( Shift + V ,最适合您的情况)

Once you selected the lines to replace, type your command: 选择要替换的行后,键入命令:

:s/<search_string>/<replace_string>/g

You'll note that the range '<,'> will be inserted automatically for you: 您会注意到,将自动为您插入范围'<,'>

:'<,'>s/<search_string>/<replace_string>/g

Here '< simply means first highlighted line , and '> means last highlighted line . 在这里, '<仅表示第一行突出显示的行 ,而'>表示最后突出显示的行

Note that the behaviour might be unexpected when in NORMAL mode: '< and '> point to the start and end of the last highlight done in one of the VISUAL modes. 请注意,在NORMAL模式下,该行为可能是意外的: '<'>指向在一种VISUAL模式下完成的最后一个突出显示的开始和结束。 Instead, in NORMAL mode, the special line number . 而是在NORMAL模式下输入特殊行号. can be used, which simply means current line . 可以使用,它仅表示当前行 Hence, you can find/replace only on the current line like this: 因此,您只能在当前行上查找/替换,如下所示:

:.s/<search_string>/<replace_string>/g

Another thing to note is that inserting a second : between the range and the find/replace command does no harm, in other words, these commands will still work: 要注意的另一件事是,在range和find / replace命令之间插入第二个:并没有什么害处,换句话说,这些命令仍然可以使用:

:'<,'>:s/<search_string>/<replace_string>/g
:.:s/<search_string>/<replace_string>/g

#6楼

/sys/sim/source/gm/kg/jl/ls/owow/lsal
/sys/sim/source/gm/kg/jl/ls/owow/lsal
/sys/sim/source/gm/kg/jl/ls/owow/lsal
/sys/sim/source/gm/kg/jl/ls/owow/lsal
/sys/sim/source/gm/kg/jl/ls/owow/lsal
/sys/sim/source/gm/kg/jl/ls/owow/lsal
/sys/sim/source/gm/kg/jl/ls/owow/lsal
/sys/sim/source/gm/kg/jl/ls/owow/lsal

Suppose if you want to replace the above with some other info. 假设您想将以上内容替换为其他信息。

COMMAND(:%s/\\/sys\\/sim\\/source\\/gm\\/kg\\/jl\\/ls\\/owow\\/lsal/sys.pkg.mpu.umc.kdk./g)

In this the above will be get replaced with ( sys.pkg.mpu.umc.kdk. ) . 在此上面的内容将被替换为( sys.pkg.mpu.umc.kdk. )。

在多行中查找和替换vim中的字符串相关推荐

  1. 在Visual Studio Code中查找并​​替换为换行符

    本文翻译自:Find and replace with a newline in Visual Studio Code I am trying out the new Microsoft Visual ...

  2. Microsoft Excel 教程:如何在 Excel 中查找或替换工作表上的文本和数字?

    欢迎观看 Microsoft Excel 教程,小编带大家学习 Microsoft Excel 的使用技巧,了解如何在 Excel 中查找或替换工作表上的文本和数字. 在 Excel 中使用「查找和替 ...

  3. EXcel中查找,替换,定位

    Excel中查找和替换: 查找和替换快捷键:ctrl+F,或者在开始菜单栏下的查找和选择中打开如下界面 查找选项: 范围:可以在工作表中或者整个工作簿中查找 搜索 :可以按照行或者列进行搜索 查询范围 ...

  4. 在Python中查找和替换文本,玩转Python正则

    最简单的查找替换 在Python中查找和替换非常简单,如果当前对象是一个字符串str时,你可以使用该类型提供的find() 或者index() 方法查找指定的字符,如果能找到则会返回字符第一次出现的索 ...

  5. Word中查找和替换功能详细介绍

    在Word中查找和替换功能很多人都会使用,最通常是用它来查找和替换文字,但实际上还可用查找和替换格式.段落标记.分页符和其他项目,并且还可以使用通配符和代码来扩展搜索. 一.查找和替换文字 例如:将& ...

  6. JS实现文本中查找并替换字符

    JS实现文本中查找并替换字符 效果图: 代码如下,复制即可使用: <!DOCTYPE html><html> <head><style type=" ...

  7. php 查找文件 替换内容,关于php:文件中查找和替换功能困扰

    我知道这不是编程问题吗? 但是我需要另一个有关在PHP代码编辑器中查找和替换功能的帮助. 我正在使用eclipse和Dreamweaver,我有大约650个php文件,并希望替换所有文件中的字符串而无 ...

  8. excel 替换 换行符_在Excel中查找和替换换行符

    excel 替换 换行符 When you want to create a line break (line feed) in a cell, you press Alt + Enter, to s ...

  9. Microsoft Word 教程:如何在 Word 中查找和替换文本?

    欢迎观看 Microsoft Word 教程,小编带大家学习 Microsoft Word 的使用技巧,了解如何在 Word 中查找和替换文本. 如果处理大量文本,并需要搜索替换某个单词或短语,可使用 ...

最新文章

  1. Windows——右键菜单显示“管理员取得所有权”选项
  2. boost::fibers模块实现single stream的测试程序
  3. java object对象的方法_Java常见对象Object类中的个别方法
  4. 关于纯HTML格式写入word
  5. linux设置php时间,修改 linux 时区时间和 php 时区(示例代码)
  6. 如何重置 RHEL7/CentOS7 系统的密码
  7. 问题 B: 编写函数:Swap (I) (Append Code)
  8. 数字信号处理《数字滤波器的MATLAB与FPGA实现》
  9. SpringBoot中发送QQ邮件
  10. Oracle客户端安装配置crystal reports注意
  11. RapidMiner介绍与实践(一)决策树
  12. layui之table操作点击编辑,使用layer.open回显值
  13. Android显示gif格式图片
  14. b2b2c商城php源码,多用户B2B2C商城系统 thinkphp5.0
  15. 使用-OB-ODC连接OceanBase数据库与模拟数据功能使用
  16. DSPE-PEG-cRGD,磷脂-聚乙二醇-环肽RGD,靶向穿膜肽RGD环肽供应
  17. windows加载wim文件
  18. 读论文Beyond Part Models Person Retrieval with Refined Part Pooling
  19. mysql修改字符串_mysql中replace替换字符串更改方法
  20. cpu的基本结构及其工作原理

热门文章

  1. oracle 知识点
  2. Windows访问Linux下的共享目录的配置方法(user安全级别)
  3. Ceylon 1.0.0
  4. Aspose.Slides for Java 3.0 发布
  5. ios整理(六)关于用富文本在tableview的cell去加载html字符串的优化方案
  6. JavaScript 设计模式之代理模式
  7. [设计模式] 23 访问者模式 visitor Pattern
  8. POJ 2117 Electricity 割点 Tarjan算法
  9. DELL R430服务器做raid5以及安装操作系统过程
  10. Linux系统扩展oracle数据库所在的分区