0

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')

endfunc

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

  • The runtime plugin directory is not what you think it is. More details at a later date; in the meantime, read the help page about plugins and packages. – D. Ben Knoble Aug 27 '18 at 2:39
0

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 vim.org compressed directories where the structure matches that of the runtime path:

This is a list of directories which will be searched for runtime
    files:
      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")
     finish
endif

echom "Our syntax highlighting code will go here."

let b:current_syntax = "foo"

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

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


Notes

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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