13 votes
Accepted

How to do file type detection based on file contents (e.g. a certain shebang line)?

If you want to introduce a file type and that file type can only be identified by examining the contents of such files then you'll need to add appropriate logic to a file named scripts.vim. But first, ...
B Layer's user avatar
  • 19.7k
4 votes
Accepted

Is it possible to override the filetype of a text file based on the first line?

Yes, you should be able to set the filetype based on the contents of the file. If you add a script to the ftdetect directory, this script should set up a new autocmd to trigger when a file of a ...
filbranden's user avatar
  • 28.7k
3 votes
Accepted

Why are files in after/ftplugin/ run on every save, and can I work around it?

In the end, the comments above were very helpful in narrowing down the problem - thanks, everyone! I've resolved it now, and the filetype is only set once. To summarize, there were several issues here:...
Andrew Ferrier's user avatar
3 votes

How can I avoid built-in filetype detection?

The *.k extension is detected by $VIMRUNTIME/filetype.vim that comes before the ~/.vim/ftdetect/*.vim in the execution list. That is why you have these scripts being evaluated (kwd.vim calling cpp.vim ...
Vivian De Smedt's user avatar
2 votes
Accepted

How can I avoid built-in filetype detection?

After a lot of trial and error, I found a solution. It turns out that the order of entries in runtimepath doesn't matter. All I had to do was use au! instead of au in the plugin's ftdetect/k.vim. ...
ngn's user avatar
  • 163
2 votes

How can I avoid built-in filetype detection?

You don't generally need to suppress the filetype detection, because (proper) filetype plugins set b:undo_ftplugin and other variables, which Vim uses automatically to "undo" the results of ...
D. Ben Knoble's user avatar
  • 25.8k
2 votes

ftdetect, ftplugin not working as advertised -- what am I missing?

Everything is "working as advertised". To expand on @Matt's answer… There are two ways to execute commands, set options, etc. for a given filetype. One is to use autocommands on the ...
romainl's user avatar
  • 40k
2 votes
Accepted

ftdetect, ftplugin not working as advertised -- what am I missing?

Do not prepend autocmd Set cindent option Do not set syntax option
Matt's user avatar
  • 20.6k
1 vote

Is it possible to override the filetype of a text file based on the first line?

You can create $HOME/.vim/scripts.vim of your own, as $VIMRUNTIME/filetype.vim will source it by executing :runtime! scripts.vim if needed. It's enough to put only these lines: if getline(1) =~# '^...
Matt's user avatar
  • 20.6k

Only top scored, non community-wiki answers of a minimum length are eligible