How do I get Vim home directory?

12

1

I wanted to set the VIMHOME variable this way (common to Windows and Linux),

let $VIMHOME=expand("%:p")."/..",

so that VIMHOME is "~/.vim" in Linux or "path/to/vimfiles" in Windows.

I put this in a var.vim file and placed this in the plugin directory. It loads properly, but VIMHOME is set only to "./..".

How do I get the full path of a file using expand?

Is there an easy way to set VIMHOME?

Edit: I changed the expression to:

let $VIMHOME=expand("<sfile>:p:h")

Now, VIMHOME is set to "~/.vim/plugin" in Linux.

My requirement is setting VIMHOME to "~/.vim" or "path/to/vimfiles". But,

let $VIMHOME=expand("<sfile>:p:h")
let $VIMHOME=expand("$VIMHOME:p:h")

is not working.

How can I resolve this?

asdfg

Posted 2010-03-15T05:11:40.723

Reputation: 2 266

Add another :h. I have updated my answer. – Chris Johnsen – 2010-03-15T09:30:44.153

oh.. Thanx thats working as well.. – asdfg – 2010-03-15T09:55:04.973

Answers

17

Put this in your var.vim plugin file:

let $VIMHOME=expand('<sfile>:p:h:h')

% in expand will refer to the file being edited (i.e. the pathname given on the command line). <sfile> will refer to the file being ‘sourced’ (i.e. the plugin or startup file that is active when the expansion is made).

The :p modifier makes the pathname absolute and the :h suffix drops the last pathname component (i.e. the filename in this case).

Chris Johnsen

Posted 2010-03-15T05:11:40.723

Reputation: 31 786

3Just a heads up, if you expand('<sfile>') in a function, it will probably not return what you are expecting. The solution is to expand('<sfile>') outside of the function in your script (in a g:sfile variable or something, use :help instance-variables for more info) and then use that variable in the funtion. – Jaymon – 2013-04-15T01:59:09.540

Jaymon's advice really helped after many frustrating hours. Set a global variable outside of a function. – Dylan – 2014-05-22T04:57:49.677

4

This is what I was looking for,

if has('win32') || has ('win64')
    let $VIMHOME = $VIM."/vimfiles"
else
    let $VIMHOME = $HOME."/.vim"
endif

asdfg

Posted 2010-03-15T05:11:40.723

Reputation: 2 266

Not always true: some people have vim installed in (and hence $VIM set to) C:\Program Files\Vim and the settings in C:\Documents and Settings\%USERNAME%\vimfiles. A safe way is to have a file installed in vimfiles and use globpath(&rtp, filename). – Al. – 2010-03-15T13:44:14.370

The accepted answer works properly.. thanx – asdfg – 2010-03-17T05:19:58.893

@asdfg: Your source link http://zd-xp1:8080/ZohoSync/ is bogus.

– Aaron Thoma – 2014-01-21T09:26:26.357