0

How can I substitute something with empty string? So far the best I can do seems to be substituting with a whitespace. If I write :s/target// then it's deemed invalid.

closed as off-topic by Martin Tournoij Sep 8 '16 at 21:26

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers." – Martin Tournoij
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 6
    The command work well for me. What error message do you get? – statox Sep 8 '16 at 15:39
  • 1
    @statox Actually it worked this time. I'm not sure what happened previously. The solution posted by Tumbler41 also worked. – xji Sep 8 '16 at 15:52
  • 4
    BTW, :s/target is even shorter :) – VanLaser Sep 8 '16 at 16:52
  • 1
    @VanLaser Cool. But that doesn't work in cases where I want to append flags, apparently. – xji Sep 8 '16 at 18:10
  • 1
    Downvotes without explanation? Guess this question shouldn't have existed in the first place anyways then. Since it can't be deleted by myself already, I'm flagging for the mods to deal with it. – xji Sep 8 '16 at 18:10
4

Your command should work but you could also try this as an alternative:

:s/target/\=''/
  • \= tells vim to evaluate the replace string as an expression
  • '' Is an empty string
  • 1
    Actually it worked this time. Your solution also worked. – xji Sep 8 '16 at 15:52
  • 1
    @tumbler41 from :h :s%: When {string} is equal to "%" and '/' is included with the 'cpoptions' option, then the {string} of the previous substitute command is used. ({string} is defined as :[range]s[ubstitute]/{pattern}/{string}/[flags] in :h :substitute) . And empty replacement string should work :-) – statox Sep 8 '16 at 15:55
  • 2
    Ok, I was thinking of if you omit the first pattern string, then it uses the last pattern for that. I'll remove that from my answer. – Tumbler41 Sep 8 '16 at 16:03

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