0

I am writing a function and I wish to yank a range of lines into a register. The line that I cannot fix is the following:

let @" = getline(l:line_in,l:line_out)

where l:line_in and l:line out are line numbers. The error that I get is E730: Using a List as a String. In my understanding, the problem relies in getline(a,b) that returns a list with all the lines between line number a and line number b, but then how shall I convert it to fit into a register?

1 Answer 1

1

You can use the join() function to join an array to a string:

let @" = getline(l:line_in, l:line_out)->join("\n")

In this case, you probably want to join them by newlines as text; alternatively, you can use the string() function to get the representation as an array literal ([…]):

let @" = getline(l:line_in, l:line_out)->string()

This can be useful for debugging things and such. printf() is also useful for this.


Example:

:echo getline(1, 3)->join("\n")
/* vi:set ts=8 sts=4 sw=4 noet:
 *
 * VIM - Vi IMproved    by Bram Moolenaar

:echo getline(1, 3)->string()
['/* vi:set ts=8 sts=4 sw=4 noet:', ' *', ' * VIM - Vi IMproved by Bram Moolenaar']
2
  • Many thanks! join is what I needed!
    – Barzi2001
    Oct 2, 2022 at 19:42
  • Possibly :help setreg() is better
    – D. Ben Knoble
    Oct 3, 2022 at 0:20

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.