For context, see How can I automate filetype detection when using sudoedit?
foo, which is detected based on varying factors, including the name, the full path or the contents of the file (or combinations thereof). I have a buffer containing
filea's contents, and I want to apply
filetype, without actually opening
Assume that I already have the path and name of the file, and the contents are in the current buffer.
How do I go about this?
Note that I may not be able to open the actual file. A simple example is Polkit configuration in Ubuntu systems. Files in
/etc/polkit-1/localauthority are accessible only to root.
Thanks to Carpetsmoker's tip about
:filetype detect, I came up with one way. Something like the following could do the trick:
function SetActualFiletype (actual_path) let l:cur_file=expand('%') exec 'file' fnameescape(a:actual_path) filetype detect exec 'file' fnameescape(cur_file) endfunction
This way, Vim has access to everything it would normally have when opening that file.
This isn't safe from
autocmds which are writing the file (and other such oddities).