How to add a vimrc path to vim - vim

currently there are a few paths where .vimrc files are being searched. (as can be seen in :scriptnames command).
How do I add another path?

You're mistaken. :scriptnames tells you which scripts were loaded. It will be:
.vimrc,
possibly .gvimrc,
plus all plugins in 'runtimepath',
plus all ftplugins, syntax files and indent files in runtimepath that apply for all the buffers you have edited,
plus the autoload plugins loaded by the previous files,
plus the files you've sourced manually.
If you want to change the places where to search for your .vimrc, it will be more complex as vim has a very specific heuristic to search for a .vimrc. See :h startup.
Any way. If you really want to add a path where the .vimrc file will be searched, it's not possible unless you define an alias to vim that use the -u flags.
If you want to add other paths where to look for plugins, you'll have to set the 'runtimepath' option in your .vimrc. For instance, :set rtp+=~/.vim/addon/foobar will have all plugins named $HOME/.vim/addon/foo/plugin/*.vim and $HOME/.vim/addon/foo/after/plugin/*.vim loaded automatically, plus the ftplugin/syntax file/indent files loaded automatically as well if you enable them, and when you enter a buffer related to them.

The 'runtimepath' option specifies the locations of the Vim configuration subdirectories (i.e. directories containing autoload/, plugin/, syntax/, etc.) The Pathogen plugin made it popular to extend this so that each plugin is installed into a separate such subdirectory, and other plugin managers (like Vundle) do that as well.
Now, there's only one .vimrc (and you can change its location via the -u command-line argument), but nothing prevents you from using :source path/to/another/script.vim to load other Vim scripts during startup.
TL;DR
To execute a separate Vimscript file during startup, just :source it from your ~/.vimrc. If you have a plugin(s) that you want to install in a separate location, use :set runtimepath+=path/to/pluginroot in your ~/.vimrc, or just use Pathogen or another plugin manager.

Related

VIM: Read "variable" from current directory / project

So in my .vimrc I have this:
let g:syntastic_python_pylint_post_args="--max-line-length=80"
I want to have something like:
let g:syntastic_python_pylint_post_args="$(cat .line_length.txt)"
And I want this cat command to run whenever I open vim.
For example if I have a file in a project
myproject/.line_length.txt
And the contents of the .line_length.txt is 120 for example and I run
vim
inside that directory, it should read that file and set the contents of the file to the variable.
...
Is this possible somehow?
I have tried using project specific .vimrc files but it does not seem to read
let g:syntastic_python_pylint_post_args="--max-line-length=120"
It will read set ... lines though, but not let g: ... lines.
A literal translation of your attempted $(...) syntax would be this:
let g:syntastic_python_pylint_post_args = substitute(system('cat .line_length.txt'), '\n\+$', '', '')
But there's actually no need to run an external command for this. Vim has the low-level :help readfile() function:
let g:syntastic_python_pylint_post_args = readfile('.line_length.txt')[0]
Caveats
Depending on the location you start Vim in, the configuration may not exist. You need to account for that, probably using a filereadable() conditional around it, or just ignoring any errors with :silent!.
Some plugins only read their configuration variables during startup, and after that ignore any changes to it. This should be fine for your plan to run in ~/.vimrc, but it may affect your ability to "reload" a different project during runtime (see below).
Refresher on local configuration
Your solution depends on Vim being started inside the "project directory". You've also mentioned project-specific vimrc as an alternative. Here are some options for that:
Central configuration
If it's okay to configure the specific commands / local exceptions centrally, you can put such autocmds into your ~/.vimrc:
:autocmd BufRead,BufNewFile /path/to/dir/* setlocal ts=4 sw=4
It is important to use :setlocal instead of :set, and likewise :map <buffer> ... and :command! -buffer ....
On the other hand, if you want the specific configuration stored with the project (and don't want to embed this in all files via modelines), you have the following two options:
Local config with built-in functionality
If you always start Vim from the project root directory, the built-in
:set exrc
enables the reading of a .vimrc file from the current directory. You can place the :set ts=4 sw=4 commands in there.
Local config through plugin
Otherwise, you need the help of a plugin; there are several on vim.org; I can recommend the localrc plugin (especially with my own enhancements), which even allows local filetype-specific configuration.
Note that reading configuration from the file system has security implications; you may want to :set secure.

Are plugins loaded by vimrc or afterwards?

I'm confused about the order in which Vim loads plugin files, and seem to be finding mixed answers online. Some answers seem to suggest that vimrc is loaded before plugins, while others suggest that they are loaded during the sourcing of vimrc, at the line filetype plugin indent on. Can someone please clarify the order in which vimrc, plugins, and plugins in the after/ directory are loaded, what causes each to load, and when each could be reloaded during a vim session (e.g. what happens when sourcing vimrc again, what happens when setting the filetype, etc.)?
Some answers seem to suggest that vimrc is loaded before plugins, while others suggest that they are loaded during the sourcing of vimrc, at the line filetype plugin indent on.
All plugins are sourced (the correct term) after your vimrc unless you source them manually. The filetype plugin indent on line doesn't change anything to that order.
Can someone please clarify the order in which vimrc, plugins, and plugins in the after/ directory are loaded,
Assuming you have filetype plugin indent on in your vimrc:
The system vimrc if there is one.
Your vimrc.
Built-in plugins.
Your plugins.
Built-in filetype-specific plugins.
Stuff in the after/ directory.
The whole thing is explained in :help startup and can be seen very clearly with :scriptnames.
what causes each to load,
The value of &runtimepath in general and the :filetype command for filetype-specific stuff.
and when each could be reloaded during a vim session (e.g. what happens when sourcing vimrc again, what happens when setting the filetype, etc.)?
:source $MYVIMRC re-executes each command in your vimrc.
Most plugins are written in a way that prevent them from being sourced twice. Read their documentation/code if you want to reset them.
:help :filetype.
.vimrc is executed before loading plugins:
At startup, Vim checks environment variables and files and sets values
accordingly. Vim proceeds in this order:
(...)
Execute Ex commands, from environment variables and/or files
An environment variable is read as one Ex command line, where multiple
commands must be separated with '|' or "".
vimrc exrc
A file that contains initialization commands is called a "vimrc" file.
Each line in a vimrc file is executed as an Ex command line.
(...)
Load the plugin scripts.
Just use :scriptnames to see all the sources files and their order in which they are loaded during startup.

vundle: ConqueTerm: not an edit command

I tried to install Conque-Shell via Vundle, and when I input :ConqueTerm bash in vim, it showed 'ConqueTerm: Not an edit command'. I thought there was something wrong with my path. But I did write set rtp+=~/.vim/bundle/vundle in my .gvimrc. And the configure of Vundle is at the beginning of the .gvimrc.
I copied the .vim/bundle/Conque-Shell/plugin/conque_term.vim to .vim/plugin/conque_term.vim and then it worked.
So, is there anything wrong with my .gvimrc?? Thanks!
My .gvimrc: https://gist.github.com/guori12321/5506991
If you observe the output of :scriptnames (the list of scripts sourced during startup), you'll note that .gvimrc comes last, after .vimrc and the plugins. Therefore, any changes to the 'runtimepath' there are too late; plugins (like ConqueTerm) have already been loaded. You can read more about the startup process at :help initialization.
Even if you only use the GUI GVIM, put the common settings into ~/.vimrc; the ~/.gvimrc file is for GUI-only stuff that doesn't apply to the terminal Vim, e.g. setting 'guifont' and similar options.

How to tell Vim not to use ~/.vim

By default Vim is looking for plugins and other stuff in ~/.vim.
Is there any way to tell Vim to search for plugins, etc. in ~/.other_folder and force it to ignore ~/.vim entirely?
Vim uses the comma-separated paths from 'runtimepath' to determine where to look for :runtime'd files.
You can change that option either in ~/.vimrc (which is sourced as the very first script), or by passing the set rtp=... commands on Vim's command-line via --cmd (the commands passed with -c are only applied after startup).

What can I do if vim still chooses the wrong syntax highlighter?

I'm on a system (linux) that always recognizes cpp files (*.cc) as tcl files. I don't know what file type that is, but I wanted to override it. The correct syntax highlighting is chosen if I manually do :set ft=cpp. However, I'm having troubles setting that automatically and I don't want to use the modeline option. My own .vimrc doesn't interfere (same result if I rename it).
From the vim help (:help ftplugin-override)
*ftplugin-overrule*
If a global filetype plugin does not do exactly what you want, there are three
ways to change this:
1. Add a few settings.
You must create a new filetype plugin in a directory early in
'runtimepath'. For Unix, for example you could use this file:
vim ~/.vim/ftplugin/fortran.vim
You can set those settings and mappings that you would like to add. Note
that the global plugin will be loaded after this, it may overrule the
settings that you do here. If this is the case, you need to use one of the
following two methods.
I have used this option before on another machine and that worked. I've tried
<file> .vim/ftplugin/tcl.vim
set filetype=cpp
"au BufRead,BufNewFile * set filetype=cpp
The first line correctly sets the filetype (:set ft? returns cpp), but syntax highlighting is not the same as if I said :set ft=cpp. It's still the tcl syntax highlighting. The second line does nothing.
2. Make a copy of the plugin and change it.
You must put the copy in a directory early in 'runtimepath'. For Unix, for
example, you could do this:
cp $VIMRUNTIME/ftplugin/fortran.vim ~/.vim/ftplugin/fortran.vim
Then you can edit the copied file to your liking. Since the b:did_ftplugin
variable will be set, the global plugin will not be loaded.
A disadvantage of this method is that when the distributed plugin gets
improved, you will have to copy and modify it again.
There seems to be no file in my $VIMRUNTIME directory /usr/share/vim/vim72/ftplugin/ called tcl.vim.
3. Overrule the settings after loading the global plugin.
You must create a new filetype plugin in a directory from the end of
'runtimepath'. For Unix, for example, you could use this file:
vim ~/.vim/after/ftplugin/fortran.vim
In this file you can change just those settings that you want to change.
Has the same effect as 1. Is there anything else I can try? Thanks a lot in advance.
cpp is the default filetype for *.cc and *.cpp files.
The tcl filetype is only set for *.tcl, *.tk, *.itcl, *.itk and *.jacl.
I see no reason why Vim would default to tcl when loading a *.cc file but you could check if theses files are installed:
/usr/share/vim/vim7x/ftplugin/cpp.vim
/usr/share/vim/vim7x/indent/cpp.vim
/usr/share/vim/vim7x/syntax/cpp.vim
and if the correct checks are done in:
/usr/shhare/vim/vim7x/filetype.vim
Are you the only person working on this machine? Do you use modelines?

Resources