Santosh Kumar
Santosh Kumar

Reputation: 27875

conflict in configuration file paths between Linux and Windows

I have this line in my .vimrc file:

set directory=~/.vim/swapfiles//

(Note that one extra slash makes the directory names to be included instead of just file names to reduce conflict)

The above config works fine on my Linux machine, but the problem is when I use the same file on Windows I get some warning about the file cannot be read. That's probably because vim is looking for ~/.vim/swapfiles/ directory, which Windows unfortunately don't have.

Is there any way to store my swap files on Windows somewhere (better if it could be in C:\Program Files\Vim\vimfiles\)?


CASE #2

If you got answer for my above question, here is the another one. I also have some lines similar to this:

autocmd filetype python source ~/path/to/file/python.vim

Windows confuses at this point too. How can I patch up?

Upvotes: 3

Views: 931

Answers (4)

Geoff Reedy
Geoff Reedy

Reputation: 36011

Maybe what you want is to have directory set based on the entries in runtimepath? Try this:

let &directory = substitute(&rtp, ",", "/swapfiles//,", "g")

With the default runtimepath setting on Unix-y systems you get

$HOME/.vim/swapfiles//,
$VIM/vimfiles/swapfiles//,
$VIMRUNTIME/swapfiles//,
$VIM/vimruntime/after/swapfiles//,
$HOME/.vim/after//

and on Windows

$HOME/vimfiles/swapfiles//,
$VIM/vimfiles/swapfiles//,
$VIMRUNTIME/swapfiles//,
$VIM/vimfiles/after/swapfiles//,
$HOME/vimfiles/after/swapfiles//

I agree, the after directories are unwanted, but vim will pick the first directory that exists and allows file creation so if you don't create the swapfiles sub-directory they won't be touched.

You might want to consider prepending these to the default value instead of replacing it so the defaults are available as a fallback if none of the directories exist.

I don't understand why you have the auto commands you mention. The default settings of runtimepath with filetype plugin on should take care of that for you.

Of course you always have the option of explicitly checking the platform and using different settings as in

if has("win32")
  " settings for windows
elif has("win32unix")
  " settings for cygwin
elif has("unix")
  " settings for unix
elif has("macunix")
  " settings for macosx
endif

If you want to avoid an error if a file does not exist then you can use the following definitions in your vimrc

func! Source(file)
  if filereadable(a:file)
    exec "source " . fnameescape(a:file)
  endif
endfunction

com! -nargs=1 -complete=file Source call Source(<f-args>)

and change source to Source when the file might not exist.

Upvotes: 1

Ingo Karkat
Ingo Karkat

Reputation: 172570

If you don't want to introduce a $MYVIM variable as ZyX suggests, maybe an alternative is placing the runtime files in $HOME/.vim instead of the default $HOME/vimfiles on Windows. You can then use ~/.vim/... everywhere. This also helps with synchronizing the files across multiple mixed-platform machines.

" On Windows, also use '.vim' instead of 'vimfiles'; this makes synchronization
" across (heterogeneous) systems easier.
if has('win32') || has('win64')
    set runtimepath=$HOME/.vim,$VIM/vimfiles,$VIMRUNTIME,$VIM/vimfiles/after,$HOME/.vim/after
endif

Upvotes: 4

Ingo Karkat
Ingo Karkat

Reputation: 172570

I fully agree with Geoff Reedy that the autocmd shouldn't be necessary. In other cases, you can use :runtime instead of :source. It will automatically search all (user and system directories in 'runtimepath'.

Upvotes: 0

ZyX
ZyX

Reputation: 53614

First of all, vim translates all forward slashes to backward on windows thus it won’t hurt having slashes. Just in case you think it can be a source of trouble.

Second, it is not impossible to have ~/.vim and all other directories on windows, just some programs don’t want to work with names that start with a dot. You may just add this to runtimepath as it is not there by default on windows and move all user configuration there.

Third, in most places you have a filename you may use $ENV_VAR. It includes setting &rtp, &directory and using :source. So the solution may be the following:

if has('win16') || has('win95') || has('win32') || has('win64')
    let $MYVIM=$HOME.'/vimfiles'
else
    let $MYVIM=$HOME.'/.vim'
endif
set directory=$MYVIM/swapfiles
autocmd FileType python :source $MYVIM/after/ftplugin/python.vim

(though I agree with @Geoff Reedy that there should be no need in using the last line).

And last, modifying and storing something in C:\Program Files is not the best idea, neither for you nor for the program itself (by the way, vim won’t modify or store something there unless you told it to). It is to be modified by installation, update and uninstallation processes, not by anything else.

Upvotes: 0

Related Questions