如何更改vim编辑器中的字符串? [英] How to change a string in the vim editor?

查看:930
本文介绍了如何更改vim编辑器中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如: foo到abc / def / foo 。有什么建议么?

解决方案

您的问题的标题和内容不符。



:帮助模式和:help:substitute 有点不知所措,但 正则表达式是Vim的核心。



Vim正则表达式101 是一个更简洁的资源。

无论如何,给出以下示例文本:

$ pre > bar baz foo baz bar

您的目标可以在各种各样的方法。这里有一些:

 :s / foo / \ / abc\ / def\ / foo 
:s / foo / \ / abc \ / def \ /&
:s + foo + / abc / def /&

一些注释:


  • 在替换部分中,& 代表匹配的文本。如果匹配是 foo & foo 。当然,在这里使用& 而不是 foo 好处不大,但要等到你有一个更复杂的模式因为斜杠用来分隔搜索部分和替换部分,所以你需要在这两个部分中使用实际的斜线来替换你的工作。

  • / p>
  • 您可以使用许多其他符号而不是斜杠来分隔搜索和替换部分。这是非常有用的,当你知道你需要转义太多的斜线。



I am trying to replace a string with slashes in vim editor

ex: foo to abc/def/foo. Any suggestions?

解决方案

The title and the content of your question don't match.

:help pattern and :help :substitute are a bit overwhelming but very useful as regular expressions are at the heart of Vim.

Vim regular Expressions 101 is a more concise resource.

Anyway, given the following sample text:

bar baz foo baz bar

your goal can be achieved in a variety of ways. Here are some of them:

:s/foo/\/abc\/def\/foo
:s/foo/\/abc\/def\/&
:s+foo+/abc/def/&

A few notes:

  • In the replacement part, & stands for "the matched text". If the match is foo, & is foo. Of course there's not much benefit in using & over foo, here, but wait until you have a more complex pattern…

  • Because Slashes are used to separate the search part and the replace part, you need to escape the actual slashes in both parts for your substitution to work.

  • You can use many other symbols instead of slashes to separate the search and the replace parts. This is very useful when you know you'll need to escape too many slashes.

这篇关于如何更改vim编辑器中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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