How to get path to the current vimscript being executed

后端 未结 3 1822
无人及你
无人及你 2020-12-02 08:33

In my vim plugin, I have two files:

myplugin/plugin.vim
myplugin/plugin_helpers.py

I would like to import plugin_helpers from plugin.vim (u

3条回答
  •  难免孤独
    2020-12-02 09:25

    It is worth mentioning that the above solution will only work outside of a function.

    This will not give the desired result:

    function! MyFunction()
    let s:current_file=expand(':p:h')
    echom s:current_file
    endfunction
    

    But this will:

    let s:current_file=expand('')
    function! MyFunction()
    echom s:current_file
    endfunction
    

    Here's a full solution to OP's original question:

    let s:path = expand(':p:h')
    
    function! MyPythonFunction()
    import sys
    import os
    script_path = vim.eval('s:path')
    
    lib_path = os.path.join(script_path, '.') 
    sys.path.insert(0, lib_path)                                       
    
    import vim
    import plugin_helpers
    plugin_helpers.do_some_cool_stuff_here()
    vim.command("badd %(result)s" % {'result':plugin_helpers.get_result()})
    
    EOF
    endfunction
    

提交回复
热议问题