Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
760 views
in Technique[技术] by (71.8m points)

replace - How to change a string in the vim editor?

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...