Syntax folder under plugin/foo/syntax/foo.vim not automatically applied when filetype change?

wengwengweng 08/26/2018. 1 answers, 64 views
syntax-highlighting plugin-system

I have a plugin under nvim/plugin/foo, but the syntax file nvim/plugin/foo/syntax/foo.vim is not automatically applied when the filetype change, but if I put it under nvim/syntax/foo.vim it works

I'm now manually doing this in nvim/plugin/foo/autoload/foo.vim

let s:srcdir = expand('<sfile>:h:h:p')

func! foo#open()

    " plugin stuff
    exec 'source ' . fnameescape(s:srcdir . '/syntax/foo.vim')


which I don't know if it's the right way to deal with plugin syntax

1 Answers

D. Ben Knoble 09/25/2018.

Difference between vim plugins & packages

:help plugin tells us

Vim's functionality can be extended by adding plugins.  A plugin is nothing
more than a Vim script file that is loaded automatically when Vim starts.  You
can add a plugin very easily by dropping it in your plugin directory.

:help packages gives

A Vim package is a directory that contains one or more plugins.  The
advantages over normal plugins:
- A package can be downloaded as an archive and unpacked in its own directory.
  Thus the files are not mixed with files of other plugins.  That makes it
  easy to update and remove.
- A package can be a git, mercurial, etc. repository.  That makes it really
  easy to update.

So vim calls 'plugins' single script files that execute commands. 'Packages', on the other hand, contain one or more plugins. Unfortunately, vim doesn't distinguish here where perhaps it should.

The plugins you and I are familiar with are the GitHub repos and compressed directories where the structure matches that of the runtime path:

This is a list of directories which will be searched for runtime
      filetype.vim  filetypes by file name |new-filetype|
      scripts.vim   filetypes by file contents |new-filetype-scripts|
      autoload/ automatically loaded scripts |autoload-functions|
      colors/   color scheme files |:colorscheme|
      compiler/ compiler files |:compiler|
      doc/      documentation |write-local-help|
      ftplugin/ filetype plugins |write-filetype-plugin|
      indent/   indent scripts |indent-expression|
      keymap/   key mapping files |mbyte-keymap|
      lang/     menu translations |:menutrans|
      menu.vim  GUI menus |menu.vim|
      pack/     packages |:packadd|
      plugin/   plugin scripts |write-plugin|
      print/    files for printing |postscript-print-encoding|
      spell/    spell checking files |spell|
      syntax/   syntax files |mysyntaxfile|
      tutor/    files for vimtutor |tutor|

That is, what you and I conceive of as plugins are really mini-runtime directories. But, we just said a plugin is a single script?!

Well, no. A plugin script lives in &rtp/plugin and is just executed. A plugin in the more usual sense is a collection of scripts organized into this mini-runtime directory.

And each of those directories can live in a package. See my dotfiles for some examples of package organization.

What do I do?

Your question deals specifically with a (new) filetype and its accompanying syntax files. But, to play nicely in the vim plugin ecosystem, you still want to mimic the runtimepath structure.

  • ftdetect can hold vimscript to detect the filetype, as below; and
  • syntax can hold vimscript to define the syntax for the filetype

So you might have the following files

" myplugin/ftdetect/foo.vim
au BufNewFile,BufRead *.foo set filetype=foo

" myplugin/syntax/foo.vim
if exists("b:current_syntax")

echom "Our syntax highlighting code will go here."

let b:current_syntax = "foo"

" myplugin/autoload/myplugin.vim
function! myplugin#utility()
     " do something useful

myplugin can then be a git repo (or similar) that is "installed" to ~/.vim/pack/some_fancy_name/start/myplugin.


  1. Learn Vimscript the Hardway from Steve Losh contains a much deeper introduction to this material than I can present here. - Download Hi-Res Songs

1 Elohim

Holding Hands flac

Elohim. 2019. Writer: Quinn XCII;Jeremy Zucker;Elohim;Danny Parra;Corey Berkowitz;Antonina Armato.
2 P!nk

Walk Me Home flac

P!nk. 2019. Writer: P!nk;Scott Harris;Nate Ruess.
3 Katy Perry

365 flac

Katy Perry. 2019. Writer: Zedd;Katy Perry;Caroline Ailin;Corey Sanders;Daniel Davidsen;Cutfather;Peter Wallevik.

Happy flac

DEAMN. 2019.
5 Ariana Grande

7 Rings flac

Ariana Grande. 2019. Writer: Ariana Grande;Richard Rodgers;TBHits;Njomza;Michael "Mikey" Foster;Kaydence;Tayla Parx;Scootie;Oscar Hammerstein II;Victoria Monét.
6 5 Seconds Of Summer

Who Do You Love flac

5 Seconds Of Summer. 2019. Writer: Andrew Taggart;Talay Riley;Oak;Sean Douglas;Luke Hemmings;Calum Hood;Ashton Irwin;Michael Clifford;Trevorious;Zaire Koalo.
7 Bonn

No Sleep flac

Bonn. 2019. Writer: Albin Nedler;Bonn;Martin Garrix.
8 Bruno Mars

Please Me flac

Bruno Mars. 2019. Writer: J. White Did It;DJ White Shadow;Philip Lawrence;Bruno Mars;Cardi B.
9 Brooks

Better When You're Gone flac

Brooks. 2019. Writer: David Guetta;Emma Lov Block;Ido Zmishlany;Jackson Foote;Jeremy Dussolliet;Brooks.
10 Ariana Grande

Imagine flac

Ariana Grande. 2019. Writer: JProof;Priscilla Renea;Happy Perez;Andrew "Pop" Wansel;Ariana Grande.
11 Avril Lavigne

Dumb Blonde flac

Avril Lavigne. 2019. Writer: Mitch Allan;Bonnie McKee;Nicki Minaj;Avril Lavigne.
12 Avril Lavigne

Birdie flac

Avril Lavigne. 2019. Writer: J.R. Rotem;Avril Lavigne.
13 Ariana Grande

Thank U, Next flac

Ariana Grande. 2019. Writer: Crazy Mike;Scootie;Victoria Monét;Tayla Parx;TBHits;Ariana Grande.
14 Ariana Grande

Fake Smile flac

Ariana Grande. 2019. Writer: Joseph Frierson;Mary Frierson;Fred Ball;Happy Perez;Andrew "Pop" Wansel;Wendy Rene;Kennedi Lykken;Priscilla Renea;Justin Tranter;Ariana Grande.
15 Avril Lavigne

Warrior flac

Avril Lavigne. 2019. Writer: Chad Kroeger;Travis Clark;Chris Baseford;Avril Lavigne.
16 Nichkhun

Home flac

Nichkhun. 2019.
17 Dua Lipa

Swan Song flac

Dua Lipa. 2019. Writer: Dua Lipa;Justin Tranter;Kennedi Lykken;Mattias Larsson;Robin Fredriksson;Junkie XL.
18 Dimitri Vegas & Like Mike

Selfish flac

Dimitri Vegas & Like Mike. 2019. Writer: Victor Thell;Michael Thivaios;Maria Smith;Jeff Porcaro;Dimitri Thivaios;David Paich;Dan Book;Will Grands;ANGEMI.
19 Jason Derulo

Let's Shut Up And Dance flac

Jason Derulo. 2019. Writer: Bongo.
20 Ariana Grande

Needy flac

Ariana Grande. 2019. Writer: Tayla Parx;TBHits;Victoria Monét;Ariana Grande.

Related questions

Hot questions


Popular Tags