0

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))) 
endfunction

It takes me 34.1666367 seconds

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

6

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

Then simply

let mylist = readfile('myWords.txt')
| improve this answer | |
  • 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 – Luc Hermitte Mar 25 at 17:29
3

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
    bd!
| improve this answer | |
  • To be fair, :exe becomes necessary when the pattern is a variable. But then we could also play with getline()/readfile() + filter(). – Luc Hermitte Mar 25 at 17:31

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.