I am having a tough time finding and replacing a string using vi. Source String:


New String after replace:


I can find &DB2_DATA; and replace it with &DB2_DATA;:tcp=true, but that is not what I need to do.

Can someone please help me understand the appropriate ':s' command in order to achieve this replacement?

  • 2
    If that's not what you need to do, could you add some more sample data, to make it clearer what it is you're asking?
    – Herb
    Commented Oct 26, 2017 at 21:43

2 Answers 2


This should do what you want:

  • Thanks a ton Karl !! It did help, work and save my time too. I always wanted to learn all about this. Is there a good online/book documented you can suggest to gain more knowledge of it ?
    – Maneesh
    Commented Oct 28, 2017 at 5:35
  • The Vim documentation is good. Start with ":h :s". Commented Oct 29, 2017 at 7:04

As you state, your string contains / so presumably that is tripping you up since it must be escaped if you use / to delimit the sections of your substitution command. See the other answer here for solution with escaping. But we don't have to use / as our separator. We can use any non-alphanumeric, single-byte character except \, " or |. For example, with #...


Note: In a substitution & is replaced with whatever string was matched by your pattern.

  • Thank you Layer !! This worked too.. Kindly suggest me some good online content I can go through about it.. Regards, Maneesh Sharma
    – Maneesh
    Commented Oct 28, 2017 at 5:53
  • @Maneesh, if you're looking for help in general look no further than Vim itself. It has great help files and also has a tutor which, if things are installed properly, you can run from command line with vimtutor. If you want more after the tutorial enter :help while in vim and start working your way through the "BASIC" section and then the user manual.
    – B Layer
    Commented Oct 28, 2017 at 6:42
  • 1
    If you just want help with things like substitution than do :help usr_10.txt and you'll read about changing text with substitution, macros, ranges, the awesome 'global' command, and more.
    – B Layer
    Commented Oct 28, 2017 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.