Clone
1
vim查找替换功能
newde edited this page 2026-04-25 04:53:20 -04:00
This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

1. 常用替换用法

:s/old/new/:替换当前行第一个匹配的 old 为 new。
:s/old/new/g替换当前行所有匹配的 old 为 new。
:%s/old/new/g替换文件中所有行全局的 old 为 new。
:%s/old/new/gc全局替换但在每次替换前提示确认 (c)。 

2. 指定范围替换

  :n1,n2s/old/new/g替换第 n1 行到第 n2 行的 old 为 new。
  :.,$s/old/new/g替换当前行 (.) 到文件末尾 ($) 的所有内容。 

3. 特殊分隔符

    如果查找或替换的内容包含 /(如路径),可以用 # 或 + 代替 /
    :s#http://www.google.com#https://www.google.com#g。 

替换操作补充

  确认提示符 (c):使用 :%s/old/new/gc 后,系统会提示 replace with new (y/n/a/q/l/^E/^Y)?。
  y (yes):确认
  n (no):跳过
  a (all):替换剩余所有
  q (quit):退出

使用正则表达式:替换命令支持正则表达式(例如使用 \v 开启魔术模式)。

快速查找与替换技巧 (结合查找与功能)

使用 / 查找目标单词。
按下 cw (change word) 修改当前单词,输入新内容。
按 Esc 退出插入模式。
按下 n 查找下一个。
按下 . (点号) 重复上一次替换操作。