使用vim搜索和替换

时间:2014-09-04 16:02:23

标签: vim

我已经使用vim打开了一个文档(在我安装的linux上只有文本编辑器),我需要搜索并将所有BGA实例替换为CGA

我正在使用Ubuntu 12.04 LTS

该文件如下:

select comments,* from LOGS where barcode in ('BGA001248788','BGA000632039','BGA001270649','BGA000997171','BGA000997172','BGA000265968','BGA000265964','BGA000720466','BGA000720467','BGA002224291','BGA002224292','BGA000726647','BGA000609927','BGA000609928',
'BGA000504740', 'BGA000702736','BGA000547632','BGA000583033','BGA000583034','BGA000632053','BGA000225618','BGA001248788','BGA000632039','BGA001270649','BGA000997171','BGA000997172','BGA000265968','BGA000265964','BGA000720466','BGA000720467','BGA002224291','BGA002224292','BGA000726647','BGA000609927',
'BGA000609928','BGA000504740', 'BGA000702736','BGA000547632','BGA000583033','BGA000583034','BGA000632053','BGA000225618','BGA001248788','BGA000632039','BGA001270649','BGA000997171','BGA000997172','BGA000265968','BGA000265964','BGA000720466','BGA000720467','BGA002224291','BGA002224292','BGA000726647',
'BGA000609927','BGA000609928','BGA000504740', 'BGA000702736','BGA000547632','BGA000583033','BGA000583034','BGA000632053','BGA000225618','BGA001248788','BGA000632039','BGA001270649','BGA000997171','BGA000997172','BGA000265968','BGA000265964','BGA000720466','BGA000720467','BGA002224291','BGA002224292','BGA000726647','BGA000609927','BGA000609928','BGA000504740', 'BGA000702736','BGA000547632','BGA000583033','BGA000583034','BGA000632053','BGA000225618','BGA001248788','BGA000632039','BGA001270649',
'BGA000997171','BGA000997172','BGA000265968','BGA000265964','BGA000720466','BGA000720467','BGA002224291','BGA002224292','BGA000726647','BGA000609927','BGA000609928','BGA000504740', 'BGA000702736','BGA000547632','BGA000583033','BGA000583034','BGA000632053','BGA000225618')

谢谢!

2 个答案:

答案 0 :(得分:2)

在正常模式下:%s/BGA/CGA/gc其中:

  • %表示所有行
  • ssubstitute命令
  • 的别名
  • g表示每行更换超过1次
  • c提出行动

这会询问您是否要:

  • 替换(y)
  • Pass(n)
  • 全部替换(a)
  • 停止(q)
  • 以及更多

更多解释here或(在正常模式下):help subs

答案 1 :(得分:2)

要执行全面搜索并替换已在vi(m)中打开的缓冲区(文件),请执行以下操作:

1)按两次退出键以确保您处于“准备好命令” 2)输入:

%s/BGA/CGA/gi

第2行分解如下:

:    - tells vi to expect a command
%s   - tells vi to use the substitute command
/    - separator to tell vi that the search string is next
BCA  - the search string to search for
/    - tells vi that the search string is terminated and to expect the replacement string
CGA  - the replacement string
/    - tells vi that the replacement string is terminated
g    - g = global, all references in the buffer, if this is omitted then only the first instance from the cursor position is replaced
i    - i = ignore case

希望能回答你的问题!

一旦你掌握了vi(m)就没有回头!!