2

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?

Alcamtar
  • 1,478
  • 13
  • 19
  • You could define an auto command to load your syntax file only when editing files in specified directories. See: https://superuser.com/questions/598947/setting-vim-options-only-for-files-in-a-certain-directory-tree/598970. So you could do: `autocmd BufRead,BufNewFile /path/to/dir/* source $HOME/.vim/syntax/somesyntax.vim` – builder-7000 May 09 '18 at 02:59
  • 1
    Possible duplicate of [Vim: apply settings on files in directory](https://stackoverflow.com/questions/456792/vim-apply-settings-on-files-in-directory) – Doktor OSwaldo May 09 '18 at 05:45

1 Answers1

1

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.

Alcamtar
  • 1,478
  • 13
  • 19
  • why don't you have your local file load up vim.cpp instead – Ahmed Masud May 11 '18 at 20:00
  • .vimrc is loaded before a file buffer is created, and syntax settings are loaded afterward. Any syntax settings in .vimrc are lost when the file is loaded... I think it is because the global syntax file wipes them out. I don't want to disable the global syntax, just add to it. – Alcamtar May 15 '18 at 07:03
  • You might want to create a file `~/.vim/after/syntax/cpp/foobar.vim` (and/or `~/.vim/after/syntax/c/foobar.vim` for C) instead. Also, you can use environment variables so you don't have to guess from the current directory (I set an environment variable to point to the root of my current project called `REPOBASE`); e.g. `if filereadable($REPOBASE . "-objdir/tags.vim")` // `source $REPOBASE-objdir/tags.vim` // `endif` – Carlo Wood Jun 28 '22 at 08:36