2

I'd like to have a directory which includes a range of vim functions written in python.

I'd like to then be able to call these functions in my .vimrc, and run them through :<FuncName>

I've looked at a number of resources online, and while it is quite easy to get python functionality working if it is written in your .vimrc, I'm struggling to bring in outside files.

I have the following file:

/home/daniel/vim/hey_vim.py

def HeyVim():
    print("Hey Vim")

In my .vimrc I have the following:

function HeyVim()

pyfile /home/daniel/vim/hey_vim.py

endfunction

:command HeyVim :call HeyVim()

I've also tried the following variations:

  • Quoting the filepath in the vimscript function
  • Removing colons from the command
  • Various path expansions to get to hey_vim.py
  • Quick file creation instead of print to see if it was just a printing issue
  • Changing file name from hey_vim.py to HeyVim.py

When I run :HeyVim, nothing happens. There are no errors, but also nothing is printed, and no file is created when that part is in there.

Any ideas?

1 Answer 1

2

In the actual state:

  • What does your Vim function? It parses your Python file.
  • What does your Python file? It defines a new python function.

So when you run your :HeyVim command as you wrote it, you simply add a new Python function into the context. You could then use it like this: :py HeyVim().

You should run :pyfile outside of the HeyVim() Vim function, so it runs during the Vim initialization process. Then run :py HeyVim() inside your Vim function, something like this:

pyfile /home/daniel/vim/hey_vim.py

function HeyVim()
    py HeyVim()
endfunction

command HeyVim call HeyVim()
Sign up to request clarification or add additional context in comments.

1 Comment

It would be easier to just change the command to command HeyVim py HeyVim().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.