0

Relatively new to vim search/replace commands and wondering how to replace part of matched string via vim commands.

E.g. If I have

printf(_UNICODE("Text and %d I want to preserve."), 20);

and I wish to get rid of the Unicode conversion and make it become

printf("Text and %d I want to preserve.", 20);

which means getting rid of the _UNICODE( and the ) but preserving the format string in between.

Can someone please help me come up with a vim command to do that and possibly explain?

Thanks a ton in advance! :)

5
  • Try this sequence /_UNICODE⏎2wdi(2bvt,p Commented Sep 7, 2017 at 23:04
  • 1
    This one is better /_UNICODE⏎f"di(2bvt,p Handles the case if the format string starts with ( Commented Sep 7, 2017 at 23:13
  • @balki Is there a way to apply these to all the occurrences in a file like %s....? Commented Sep 7, 2017 at 23:13
  • You can record in a macro and run it multiple times. But not like with %s Commented Sep 7, 2017 at 23:17
  • @balki Thanks for your response. I'd prefer an %s solution but I upvoted yours too. Commented Sep 7, 2017 at 23:26

1 Answer 1

1
:s/_UNICODE(\([^)]*\))/\1/g

or

:%s/_UNICODE(\([^)]*\))/\1/g

Match "_UNICODE(" + not-a-closing-bracket + ")" and replace with just the not-a-closing-bracket part.

(%s to do all lines instead of current line)

Sign up to request clarification or add additional context in comments.

5 Comments

This will break if the text in the format string contains a )
Is \1 a register here? When I typed it in it replaced the not-a-closing-bracket part with "\1"... am I missing something here?
@user3291342: The \1 should refer to the part of the regular expression between the first (in this case, only) ( and ) bits. I tested in MacVim and an HPUX vi and it works for me. Are you sure you copied the string exactly?
@balki: That's a fair criticism. I am assuming a relatively known/simple use case. It could be modified to treat ") as the delimiter or made even more complicated to handle potential escaping of the " and ).
@EdmCoff That did not work on my editor vim plugin but worked in actual vim. Sorry for the confusion.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.