Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get path to the current vimscript being executed

Tags:

vim

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 (using the vim python support), so I believe I first need to put the directory of my plugin on python's sys.path.

How can I (in vimscript) get the path to the currently executing script? In python, this is __file__. In ruby, it's __FILE__. I couldn't find anything similar for vim by googling, can it be done?

Note: I am not looking for the currently edited file ("%:p" and friends).

like image 713
gfxmonk Avatar asked Feb 12 '11 06:02

gfxmonk


People also ask

How to see current file path in vim?

If all that is wanted is to display the name of the current file, type Ctrl-G (or press 1 then Ctrl-G for the full path). When using @% , the name is displayed relative to the current directory. In insert mode, type Ctrl-R then % to insert the name of the current file.

How do I get full path of file in Gvim?

Pressing 1 followed by Ctrl + G shows the full path of the current file.

How to check file name in vi?

ctrl + g will do it.

What does echo do in Vim?

Plain old :echo will print output, but it will often disappear by the time your script is done. Using :echom will save the output and let you run :messages to view it later.


1 Answers

" Relative path of script file: let s:path = expand('<sfile>')  " Absolute path of script file: let s:path = expand('<sfile>:p')  " Absolute path of script file with symbolic links resolved: let s:path = resolve(expand('<sfile>:p'))  " Folder in which script resides: (not safe for symlinks) let s:path = expand('<sfile>:p:h')  " If you're using a symlink to your script, but your resources are in " the same directory as the actual script, you'll need to do this: "   1: Get the absolute path of the script "   2: Resolve all symbolic links "   3: Get the folder of the resolved absolute file let s:path = fnamemodify(resolve(expand('<sfile>:p')), ':h') 

I use that last one often because my ~/.vimrc is a symbolic link to a script in a git repository.

like image 162
Zenexer Avatar answered Sep 21 '22 10:09

Zenexer