0

I have a multi-line string that I'm reading by:

let a = getreg('*')

What would be the best way to get the first line of that string? I was hoping I could do something like:

  • match
  • getbufline

But am having trouble finding the necessary string function to do this. In python it'd be something like: str.split('\n')[0]

If helpful, when I do echom a the output looks like:

enter image description here

Also, what is ^@ in vim? I've seen ^M for linebreak (return) but have never seen ^@ before.

Update: After some investigation it seems like that's the nul or \0 byte, and can be entered in by doing ctrlv ctrl@. And I ended up using this:

let first_line = matchstr(register, '\v<[^\n]+>')

It's very odd that this regex is what's used, as I tried doing something basic like `\v^.+$' and although it would work on a search page, it returned no results when I tried to search the register with it...

1

Normally I prefer let a = getreg('*', 1, 1)[0] which is a bit shorter than let a = split(getreg('*'), "\n")[0] and does the same thing.

However, in your case the register contents for some reason contains Nul bytes (see :h NL-used-for-Nul). Hence you have to split on Nul manually: let a = split(getreg('*'), '^@')[0]

2
  • oh that's so much better/easier than what I came up with, thank you! What is the second arg in getreg ? In :h it just says 1 and nothing else (the third argument seems to be whether to return it as a string (default) or list (1).
    – David542
    Jun 14 '20 at 4:19
  • @David542 It's for expression register (=) only. getreg('=', 1) will return expression itself, not its result, if I remember it correctly.
    – Matt
    Jun 14 '20 at 4:23
1
^@

The null byte 0x00. Due to various things at the source-code level, newlines get embedded as null bytes in certain things (not all strings, since you can use ^M, as you know).

4
  • I would very much appreciate a more detailed answer that could explain why and when this occurs, but I don’t have it, so I’ll leave this here in the meantime
    – D. Ben Knoble
    Jun 14 '20 at 3:34
  • Ben -- cool, yes I learned how to enter that in too, which was cool. So, would the best way to get the first line be to do: a[0:match(a, '^@')-1] ? That seems a bit hack-y but wasn't sure if there was a better way.
    – David542
    Jun 14 '20 at 3:36
  • although when I actually run it in the function, echom match(a, '^@') that fails with a `Missing quote: '` (though it runs on the command input??)
    – David542
    Jun 14 '20 at 3:47
  • @David542 honestly im not sure
    – D. Ben Knoble
    Jun 14 '20 at 13:13

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.