datavoredan
datavoredan

Reputation: 3746

run external python function as vim command

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:

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?

Upvotes: 2

Views: 1116

Answers (1)

yolenoyer
yolenoyer

Reputation: 9445

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()

Upvotes: 2

Related Questions