Vim 全局命令使每一行包含一个符号,以单词开头

发布于 2025-01-05 02:27:35 字数 382 浏览 0 评论 0原文

考虑以下文本文件:

something
something
something = someother thing
other thing = third thing
another thing = forth thing

我想让它看起来像这样:

something
something
keyword something = someother thing
keyword other thing = third thing
keyword another thing = forth thing

这样,我向每一行添加关键字,其中包含一个等号。 我可以使用全局命令来执行此操作吗?或者您建议我应该如何执行此操作?

Consider following text file:

something
something
something = someother thing
other thing = third thing
another thing = forth thing

I want to make it look like this:

something
something
keyword something = someother thing
keyword other thing = third thing
keyword another thing = forth thing

so that, I add keyword to each line, what is contains a equals symbol in it.
Can I do this with global command, or how do you recommend I should do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒路情人 2025-01-12 02:27:35
:g/=/s/^/keyword /

:g/=/normal ikeyword

注意“关键字”后面的空格

对于此类问题,使用如下解决方案也很常见:

:%!sed '/=/s/^/keyword /'
:g/=/s/^/keyword /

or

:g/=/normal ikeyword

Note the space after "keyword"

For this type of problem, it's also quite common to use a solution like:

:%!sed '/=/s/^/keyword /'
花心好男孩 2025-01-12 02:27:35

我不太确定你想要实现什么目标。您的标题暗示了一种常见模式,但我在您的示例中没有看到这种模式。所以我会向你们展示两者。

使用通用模式对事物进行更改

您可以使用以下内容进行搜索和替换:

:s/<regex you are searching for>/<string to replace with>/g

s/pattern/replacement/ 进行搜索和替换替换,额外的 g 将传播更改

多行编辑

Vim 还允许您一次编辑多行。假设您要编辑以下三行:

something = someother thing
other thing = third thing
another thing = fourth thing
  1. 将光标放在第一行 somethings 上。
    在插入模式之外按 -v 进入可视模式。`
  2. 向下滚动到底行的 a。所有 3 行的所有三个起始字符都应突出显示。
  3. A 追加或按 I 直接进入插入模式并开始输入。当您点击退出时,您的更改应该会反映出来!您还可以执行其他命令,例如 yd 等。

I'm not quite sure what you're attempting to accomplish. Your title suggests a common pattern, but I don't see one in your example. So I'll show you both.

Making Changes Among Things With A Common Pattern

You can do search and replace with the following:

:s/<regex you are searching for>/<string to replace with>/g

s/pattern/replacement/ does search & replace, and the extra g will propogate the changes

Multi-Line Edit

Vim also lets you edit multiple lines at once. Say you want to edit the following three lines:

something = someother thing
other thing = third thing
another thing = fourth thing
  1. Put your cursor on the s at the first something line.
    Press <ctrl>-v outside of insert mode to go into Visual mode.`
  2. Scroll down to the a on the bottom line. All three starting characters of all 3 lines should be highlighted.
  3. Press A to append or I to enter directly into insert mode and start typing. When you hit escape your changes should reflect! You can also do other commands like y and d, etc.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文