简体   繁体   中英

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?

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

You have two options either you pass the filename directly as an argument or pass it as a local variable:

fu! Test(filename)
    "echom a:filename
    execute "!test.py ".a:filename

or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM