Vim Tips Wiki
Register
(My first article here. It began by editing Tip 939 in order to remove any advice to mess up with the HOME var. But then, this doesn't just concern Windows and Cygwin, so I felt having a new entry was appropriate.)
 
(Expand a bit, in particular to clarify that we want all files to be in a common directory even with option 2 (that's obvious, but the wording made it appear as if only option 1 allowed that))
Tag: Source edit
 
(15 intermediate revisions by 2 users not shown)
Line 1: Line 1:
  +
If you use Vim on both native Windows and Cygwin, or on several virtual machines, you have a couple of options to keep one copy of your [[vimrc]] file around, and share that copy among your systems and environments.
To share your [[vimrc]] file between all of your systems and environments, you have at least a couple of options.
 
   
  +
'''Note:''' For systems that use different line endings, you might need to use Unix line endings everywhere (which don't give problems in Vim for Windows). You can do so via [https://editorconfig.org/ EditorConfig] (recommended) or by manually setting [[File format|<code>'fileformat'</code>]].
==Option 1 (use the <code>:source</code> command)==
 
   
By using the <code>:source</code> command, you can put your vimrc file and all its dependencies in a common directory (preferably under version control) and source those common files from each vimrc that needs them. In fact, you'll very likely just source one of them (the "main" one) and that will, in turn, source others.
+
The following assumes that you put your vimrc file and all its dependencies in a common directory (preferably under version control).
   
 
==Option 1: using <code>:source</code>==
For instance, the vimrc file in your Windows home will look something like:
 
  +
By using the <code>:source</code> command, you can source your "real" vimrc (see above) from each system-specific vimrc. If your real vimrc is large, you can in fact have one "main" Vim script which in turn sources all the others as needed. Then, the system-specific vimrc's will just source the main script. (I recommend this kind of modularization even if you use option 2, below.)
   
 
For instance, the vimrc file in your Windows home will look something like:
 
<pre>
 
<pre>
 
source X:/path/to/common/dot-files/vim/my_vim.vim
 
source X:/path/to/common/dot-files/vim/my_vim.vim
 
</pre>
 
</pre>
   
 
==Option 2: using symbolic links==
For other systems, you'd have the moral equivalent of the above, using the system's file naming conventions (Note: Cygwin, in particular, may have problems with DOS-style paths, so you are probably better off with Option 2, below).
 
 
Once you have the common files available somewhere, you can make each <code>.vimrc</code> that you need a symbolic link to the common <code>.vimrc</code> "main" file (see above). Under Unix and Cygwin:
 
==Option 2 (symbolic link)==
 
 
Once you have the common files available somewhere you can make each <code>.vimrc</code> that you need be actually a symbolic link to the common <code>.vimrc</code> "main" file (see above). Under Unix and Cygwin:
 
 
 
<pre>
 
<pre>
$ cd ~
 
 
$ ln -s /path/to/common/dot-files/vim/my_vim.vim .vimrc
 
$ ln -s /path/to/common/dot-files/vim/my_vim.vim .vimrc
 
</pre>
 
</pre>
  +
  +
  +
This option, though, is only adequate if your vimrc is very simple. For instance, if it doesn't, in turn, source other files; because, in that case, Vim might have troubles locating those sourced files.
   
 
Do not forget that you can use <code>has()</code> and <code>exists()</code> to cope with the different capabilities offered by the various Vim builds and distributions.
 
Do not forget that you can use <code>has()</code> and <code>exists()</code> to cope with the different capabilities offered by the various Vim builds and distributions.
  +
  +
==Comments==

Latest revision as of 13:40, 7 March 2022

If you use Vim on both native Windows and Cygwin, or on several virtual machines, you have a couple of options to keep one copy of your vimrc file around, and share that copy among your systems and environments.

Note: For systems that use different line endings, you might need to use Unix line endings everywhere (which don't give problems in Vim for Windows). You can do so via EditorConfig (recommended) or by manually setting 'fileformat'.

The following assumes that you put your vimrc file and all its dependencies in a common directory (preferably under version control).

Option 1: using :source[]

By using the :source command, you can source your "real" vimrc (see above) from each system-specific vimrc. If your real vimrc is large, you can in fact have one "main" Vim script which in turn sources all the others as needed. Then, the system-specific vimrc's will just source the main script. (I recommend this kind of modularization even if you use option 2, below.)

For instance, the vimrc file in your Windows home will look something like:

source X:/path/to/common/dot-files/vim/my_vim.vim

Option 2: using symbolic links[]

Once you have the common files available somewhere, you can make each .vimrc that you need a symbolic link to the common .vimrc "main" file (see above). Under Unix and Cygwin:

$ ln -s /path/to/common/dot-files/vim/my_vim.vim .vimrc


This option, though, is only adequate if your vimrc is very simple. For instance, if it doesn't, in turn, source other files; because, in that case, Vim might have troubles locating those sourced files.

Do not forget that you can use has() and exists() to cope with the different capabilities offered by the various Vim builds and distributions.

Comments[]