VIM:查找和替换单词的前 N ​​次出现 [英] VIM: Finding and replacing first N occurrences of a word

查看:25
本文介绍了VIM:查找和替换单词的前 N ​​次出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编辑一个文件,我只想用另一个词更改一个特定的词,但只针对前 N 次出现.我尝试了多个命令

I am editing a file and i want to change only a specific word with another word, but only for first N occurrences. I tried multiple commands

N :s/word/newword/

:%s/word/newword/count

以及我可以在谷歌上找到的其他命令.但它们都不起作用.

And other commands that i could find on google. But none of them works.

: Vim 命令是首选.但是也可以使用 Vim 脚本.我之前没有任何 vim 脚本方面的经验.

: Vim commands is preferred. But Vim script can also be used. I don't have any prior experience in vim scripting.

推荐答案

使用一次性录音可让您准确控制所做的更改数量:

Using a disposable recording allows you to control exactly how many changes you do:

qq             " start recording in register q
/foo<CR>       " search for next foo
cgnbar<Esc>    " change it to bar
q              " end recording
11@q           " play recording 11 times

参见:help记录:help gn.

另一种方式,使用:normal:

:norm! /foo<C-v><CR>cgnbar<C-v><Esc>     <-- should look like this: :norm! /foo^Mcgnbar^[
11@:

参见 :help :normal:help @:.

或者干脆:

:/foo/|s//bar<CR>
11@:

这篇关于VIM:查找和替换单词的前 N ​​次出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆