How do I "source" something in my .vimrc file?

PluginsViVim

Plugins Problem Overview


I've been working on expanding my vim-foo lately and I've run across a couple of plugins (autotag.vim for example) that require them to be "sourced" in my .vimrc file. What exactly does this mean and how do I do it?

Plugins Solutions


Solution 1 - Plugins

Sourcing a file is 'executing' it. Essentially, each line of the file is considered a command. Sourcing it is the same as typing each command in order. You source with the command :source (usually shortened to :so).

So if you source myStuff.vim

:so myStuff.vim

and if myStuff.vim contained these lines

set xx iI just intersted this<C-]>
set yy bbbb4dw

It's the same as if you typed those commands into Vim

:set xx iI just intersted this<C-]>
:set yy bbbb4dw

The only file sourced by default is the .vimrc(_vimrc on windows) so that's a place you can keep all the commands you use to set up Vim every time.

Where it gets interesting is the fact that since a sourced file is just a series of commands, and sourcing is a command, you can source files from your source files. So plugins you use every time could be sourced when you start up Vim by adding a line to your .vimrc like this

 so myPlugin.vim

Solution 2 - Plugins

Files in your .vim/plugin directory are sourced (loaded) automatically.

Solution 3 - Plugins

There is always the :source file command. I usually write .vimrc that contains custom commands and what not for the console application and then a .gvimrc that contains additional goodies that are appropriate for a windowed version. My .gvimrc starts with source $HOME/.vimrc to pick up everything from the console version before adding in new stuff.

Solution 4 - Plugins

There are normally two vimrc files, one is _vimrc and the other _gvimrc (in the first one are the things for vim, and in the second for gvim - graphical things) - although most people I know just put everything in _vimrc.

A good practice is to keep all your extra files (plugins, colorschemes, snippets ...) in a separate (your own) vimfiles directory (which you can take with you).

If you do

:help vimfiles

vim will tell your vimfiles directory should be located. It depends somewhat on the platform (win, unix). On windows the usual is in your user folder (documents and settings, then user ...). In vimfiles directory there are a couple of subdirectories. Amongst them is the "plugin" subdirectory. Plugins put in that dir will be loaded automatically (also plugins put in subdirectories of "plugin"). If you do not wish to load it automatically, just put it in your "vimfiles", or some other directory, and

:so plugin_name.vim (with the appropriate path)
(you can use the $vim, $vimfiles, and $home as shortcuts when defining path to plugin)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaul WicksView Question on Stackoverflow
Solution 1 - PluginsWhaledawgView Answer on Stackoverflow
Solution 2 - PluginsMichiel de MareView Answer on Stackoverflow
Solution 3 - PluginsD.ShawleyView Answer on Stackoverflow
Solution 4 - PluginsRookView Answer on Stackoverflow