6

I’m trying to set the syntax of Processing files (*.pde) to java with a ftplugin.

.vim/
  after/
    ftdetect/
        pde.vim
    ftplugin/
        processing.vim  

In after/ftdetect/pde.vim I detect the filetype:

autocmd BufNewFile,BufReadPost *.pde set filetype=processing

In after/ftplugin/processing.vim I set the syntax.

setlocal syntax=java

When I open file.pde :set ft? says processing. However, :set syntax? gives processing as well!

I made sure that ftplugin on is set, and setting other options (like e.g. tabstop=4) do take effect.

Also, :scriptnames gives ~/.vim/after/ftplugin/processing.vim as the last entry.

Has anybody an idea what’s going wrong?

  • 1
    What does :verbose set syntax? report? – muru Jun 12 '18 at 13:28
  • @muru It says /usr/local/share/vim/vim81/syntax/syntax.vim which is also the 6th entry in :scriptnames. – smeikx Jun 13 '18 at 8:06
1

It is unclear why you are using the after directory. From :help after-directory:

5. In the "after" directory in your home directory.  This is for
   personal preferences to overrule or add to the distributed defaults
   or system-wide settings (rarely needed).

So unless you are overwriting the ftplugin preferences from a plugin, you should use the .vim/ftplugin/processing.vim. For details you check :help filetype-plugin and :help write-filetype-plugin.

The filetype detection could also be done without the after, as explained in :help new-filetype. One of the recommend ways it to use the filetype.vim:

C. If your file type can be detected by the file name.
   1. Create your user runtime directory.  You would normally use the first
      item of the 'runtimepath' option.  Example for Unix: >
    :!mkdir ~/.vim

   2. Create a file that contains autocommands to detect the file type.
      Example: >
    " my filetype file
    if exists("did_load_filetypes")
      finish
    endif
    augroup filetypedetect
      au! BufRead,BufNewFile *.mine     setfiletype mine
      au! BufRead,BufNewFile *.xyz      setfiletype drawing
    augroup END
      Write this file as "filetype.vim" in your user runtime directory.  For
      example, for Unix: >
    :w ~/.vim/filetype.vim

Edit:

I moved ftdetect and ftplugin out of ~/.vim/after/ to ~/.vim/ but it doesn’t change anything.

Indeed, I tried it here with the same results. As suggested in the comments, :verbose set syntax? showed the culprit:

" Set up the connection between FileType and Syntax autocommands.
" This makes the syntax automatically set when the file type is detected.
augroup syntaxset
  au! FileType *    exe "set syntax=" . expand("<amatch>")
augroup END

From :help FileType:

FileType            When the 'filetype' option has been set. (...)

So despite you set the syntax correctly, after your ftplugin is loaded the autocmd above overwrites it.

As this is in the Vim runtime files, you shouldn't change it. So one option would be to set the filetype to java, and on the java ftplugincheck the file extension (e.g.: using expand("%:e")) and set the pde options instead of the java options.

Another approach would be delaying the setting of the syntax until the filetype is finished:

" ~/.vim/ftplugin/pde.vim
if exists("b:did_ftplugin")
   finish
endif

augroup pdeSyntax
   au!
   autocmd Syntax          <buffer> call SetPdeSyntax()
augroup END
function! SetPdeSyntax()
   " remove the augroup
   au! pdeSyntax
   set syntax=java
endfunction
  • I moved ftdetect and ftplugin out of ~/.vim/after/ to ~/.vim/ but it doesn’t change anything. Still, thanks for pointing that out and demonstrating the power of the :help system. – smeikx Jun 13 '18 at 9:38
  • @smeikx please check the update on the answer. – mMontu Jun 13 '18 at 12:32
  • I went for the latter. For me, the perfect solution. Thank you very much! – smeikx Jun 13 '18 at 18:32

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.