I have a file (myWords.txt) which contains one word on each line :

Word 1
Word 2
Word 3000

With this file, I create a list :

function! MyFunction()
        let start = reltime()
        exe 'e myWords.txt'
        let myList = []
        exe '%s/.*/\=add(myList, submatch(0))/g'
        exe 'bd!'
        echom printf('Duration = %s seconds', reltimestr(reltime(start))) 

It takes me 34.1666367 seconds

What can I do to download my list more quickly ? Could you help me, please ?

2 Answers 2


I have a file (myWords.txt) which contains one word on each line

Then simply

let mylist = readfile('myWords.txt')
  • 1
    Note that for the current buffer we can directly use getline(1, '$'). and there are other functions to fetch the lines in the other loaded buffers. See :h functions Mar 25, 2020 at 17:29

Matt's answer is the best way to do this, but here's an alternative. After you load your file into the buffer, you can call getline() which returns the lines you ask for as a List:

let myList = getline(1, '$')

As a side note, I'd also like to point out that none of your uses of exe are necessary. You can write all of these Ex commands directly into your function:

    e myWords.txt
    %s/.*/\=add(myList, submatch(0))/g
  • To be fair, :exe becomes necessary when the pattern is a variable. But then we could also play with getline()/readfile() + filter(). Mar 25, 2020 at 17:31

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.