Bentley4
Bentley4

Reputation: 11028

Get absolute path of myvim.vim file in Python code block

I'm trying to write a vim plugin that uses a Python code block inside of it. I would like to get the full path of myvim.vim (/home/myusername/.vim/bundle/myvim/plugin/myvim.vim) inside of my python code block. Unfortunately you can't get the path by using __file__ as in a .py file. I can't use vim.command(':pwd') either because that just prints the path of the location where the plugin function is called from.

myvim.vim

function! Myvim()

python << EOF
import vim

vim_path = "full myvim.vim path here"
print vim_path

EOF

endfunction

EDIT

@actionshrimp, I'm trying this:

myvim.vim

function! Myvim()
    let s:curfile = expand("<sfile>")
    let s:curfiledir = fnamemodify(s:curfile, ":h")

python << EOF
import vim

py vim_path = vim.eval('expand("<sfile>")')
print vim_path

EOF

endfunction

Upvotes: 1

Views: 451

Answers (1)

actionshrimp
actionshrimp

Reputation: 5229

You can use <sfile> to get the path of the currently executing vimscript, like so:

let s:curfile = expand("<sfile>")
let s:curfiledir = fnamemodify(s:curfile, ":h")

To pass that to python you should be able to use:

py vim_path = vim.eval('expand("<sfile>")')

or if you've set the variable:

py vim_path = vim.eval('s:curfile')

For clarity here's a full example (saved as 'D:\tmp\test.vim'):

python << EOF
import vim

vim_path = vim.eval('expand("<sfile>")')
print vim_path

EOF

When I have it open and type :so % it shows 'D:\tmp\test.vim' at the bottom.

Upvotes: 3

Related Questions