kennedyl kennedyl - 4 months ago 14
Python Question

Using vimrc function to pass python arguments

I am attempting to create a vimrc function that will be used in an autocmd. The function must simply call a python script and pass the file name as an argument.

.vimrc

fu! Test(filename)
let filename = expand("%:t")
"echom filename
!test.py filename


example.py

#!usr/bin/python
import sys

print sys.argv[1]


If I uncomment the echo line, "example.py" is echo'd correctly. If I try to execute as it is displayed above, however, the string "filename" is passed literally.

Is there any way around this?

Answer

Sure, you can use the execute command to execute a string, which is built from the command you want and the variable concatenated together:

fu! Test()
    let filename = expand("%:t")
    execute "!test.py " . l:filename
endfunction

I've omitted the filename argument in your Test function because it doesn't seem to be used