branquito
branquito

Reputation: 4054

python inside vim to obtain file list from current directory

How would I use python script to communicate with vim..

Using ultisnips plugin, I have option to include shell script or python script inside snippet definition, using !p for python for example. Now, what I am trying to do is to get list of files in current directory and put them in between <files> tag. something like this.

snippet lsf

!p 
import glob
cwd = vim.eval("getcwd()")  # or maybe vim.eval("expand('%:h')")
snip.rv = "\n".join([ "\t<files>%s</files>" % file for file in glob(cwd + "/*")])  

endsnippet

Dont pay attention to first and last lines as they are syntax for creating a snippet using ultisnips, what counts is the whole script I have in syntax highlighting.

but it throws errors. where I might be wrong, any suggestion?

Upvotes: 2

Views: 441

Answers (1)

Sean McCully
Sean McCully

Reputation: 1132

You have simply imported the glob module not the glob function and have tried calling the module as a function.

!p 
from glob import glob #Change this line as shown
cwd = vim.eval("getcwd()")  # or maybe vim.eval("expand('%:h')")
snip.rv = "\n".join([ "\t<files>%s</files>" % file for file in glob(cwd + "/*")])

This is at least the initial problem.

Upvotes: 1

Related Questions