11

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?

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

3 Answers 3

5

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
3
  • 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
    Commented Jun 13, 2018 at 9:38
  • @smeikx please check the update on the answer.
    – mMontu
    Commented Jun 13, 2018 at 12:32
  • I went for the latter. For me, the perfect solution. Thank you very much!
    – smeikx
    Commented Jun 13, 2018 at 18:32
1

using the command

runtime! syntax/java.vim

will execute all the commands from the specified syntax file. It does the trick but the syntax variable is still set to the filetype as explained by the answer of mMontu

1
1

Neovim

you can use the vim.api.nvim_create_autocmd command and after/ftdetect/<filetype>.lua

Here is an example of detecting *.brs files.

-- after/ftdetect/brs.lua

local callback = function()
  vim.bo.filetype = "brs"       -- set filetype
  vim.bo.syntax = "basic"       -- set syntax
  vim.bo.commentstring = "'%s"  -- set comment string
  vim.bo.ts = 4                 -- set tapspaces 4
  vim.bo.sw = 4                 -- set shiftwidth 4
end

vim.api.nvim_create_autocmd({ "BufNewFile", "BufFilePost", "BufRead" }, {
  pattern = "*.brs",
  callback = callback,
})

Your Answer

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

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