Vim: load syntax file from local directory - vim

I would like to be able to create a vim syntax file for a specific project, to highlight identifiers unique to that project. I could, of course, install a syntax file in $HOME/.vim/after/syntax/*.vim but I want to set this up as a per-folder configuration. The project syntax file should add on to the existing syntax file, not replace it. I also want a general solution that automatically loads a local file it if exists, and doens't need to be configured for specific directories in .vimrc or in a plugin.
Edit: This is similar to the question Vim: apply settings on files in directory but that deals with settings in .vimrc, which is loaded before syntax files. Syntax files conventionally wipe out predefined syntax information, so it is not useful to set this infomration in .vimrc, and the techniques to load alternate .vimrc files are therefore not helpful. I don't know of a way to trigger additional "after" files to be loaded. I have seen this question asked before but no answers except those pertaining to .vimrc.
I have configured local .vimrc files using :set exrc and have verified that these files get loaded, but syntax definitions in .vimrc get overwritten by syntax files that are loaded later on. (And I want project syntax to add to standard syntax, not replace it.)
Is a similar functionality, whether a vim feature or a plugin, that can be used for local "after" syntax files?

I adapted a solution based on this answer: https://stackoverflow.com/a/13192721/2150289. While I'd still like to explore other options if they exist, this works. Leaving this as a separate question and answer, since .vimrc answers don't apply, and it is not obvious that this is possible for a syntax file.
In my project directory I have a local .vimrc that sets tabs and so forth. This is loaded by set exrc or any other method that loads a local .vimrc.
To load "after" files, you can daisy-chain them to an "after" file that gets loaded in your .vim/after directory. For example, I have a .vim_syntax_cpp.vim with C++ syntax specific to the project. I place this is the project directory:
PROJECT/
.vimrc # gets loaded by set exrc
.vim_syntax_cpp # daisy chained from .vim/after/syntax/cpp.vim
Makefile
foo.cpp
bar.cpp
In ~/.vim/after/syntax/cpp.vim, I place this code:
" load syntax file if it exists in the
" current directory or parent directory
if filereadable(".vim_syntax_cpp.vim")
source .vim_syntax_cpp.vim
elseif filereadable("../.vim_syntax_cpp.vim")
source ../.vim_syntax_cpp.vim
endif
I have verified that this loads the local syntax file and adds them to existing syntax definitions.
This method has limitations; see the comments in the linked answer.

Related

Syntax file sourced from the plugin is overwritten by Vim

I am creating a very simple plugin to make syntax highlighting better in Vim for baan syntax (basically an improved version of syntax/baan.vim).
This is my plugin directory.
One file inside ftdetect folder; baan.vim. It looks like this.
au BufRead,BufNewFile *.bc set filetype=baan
The file inside syntax folder; baan.vim. It is almost the same as baan.vim in syntax folder within Vim. With one line of change in syntax highlighting.
My problem is when I open any files with .bc extensions, two more syntax files are called; one before my own plugin syntax file, and other one is after.
bc.vim syntax file is called already from filetyp.vim, I guess. Because filetypes I want to set syntax is with extension .bc. This is the first issue. Second one is that I set filetype to 'baan' but Vim is looking and sourcing all baan.vim files. But once baan.vim is source from myplugin, it is still sourcing the one from Vim itself. How can I solve those issues elegantly without using /after directory?
This is normal. Consider the following.
$VIMRUNTIME/filetype.vim sources ftdetect/*.vim scripts near to the end. At this point setf bc was already executed and the first FileType event was triggered and processed. BTW. This is the reason why late set ft=baan works but setf baan no more. Shouldn't be a problem though, as :syn clear is executed in $VIMRUNTIME/syntax/synload.vim automatically. And no, you can't do anything with this unless you patch/replace filetype.vim.
synload.vim intentionally sources all matching files (:runtime!). This is the reason they respect b:current_syntax variable. The first to set it wins, others step aside by executing :finish at a top. Nonetheless, they all are sourced and get into :scriptnames. Note this also means that extending existing syntax with after/syntax (while not respecting b:current_syntax) is usually more preferable.

How to load a vimscript file after loading plugin utl.vim?

I need to source some configuration lines after loading a plugin called [utl.vim][1].
The documentation says, I need to put these configurations into after-directory:
[23] NOte that you cannot include this line in your .vimrc because it is
:source'd before utl_rc.vim (which defines g:utl_cfg_hdl_mt_generic).
So either include/change it at <url:config:#r=utl_cfg_hdl_mt_text_directory>
or include it in the after-directory, see <url:vimhelp:after-directory#5.>
I created a file called utl.vim inside ~/.vim/after/ directory.
But this file is not sourced. I verified this using :scriptnames.
How to assure that Vim sources this file after loading utl.vim plugin?
The list of files/directories loaded by Vim is described at :help startup. Despite it lists the plugin directories, it doesn't mentions the after directory, so what you are seeing is the expected behavior.
Usually the after directory is source for filetype plugins, as explained in Vim FAQ 26.3 - "How do I extend an existing filetype plugin?". It is possible that the plugin will run something like :runtime! after/**/*.vim, but if the documentation is unclear will should ask to the plugin author, because it would be probably easier for users to be allowed to set this variable, and them the plugin could append/prepend default values to it

How to execute a file everytime before opening Vim

I have recently started using vim and I really like it. I have added a few easy mappings in my vimrc file.
But the problem is I get to use a lot of remote machines a lot of time and I can't copy my vimrc on to them but most of the times I won't have enough permissions to do that.
So, I was wondering if there is any way I can put all my vim mappings in a file and tell vim to run it every time it loads, just like a vimrc?
The action that is "parsing" the .vimrc is called source.
In runtime, you can reapply/reparse your .vimrc by using
:source ~/.vimrc
So if you can somehow copy your .vimrc, even if not in your home, but a folder like /tmp you should be able to source it from there, with
:source /tmp/.vimrc
This question has more details and solutions.
One option would be to specify an alternative .vimrc file while launching the program.
The vim man pages has this to say about specifying a vimrc file:
-u {vimrc}
Use the commands in the file {vimrc} for initializations. All the other initializations are skipped. Use this
to edit a special kind of files. It can also be used to skip all
initializations by giving the name "NONE".
See ":help initialization" within vim for more details.
Note that this option overrides the default vimrc file, so you'll have to specify all of your settings/options in this file.
As was mentioned in another answer, you can place your custom vimrc file anywhere you want (or have access to) and then specify the the -u option with the path to your vimrc file.
You could even combine this method with managing your custom vimrc file in an online version control system (like github) - this way you will be able to wget your file from the web instead of having to manually copy it from machine to machine.

Vim doesn't show a certain file with colors

Vim doesn't show a certain file with colors. However, renaming this file will avoid this problem. Besides, files of the same type don't have the same problem.
What can be a problem?
If the file extension is not recognized for syntax highlighting you can use set syntax command.
Here is an example to set the current window html syntax highlighting:
:set syntax=html
If you want to set the file extension to be recognized to a specific file extension I would follow the instructions here:
vim: persistent :set syntax for a given filetype?
Since you asked why a certain file doesn't use syntax coloring, here's the reason behind it:
The swap file!
You somehow changed the syntax value, which gets stored in the swap file. Renaming solves your problem, because Vim doesn't have the old swap file associated with it.
Yes, you fixed your problem by :set syntax=foo. But if you encounter problems, which only apply to a specific file, then it's rooted always in the swap file! There are tons of problems like this, which can be solved by deleting the swap file and therefore restoring the default expected behaviour.
The variable for the swap directory can be set like this:
:set dir=~/vimfiles/swap (my swap)
Defaults from Vims help (:h dir):
for MS-DOS and Win32: ".,c:\tmp,c:\temp"
for Unix: ".,~/tmp,/var/tmp,/tmp")

howto change location of .vimrc and .vim

How can I change the location of the .vim folder and the .vimrc file so that I can use two (or more) independent versions of vim? Is there a way to configure that while compiling vim from source? (maybe an entry in the feature.h?)
Why do I want to do such a thing?:
I have to work on project that use python2 as well as python3, therefore I want to have two independent vim setups with different plugins, configurations etc. Moreover, one version has to be compiled with +python, the other with +python3.
You can influence which ~/.vimrc is used via the -u vimrc-file command-line argument. Since this is the first initialization, you can then influence from where plugins are loaded (i.e. the .vim location) by modifying 'runtimepath' in there.
Note that for editing Python files of different versions, those settings (like indent, completion sources, etc.) are taken from filetype plugins which are sourced for every buffer separately, so it should be possible to even edit both Python 2 and 3 in the same Vim instance. (Unless you have some badly written plugins that define global stuff.) So for that, some sort of per-buffer configuration (some :autocmds on the project path, or some more elaborate solution (search for localrc plugins or questions about project vimrc here) might suffice already.
Also note that when the Python interpreter (which you'd only need for Python-based plugins and some interactive :py commands, not for editing Python) is compiled in with dynamic linking (which is the default at least on Windows), you can have both Python 2 and 3 support in the same Vim binary.
I think the easiest solution would be just to let pathogen handle your runtimepath for you.
pathogen#infect() can take paths that specify different directories that you can use for your bundle directory.
So if your .vim directory would look like this
.vim/
autoload/
pathogen.vim
bundle_python2/
<plugins>
bundle_python3/
<other plugins>
Then inside one of your vimrc for python 2 specific stuff you would have
call pathogen#infect('bundle_python2/{}')
and for python 3 specific stuff you would have
call pathogen#infect('bundle_python3/{}')
Since each plugin folder is really just a .vim folder you can place your python specific configuration stuff in a folder of the corresponding bundle and pretend its a .vim.
This structure also has the added benefit that you can change both configurations at the same time if you feel like it by putting common stuff in .vim normally.
You can also pass multiple bundle directories if you feel like to pathogen so you can have plugins that are shared without duplicating files. You just do this by passing multiple paths to pathogen#infect('bundle/{}', 'bundle_python3/{}')
After this is all done you can just create aliases for vim to call the correct vimrc file.
I found a way to do this!
You can just create a fake $HOME, whose contents are simply the .vim folder and .vimrc. Then, when running vim, set the HOME environment variable to that folder, and change it back on VimEnter.
Run vim with:
OLD_HOME="$HOME" HOME="$FAKE_HOME" vim
Add this to your .vimrc:
autocmd VimEnter * let $HOME = $OLD_HOME
On Windows you can use
let $HOME = $HOMEDRIVE.$HOMEPATH
insetead, no need to store the old home.
This works, but if you use $HOME inside your vimrc or any plugins will see the old value, it might affect them somehow. So far I haven't seen it.
Note: I don't really recommend doing this.
If you really really want to recompile vim so that it uses a different vimrc and different configuration directory take a look at src/feature.h
Search this file for USR_VIMRC_FILE. Uncomment it and place the name of your vimrc here. This will change the defualt vimrc file.
So it should look something like this
#define USR_VIMRC_FILE "~/path/to/vimrc"
Inside src/os_unix.h or src/os_mac.h and search for DFLT_RUNTIMEPATH. Change all instance of ~/.vim to whatever folder you want. This should set the default runtime path that vim searches for settings.

Resources