I was writing a simple Vim plug-in for a generic code generator called Templaty. The generator can theoretically work with any programming language, and I'd like Vim to automatically apply the correct highlighting.
From experiments, I have noticed Vim automatically loads the correct highlighter if I do something like set ft=c.templaty
. Now, I was wondering if there's a generic way to auto-detect the first part of this string. Right now, I brute-force it by keeping track of a small dictionary of file extensions and their corresponding file type, then doing exec "setfiletype" (filetype . ".templaty")
, but this is far from ideal.
Vim already has ftdetect
-logic, quite possibly with lots of user-defined ones. How would I be able to query this information programmatically?
Edit:
As suggested in the comments, I could just read b:current_syntax
, but the problem is that the additional .tply
file extension might prevent the default ftdetect
-logic from working. Would it be possible to tell Vim to try detecting the correct file type as if the file was without the .tply
extension?
unlet b:current_syntax
inside your code. Just don't do this and you can read it afterwards.Now, I was wondering if there's a generic way to auto-detect the first part of this string.
Could you explain a bit more about the scenario in which you need to run auto-detection? Is there a buffer? Is it saved? Does it have contents?ftdetect/
, so I suppose this is right after a file is loaded.