Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
Add another :h. I have updated my answer. –  Chris Johnsen Mar 15 '10 at 9:30
    
oh.. Thanx thats working as well.. –  asdfg Mar 15 '10 at 9:55
add comment

2 Answers

up vote 6 down vote accepted

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

share|improve this answer
3  
Just 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 Apr 15 '13 at 1:59
    
Jaymon's advice really helped after many frustrating hours. Set a global variable outside of a function. –  Dylan May 22 at 4:57
add comment

This is what I was looking for,

if has('win32') || has ('win64')
    let $VIMHOME = $VIM."/vimfiles"
else
    let $VIMHOME = $HOME."/.vim"
endif
share|improve this answer
    
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. Mar 15 '10 at 13:44
    
The accepted answer works properly.. thanx –  asdfg Mar 17 '10 at 5:19
    
@asdfg: Your source link zd-xp1:8080/ZohoSync is bogus. –  accolade Jan 21 at 9:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.