2

I am working with gvim on WSL2 on Windows 10 and I receive strings of the form:

some_text^M^@some_other_text^M^@more_text

when I use split(message, '\r'), where message is a variable containing the above string, I have the following list:

['some_text', '^@some_other_text', '^@more_text']

It looks like that the ^M is correctly recognized and the string is split correctly, but there is that ^@ that I cannot eliminate.

What I wish to get is the following list:

['some_text', 'some_other_text', 'more_text']

I tried with substitute(message, '\^@', '', 'g') along with some variants of it, but none worked.

EDIT: I just tried substitute(message,'[^[:print:]]', '', 'g') and it seems to work for a very specific use-case, but it does not seems a robust solution... at all!

2
  • Where do you receive the strings from? If it's system(), have you tried systemlist()?
    – D. Ben Knoble
    Commented May 14 at 14:24
  • I receive it from a pty.
    – Barzi2001
    Commented May 14 at 15:34

1 Answer 1

2

^@ can't work as a pattern in this case because it would match the sequence of characters ^ then @ while what you want to match is a literal ^@, or "null character". There are various ways to match that character in Vim, my preferred would be \%x0, see :help \%x.

Since the second argument of :help split() is a regular expression, you can simply adjust your existing split() call:

split(message, '\r\%x0')
4
  • I accepted your answer because it works on wsl2, but will it works on other platforms? Do you think that splitting based on “one or more unprintable characters” (e.g. [\x00-\x1F\x7F-\xFF]+ may be more robust approach among different platforms?
    – Barzi2001
    Commented May 16 at 4:39
  • Or, even better, based on \r | \r\x%0 ?
    – Barzi2001
    Commented May 16 at 4:46
  • 1
    Your question was specific, so the answer is also specific. If the EOLs of the text you receive differ between platform, then you will definitely have to adjust your pattern accordingly.
    – romainl
    Commented May 16 at 4:46
  • Make sense. I‘ll see how to deal with EOL on different platforms, including when receiving that null character that I mentioned at the beginning.
    – Barzi2001
    Commented May 16 at 5:06

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.