I try to do something similar to this question.

E.g. I have a python script in a file /tmp/testme.py which should do different things depending on what argum ents are passed to it:

import vim

currentline = int(vim.eval('line(".")'))

if vim.argument == "close": # quasi code here
    mylines = [ 'this is the script',
                'and I say good bye!' ]
    mylines = [ 'this is the script',
                'and I say hello!' ]

vim.current.buffer[currentline:currentline] = mylines

And I intend to call it from vim with

:py3f /tmp/testme.py "close"

for the if branch or

:py3f /tmp/testme.py 

for the else branch. Is there a way to pass arguments to scripts called with pyf or py3f?

The syntax I suggest above fails because "close" is understood as second filename and vim tells me off

E172: Only one file name allowed


FileNotFoundError: [Errno 2] No such file or directory: "/tmp/testme.py 'close'"

:help :pyfile describes the mechanism to pass arguments as such:

To pass arguments you need to set sys.argv[] explicitly. Example:

:python import sys
:python sys.argv = ["foo", "bar"]
:pyfile myscript.py
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.