Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expand variables in vim commands?

I'm trying to get a variable expanded in a command call. Here's what I have in my .vimrc:

command! -nargs=1 -complete=dir TlAddPm call s:TlAddPm(<f-args>)
function! s:TlAddPm(dir)
    let flist = system("find " . shellescape(a:dir) . " -type f -name '*.pm' | sort")
    TlistAddFiles `=flist`
endfun

At the : prompt, the `=flist` syntax seems to work (or, at least it did with a w: variable), but in the .vimrc file it doesn't — TlistAddFiles is just passed the string `=flist`.


Thanks to Andrew Barnett's and Mykola Golubyev's answers, I've now got this, which appears to work. Is there no better way?

command! -nargs=1 -complete=dir TlAddPm call s:TlAddPm(<f-args>)
function! s:TlAddPm(dir)
    let findres = system("find " . shellescape(a:dir) . " -type f -name '*.pm' | sort")
    let flist = []
    for w in split(findres, '\n')
        let flist += [ fnameescape(w) ]
    endfor
    exe "TlistAddFiles " . join(flist)
endfun
like image 611
derobert Avatar asked Mar 20 '09 15:03

derobert


2 Answers

Try just

let joined = join(split(flist))
exec 'TlistAddFiles '.joined

To your edit:

 let flist = split(findres, '\n')
 call map(flist, 'fnameescape(v:val)')
like image 89
Mykola Golubyev Avatar answered Sep 23 '22 23:09

Mykola Golubyev


Something like

exe "TlistAddFiles `=".flist

might work.

like image 38
Andrew Barnett Avatar answered Sep 21 '22 23:09

Andrew Barnett