今天来分享几个非常好用的vim命令,学会后,工作效率翻倍!

1. 官方文档

官方文档:Search and replace

The :substitute command searches for a text pattern, and replaces it with a text string. There are many options, but these are what you probably want:

  • :s/foo/bar/g

    Find each occurrence of ‘foo’ (in the current line only), and replace it with ‘bar’.

  • :%s/foo/bar/g

    Find each occurrence of ‘foo’ (in all lines), and replace it with ‘bar’.

  • :%s/foo/bar/gc

    Change each ‘foo’ to ‘bar’, but ask for confirmation first.

The g flag means global – each occurrence in the line is changed, rather than just the first.

2. 替换命令

  • s表示匹配当前行
  • %s表示匹配所有行
  • g参数表示替换掉当前行所有的可能,如果不加g则只会替换掉当前行的第一个

2.1 示例:

far Bar from api import func for api bar
hello this is a foo bar foo bar test.


如果在第一行执行 :s/api/change,得到

far Bar from change import func for api bar
hello this is a foo bar foo bar test.

如果在第一行执行 :s/api/change/g,得到

far Bar from change import func for change bar
hello this is a foo bar foo bar test.

如果在第一行执行 :s/bar/yyy/g,得到

far yyy from api import func for api yyy
hello this is a foo yyy foo yyy test.

注:大小写匹配是在.vimrc中配置set ignorecase

3. 用法总结

(1)替换当前行匹配内容

:s/foo/bar/g
:s/foo/bar

(2)替换全局匹配内容

:%s/foo/bar/g
:%s/foo/bar

(3)替换选中的几行的匹配内容(Visual Mode下按 : )

:'<,'>s/foo/bar/g
:'<,'>s/foo/bar

(4)删除匹配掉的内容

:%s/foo//g # 以全局为例,把bar去掉即可

4. 相关资料

  • What is the difference between :g and :%s commands in vim
  • Is it possible to interactively delete matching search pattern in Vim?

Linux Vim搜索替换命令详解 :%s/foo/bar/g相关推荐

  1. Ubuntu (Linux) 系统 find 命令详解

    Ubuntu (Linux) 系统 find 命令详解 在Ubuntu (Linux)系统的终端上,虽然没有像 windows 上那样简介易用的图形界面 搜索工具,但只要你使用足够熟练, 有一个强大命 ...

  2. Linux操作系统上lsof命令详解

    Linux操作系统上lsof命令详解 2011-10-08 18:31:31 http://xjsunjie.blog.51cto.com/999372/682865 标签:Linux lsof命令 ...

  3. LINUX经常使用的命令详解

    LINUX经常使用的命令详解 源地址:http://blog.itpub.net/29065182/viewspace-1189162/ 1.man 对你熟悉或不熟悉的命令提供帮助解释  eg:man ...

  4. linux ps(process status) 命令详解

    linux ps(process status) 命令详解 功能说明:报告程序状况. 语 法:ps [-aAcdefHjlmNVwy][acefghLnrsSTuvxX][-C <指令名称> ...

  5. linux slocate(secure locate) 命令详解

    linux slocate(secure locate) 命令详解 功能说明:查找文件或目录. 语 法:slocate [-u][--help][--version][-d <目录>][查 ...

  6. Linux上的free命令详解

    Linux上的free命令详解 转自: http://www.cnblogs.com/coldplayerest/archive/2010/02/20/1669949.html 解释一下Linux上f ...

  7. linux cut命令学习,Linux中的cut 命令详解

    今天小编要跟大家分享的文章是关于Linux中的cut 命令详解.cut 命令在Linux和Unix中的作用是从文件中的每一行中截取出一些部分,并输出到标准输出中.我们可以使用 cut 命令从一行字符串 ...

  8. linux磁盘分区fdisk命令详解及云硬盘挂载实操

    linux磁盘分区fdisk命令详解 1.什么是分区? 分区是将一个硬盘驱动器分成若干个逻辑驱动器,分区是把硬盘连续的区块当做一个独立的磁硬使用.分区表是一个硬盘分区的索引,分区的信息都会写进分区表. ...

  9. avast! for linux/unix servers,最常见的Linux/Unix监控工具vmstat命令详解

    最常见的Linux/Unix监控工具vmstat命令详解 vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率.内存使用.虚拟内存交换 ...

最新文章

  1. AutoShape:实时单目3D检测网络(ICCV2021)
  2. linux安装 mysql-5.7.25_Linux 系统下安装 mysql5.7.25(glibc版)
  3. flask之url_for()函数解析
  4. Ubuntu16.04上安装kitti2bag
  5. 数万字的0基础React知识大纲一定要藏藏好
  6. Linux工作笔记040---Centos8.2安装mysql5.7.18_已经测试成功
  7. grub-install: warning: this GPT partition label contains no BIOS Boot Partition; embedding won’t be
  8. 【语音去噪】基于matlab小波软阈值语音降噪【含Matlab源码 531期】
  9. 国外字体设计师也是蛮重视数学的
  10. 语法分析器实现过程(java)
  11. 熟练掌握计算机应用,计算机应用技术专业个人技能怎么写
  12. LINUX内存管理子系统和进程管理子系统
  13. python变量贡献率排序_103,每个特征的贡献率是多少?
  14. echarts的中国地图,点击进入省级地图,按需加载对应的省js,可返回中国地图
  15. 山西省2021年高考试成绩查询,山西省2021年普通高等学校招生体育专业考试成绩查询入口...
  16. Python3.7学习笔记30-垃圾回收机制
  17. 什么浏览器好用稳定速度快?
  18. imoo c1语言设置在哪里,imoo C1学生手机怎么样?imoo C1参数配置介绍
  19. 第一篇 重新认识皮肤
  20. 《需求工程——软件建模与分析》阅读笔记5

热门文章

  1. 一、《图解HTTP》了解Web和网络基础
  2. Ios android crash report,iOS App 后台 Crash 调查
  3. qq_seconds.html,qq英语名字汇总82句
  4. Ubuntu 11.10安装QQ2012
  5. 混合光电探测器(HPD)行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
  6. android远程控制灯光,11款可用手机远程控制的智能灯
  7. 2020年第一波薅当当网羊毛的机会,别错过!
  8. 在坦桑尼亚如何打国际长途,打给你中国的朋友?
  9. java csvwriter 追加_CSV文件每行的末尾追加写数据
  10. ERROR CoarseGrainedExecutorBackend: RECEIVED SIGNAL TERM