首页 > vim整段删除或者替换

vim整段删除或者替换

有一段这样的文本

As of initscripts-2012.07.5, the default format of /etc/rc.conf and
/etc/crypttab have changed. See their respective man pages for 
more details.

--BEGIN--
The purpose of this change is to unify the configuration of systemd
and initscripts. This will allow us to share code and documentation, 
and should make maintenance of initscripts simpler in the long-run.
--END--

The old format is still supported, so old config files should still
work unchanged.

希望能吧--BEGIN-- --END--之间的所有行都删除掉,vim有什么直接的方法吗?

另外一个附加的问题,就是已经有
--BEGIN-- 标志了,如何能添加一段内容。

以上都是希望能脚本化的处理文本文件。


只是一个例子,--BEGIN--,--END--之间的行数不确定
手动标记这个,有点辛苦,实际文件不小,而且类似的结构
比较多.


vim可以做到,

在命令模式下:

%s/--BEGIN--\_.*--END--//g

其中的\_是关键,这个可以匹配多行。
已经有--BEGIN--标识如何替换,这个也可以用上面的替换思路来做
:%s/^--BEGIN--/&要替换的内容/g


既然要脚本化,就用 sed 呗:

sed -i '/--BEGIN--/,/--END--/d' files_to_modify

ndd,n为要删除的行数。


:g/--BEGIN--/,/--END--/d
如果要同时删除前后的空行
:g/--BEGIN--/-1,/--END--/+1d


光标移至开始部分
Esc v 进行view模式
j 向下选中操作部分
d 删除 x 剪切 两个都可以

【热门文章】
【热门文章】