Vi / VIM: Find And Replace All Text Substitute Command

本文介绍了如何使用Vim编辑器进行高效搜索与替换操作的方法。包括仅替换当前行中的匹配项、替换整个文件中所有匹配项及指定范围内替换等实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vim

Vim: Search and replace syntax

The syntax is as follows:

:s/Search/Replace/CommandFlag
:s/Search-Word/Replace-Word/g
:%s/Search-Word/Replace-Word/g
:%s/Search-Word/Replace-Word/gc

Examples Text

-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

## full acess to lo and eth0 ##
-A INPUT -i lo -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
-A INPUT -i eth0 -j ACCEPT
-A OUTPUT -o eth0 -j ACCEPT

# log spoof
-A INPUT -i eth1 -s 10.0.0.0/8 -j LOG --log-prefix "IP DROP SPOOF A: "
-A INPUT -i eth1 -s 10.0.0.0/8 -j DROP

-A INPUT -i eth1 -s 172.16.0.0/12 -j LOG --log-prefix "IP DROP SPOOF B: "
-A INPUT -i eth1 -s 172.16.0.0/12 -j DROP

To find each occurrence of eth0 in the current line only, and replace it with br0, enter (first press Esc key and type):

:s/eth0/br0/g

To find and replace all occurrences of eth1 with br1, enter:

:%s/eth1/br1/g

To find and replace all occurrences of eth1 with br1, but ask for confirmation first, enter:

:%s/eth1/br1/gc

To find and replace all occurrences of case insensitive eth1 with br1, enter:

:%s/eth1/br1/gi

The above example will find eth1, ETH1, eTh1, ETh1 and so on and replace with br1.
To find and replace all occurrences of eth1 with br1 for lines from 3 to 7, enter:

:3,7s/eth1/br1/g

A quick demo

vim_demo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值