Vimscript: getline(): a file's lines into an array

getline() can take two parameters: the start position in a file, and an end position in a file.

getline(1, '$') will get all the lines from line 1 until the end. And it puts this in an array/list.

We can use the previously learnt filter() function and regex operator to filter all the lines with a word in them:

let bicycles = getline(1, '$')->filter({ _,line -> line =~ '.*bicycle.*' })

Then you'd loop over this as normal.

You can use getline() to get the current line as a string with getline(".") or pass it a line number to get that line.

vimscript

Edit on github
comments powered by Disqus
Click me