3

How do I replace all occurences of a string that are not followed by a forward slash character in vi (or vim)? I want all occurences of '<[text]', but NOT '</[text]' to be replaced with '<db:[text]' Here's what I tried, escaping / and not escaping:

%s/<\(\/\)\@!/<db:/
%s/<\(/\)\@!/<db:/

Result is the same:

E488: Trailing characters

Per comment below, the command I was actually trying that didn't work was:

    4,$s/<\(\/\)\@!/<db:/

The first one above, escaping / works just fine.

4
  • 1
    Welcome to Vi and Vim! I'm trying the first command you gave and it works for me. Second fails with E54: Unmatched \(. Regarding E488, see this for what help has to say about it, it seems to be about an Ex command. Are you sure that's the whole command you're using there? Any more details you could give? Please edit your question to add more, if needed.
    – filbranden
    Jun 7, 2022 at 0:19
  • @filbranden Thank you! That helped me find the issue. I updated my question. If you want to post your response as an answer below, I'll accept it. :-)
    – Lidia
    Jun 7, 2022 at 0:26
  • 3
    Since you're only using character before the \@!, you don't need the parenthesis at all; e.g. :%s<\/\@!db:/ should work. You can also use e.g. # instead of / to reduce the number of escapes required, making things a bit easier: :%s#</\@!#db:#. You only need the parens if you've got more than one character. Jun 7, 2022 at 2:24
  • 1
    It turns out I didn't have any problems with 4,$s/<\(\/\)\@!/<db:/ either... Not sure what's going on with your specific case. Like Martin mentioned above, it gets much easier if you use a different delimiter such as # and you can skip the grouping.
    – filbranden
    Jun 7, 2022 at 2:46

0

Your Answer

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

Browse other questions tagged or ask your own question.