Is it possible to create additional subcategories of filetypes?

For example, it would be great if (neo)vim could pick up that the yaml file I'm editing is acutally a Kubernetes yaml file and set the filetype to something more specific: setfiletype=yaml-kubernetes. For this example, this might be based on the extension AND the contents: e.g. 'apiVersion: v1'

Currently, the blunt filetype logic for yaml file detection is in /usr/share/nvim/runtime/filetype.vim and is based on file extension:

" Yaml
au BufNewFile,BufRead *.yaml,*.yml      setf yaml

The solution I'm looking for ideally is to extend this in the users' config rather than to overwrite the system filetype.vim file.

This would allow user config to include filetype-specific shortcuts, for example enabling kubectl apply -f % for relevant matched files (not applicable for all yaml files).

  • 3
    Related to How to create a new Vim filetype?. Also :help new-filetype spells out the process. You might find item D. (:help new-filetype-scripts) especially interesting. I'd write an answer but I can't hope to do better than what's already in :help.
    – Friedrich
    Commented Oct 20, 2023 at 6:23

2 Answers 2



This is called a « dotted filetype », and it has some subtlety but can be used for the mechanism you want:

set filetype=yaml.kubernetes

Then you can customize the kubernetes filetype. Beware of accidentally undoing anything from the yaml filetype you meant to keep, though.

  • 1
    Great to learn that today. Thanks.
    – gildux
    Commented Oct 25, 2023 at 18:54

Thanks Ben for the partial solution that got me to the answer (along with this tip from SO)

However, just using:

autocmd BufRead *.yaml,*.yml setfiletype yaml.kube

doesn't work because non-kube YAML files will get the wrong filetype.

So it needs to be combined with some logic to pick up something specific to the type of YAML file that you want to subcategorise.

For my purposes, the string "apiVersion: " seems to be satisfactory for this.

Adding the following to init.vim/vimrc seems to be good enough (haven't found any falsely classified examples with brief testing):

" Create a unique filetype for Kubernetes YAML
au BufRead *.yaml,*.yml
  \ if search('apiVersion: ', 'nw') | 
  \ setlocal ft=yaml.kube | 
  \ endif
  • 1
    I would suggest adding this to ~/.vim/ftdetect/kubernetes.vim or ~/.vim/scripts.vim: see all the stuff in :help ftdetect
    – D. Ben Knoble
    Commented Oct 25, 2023 at 20:12

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.