简体   繁体   English

使用vimrc函数传递python参数

[英]Using vimrc function to pass python arguments

I am attempting to create a vimrc function that will be used in an autocmd . 我正在尝试创建将在autocmd使用的vimrc函数。 The function must simply call a python script and pass the file name as an argument. 该函数必须简单地调用python脚本并将文件名作为参数传递。

.vimrc .vimrc

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

example.py example.py

#!usr/bin/python
import sys

print sys.argv[1]

If I uncomment the echo line, example.py is echo'd correctly. 如果我取消注释回显行,则example.py将正确回显。 If I try to execute as it is displayed above, however, the string filename is passed literally. 但是,如果我尝试执行上面显示的内容,则字符串filename名将按字面传递。

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: 当然,您可以使用execute命令执行一个字符串,该字符串是根据您想要的命令和变量串联而成的:

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 我已经在您的Test函数中省略了filename参数,因为它似乎没有被使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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