1

I have to find a word and append some other text to in in Vim: for instance I have to append to every occurrence of the word "public_html" the text "/Text-Analysis-Tool" to obtain "public_html/Text-Analysis-Tool".

I've tried using the following command:

:%s/public_html/public_html/Text-Analysis-Tool/gg

But is is producing the error E488: trailing characters.

Can someone please tell me how can I do this?

  • 1
    You can use another separator character, instead of /. For instance, :%s#public_html#public_html/Text-Analysis-Tool#gg. – guillem Apr 7 '15 at 10:58
9

The problem here is that Vim is interpreting the / character in your replacement as a delimiter character used by the :substitute command. The syntax of the :s command is as follows:

:[range]s/pattern/replacement/flags

The above syntax uses the / character to delimit pattern, replacement, and flags; however, some other characters (usually punctuation or symbols) can be used.

To fix your problem, you have two options.

Option 1

Escape any / characters in your pattern and replacement using \:

:%s/public_html/public_html\/Text-Analysis-Tool/gg

Option 2

Use a different delimiter character. To avoid escaping, choose one that does not appear in your pattern or replacement. For this case, let's use #:

:%s#public_html#public_html/Text-Analysis-Tool#gg

Bonus

Appending to a search pattern is a special situation where we can use some features that are fairly unique to Vim's flavour of regex. Vim has zero-width anchors \zs and \ze that define the start and end (respectively) of the part of your pattern that you want to match. Anything before \zs or after \ze in your pattern will not be part of the patch, and thus will remain unaffected by replacement.

Instead of repeating the pattern part in the replacement, we can define the start of the match as the end of the pattern. Below, I'll continue with the technique from Option 2 to avoid escaping.

:%s#public_html\zs#/Text-Analysis-Tool#gg

You can look at it like this: we're replacing an empty string that follows public_html with /Text-Analysis-Tool.

Note that omitting either of these zero-width anchors is like having \zs at the start of the pattern and/or \ze at the end of the pattern.


Side note

I noticed you're using gg for your flags. This is identical to not using any g in your flags at all -- each g inverts the global behaviour. If this wasn't merely a typo, I suspect that you might have the 'gdefault' option enabled. This has the effect that using g will make the substitution not global, and no g in flags (or gg) will make it global.

Turning this option off (rather, not turning it on, as it's off by default) will let you use a single g for the global behaviour.

  • 6
    Even more bonus: Use & to replace with the entire match. e.g. :%s#public_html#&/Text-Analysis-Tool#. For more help see :h /\0 – Peter Rincker Apr 7 '15 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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