DerWeh
DerWeh

Reputation: 1821

getting Vim directory for vimrc file

How do I get the directory Vim is 'looking into'? I want to set up my vimrc platform independent so I don't want to put explicit directories. On Linux e.g. vim looks into ~/.vim and on Windows in ~\vimfiles. How can I get the directory that is used, to put my files there?

Upvotes: 2

Views: 753

Answers (2)

Luc Hermitte
Luc Hermitte

Reputation: 32926

I can't say this is an information we never need as I have one use for it.

In lh-vim-lib, I proceed this way (in lh#path#vimfiles()):

  • First I build a regex from $HOME (i.e. I replace all slashes and backslahes into [/\\]): :let regex = substitute($HOME, '[/\\]', '[/\\\\]', 'g').
  • Then I append the possible directories: let regex .= '\(vimfiles\|.vim\)'
  • And i finally, I search for my regex into &rtp. The shortest result is the good one. :let paths = filter(split(&rtp, ','), 'match(v:val, regex) != -1') + :return paths[lh#list#arg_min(paths, function('len'))]

Note: The real code is a little bit more complex, generic and resilient (/resistant?) to errors.

PS: because of cygwin, testing for has('win32') is not enough.

EDIT: I've been asked to provide lh#list#arg_min() definition. It can be found in lh-vim-lib. Its definition is quite simple and a little bit cumbersome (IMO)

function! lh#list#arg_min(list, ...) abort
  if empty(a:list) | return -1 | endif
  let Transfo = a:0 > 0 ? a:1 : function(s:getSNR(id))
  let m = Transfo(a:list[0])
  let p = 0
  let i = 1
  while i != len(a:list)
    let e = a:list[i]
    let v = Transfo(e)
    if v < m
      let m = v
      let p = i
    endif
    let i += 1
  endwhile
  return p
endfunction

Upvotes: 1

Ingo Karkat
Ingo Karkat

Reputation: 172510

You can override the default 'runtimepath' to make Vim use the same ~/.vim/ on Windows, too. Just put this into your ~/.vimrc:

if has('win32') || has('win64')
    let &runtimepath = substitute(&runtimepath, '\C\V' . escape($HOME.'/vimfiles', '\'), escape($HOME.'/.vim', '\&'), 'g')
endif

Upvotes: 0

Related Questions