1

I want some common syntax highlighting for various filetypes.

Currently I have runtimepath/after/syntax/base.vim which has syntax rules I want and then I created mklink (this is windows) to base.vim for all filetypes (eg javascript.vim, c.vim, etc) that I want to have same /after processing.

In this way single edit (in base.vim) will have effect in all filetypes I want to propagate to.

Above works, but is there a better way to do it?

2
  • This question is related, I think
    – Rich
    May 6, 2020 at 8:10
  • Hi, welcome here. I think you should try to make your post more readable. Try to use the simple markdown syntax the site offers, as well as more new lines. This will increase your chance of getting answers :)
    – Biggybi
    May 6, 2020 at 11:27

2 Answers 2

1

If in specific you would like to avoid the mklink on Windows or symlinks on Linux/Unix/Mac, you can use small files for the specific languages that simply load base.vim.

The technique is recommended in :help :syn-files:

When a language is a superset of another language, it may include the other one, for example, the cpp.vim file could include the c.vim file:

:so $VIMRUNTIME/syntax/c.vim

In your case, to source file base.vim from the same directory where the language file is located, you can use:

source :h/base.vim

As the whole contents of files javascript.vim, c.vim, etc. in your $HOME/vimfiles/after/syntax/.

If the name syntax/base.vim is unique, you can also use the runtime command to load it, which will search the file in the directories listed under 'runtimepath':

runtime syntax/base.vim

Or if you want to load all files named syntax/base.vim in directories listed under 'runtimepath', then use runtime! instead.

3
  • 1
    runtime would work equally well, no?
    – D. Ben Knoble
    May 6, 2020 at 14:13
  • @D.BenKnoble Yes. I updated the answer to include an option using :runtime. The one caveat with :runtime is that the name syntax/base.vim needs to be quite unique (or you're willing to source all of them), so I think personally I still lean towards the :source approach in this particular case.
    – filbranden
    May 6, 2020 at 20:04
  • 1
    @filbranden♦ This is good, thx. More in line with Vim design than mklink
    – Giorgi Sh.
    May 6, 2020 at 23:42
0

You can use mysyntaxfile hook. E.g.

" in vimrc
let g:mysyntaxfile = '~/vimfiles/after/syntax/base.vim'
syntax on
...

" in base.vim
autocmd Syntax javascript,c call DoWhatever(expand('<amatch>'))

Note that you don't need augroup and stuff, as synload.vim manages syntax files by its own.

5
  • :help synload-2 says: "Source the user's optional file, from the mysyntaxfile variable. This is for backwards compatibility with Vim 5.x only." :help mysyntaxfile basically covers creating a new file named after a filetype under the syntax/ directory, so I imagine that's the preferred approach these days.
    – filbranden
    May 6, 2020 at 20:11
  • @filbranden I think there's a good reason to put it to use it if it saves us a dozen of similar edits.
    – Matt
    May 6, 2020 at 20:42
  • @Matt: And while you can use au's in base vim, one can also have usual syntax commands as well, eg syn keyword someSyntax someKWord right?
    – Giorgi Sh.
    May 7, 2020 at 0:11
  • @GiorgiShonia In Vim there's no real difference between "colorschemes", "sessions", "syntaxes" etc. They all are just VimScript.
    – Matt
    May 7, 2020 at 4:41
  • @GiorgiShonia In fact, the main reason why we can't just trap into autocmd Syntax x,y,z is because we should play nicely with syntax on/off
    – Matt
    May 7, 2020 at 4:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.